【问题标题】:Nginx configuration for a nodejs app with SSL带有 SSL 的 nodejs 应用程序的 Nginx 配置
【发布时间】:2016-07-18 19:34:14
【问题描述】:

我有一个节点应用程序在运行

localhost:8008/serviceName

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

我创建了一个名为 service.conf 的文件并将其符号链接到 /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);
});

通过 sudo service nginx restart 重新启动 nginx 并使用 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 路由。 我错过了什么?

【问题讨论】:

  • 您已经创建了 https 服务器,在下一行中您将其丢弃并创建默认 http 服务器 (app.listen(...);)
  • 另外,当你转到localhost:8008 时,你完全绕过了 nginx。如果你想使用 nginx,你的应用不需要使用 HTTPS

标签: node.js ssl nginx


【解决方案1】:

替换:

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);
});

修复了问题。

【讨论】:

    猜你喜欢
    • 2016-11-16
    • 2017-01-17
    • 2015-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-17
    • 2018-01-28
    相关资源
    最近更新 更多