gpt4 book ai didi

node.js - 使用 SSL 的 nodejs 应用程序的 Nginx 配置

转载 作者:太空宇宙 更新时间:2023-11-03 15:13:48 24 4
gpt4 key购买 nike

我有一个 Node 应用程序运行在

localhost:8008/serviceName

此应用程序位于 /opt/myfolder/test/ 中,我还在其中生成了一个 certs 文件夹、一个 .pem 文件和一个 .cer 由 CA 签名的文件。我还安装了 pm2 来帮助我从 https 端点运行此服务器。

我创建了一个名为 service.conf 的文件并将其符号链接(symbolic link)到 /etc/nginx/sites-enabled

这是 .conf 文件的样子

    server {
listen 443;

server_name localhost

root /opt/myfolder/test/public;
index index.html index.htm;

ssl on;
ssl_certificate /opt/myfolder/test/certs/server.cer
ssl_certificate_key /opt/myfolder/test/certs/server.pem;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers "ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES256-SHA:ECDHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA256:DHE-RSA-AES128-SHA256:DHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA:ECDHE-RSA-DES-CBC3-SHA:EDH-RSA-DES-CBC3-SHA:AES256-GCM-SHA384:AES128-GCM-SHA256:AES256-SHA256:AES128-SHA256:AES256-SHA:AES128-SHA:DES-CBC3-SHA:HIGH:!aNULL:!eNULL:!EXPORT:!DES:!MD5:!PSK:!RC4";
ssl_session_cache shared:SSL:1m;


location @app {
log_not_found off;
access_log off;
proxy_pass https://127.0.0.1:8008;
proxy_http_version 1.1;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_cache_bypass $http_upgrade;
}

location / {
try_files $uri $uri/ @app;
}

}

在我的 Node.js 应用程序中,我添加了以下内容:

var server = https.createServer({
key: fs.readFileSync(path.join(__dirname, 'certs', 'server.pem')),
cert: fs.readFileSync(path.join(__dirname, 'certs', 'server.cer'))
}, app);


server = app.listen(8008,'localhost', function() {
console.log('Express server listening on port ' + server.address().port);
});

通过以下方式重新启动 nginx:sudo service nginx restart 并使用 pm2 启动应用程序:pm2 start app.js

我现在可以访问了

localhost:8008/serviceName

但是我无法访问

https://localhost:8008/serviceName.

Secure Connection Failed

The connection to localhost:8008 was interrupted while the page was loading.

The page you are trying to view cannot be shown because the authenticity of the received data could not be verified.
Please contact the website owners to inform them of this problem.

我想知道我的配置是否有问题,或者应用程序是否通过 nginx 路由。我错过了什么?

最佳答案

替换:

server = app.listen(8008,'localhost', function() {
console.log('Express server listening on port ' + server.address().port);
});

与:

server.listen(8008,'localhost', function() {
console.log('Express server listening on port ' + server.address().port);
});

解决了这个问题。

关于node.js - 使用 SSL 的 nodejs 应用程序的 Nginx 配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38444636/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com