【问题标题】:Setup NGINX reverse proxy with SSL?使用 SSL 设置 NGINX 反向代理?
【发布时间】:2020-05-05 19:12:53
【问题描述】:

我正在尝试使用 https 设置 Node.js Express 服务器,为此我正在使用 NGINX 反向代理。我的 VPS 运行的是 Ubuntu 18.04。我将/etc/nginx/sites-enabled/default 中的默认服务器配置更新为此,因此它可以与 SSL 一起使用(如果可以的话):

server {

    listen 80 default_server;
    listen [::]:80 default_server;

    listen 433 ssl default_server;
    listen [::]:433 ssl default_server;

    ssl_certificate /etc/nginx/fullchain.pem;
    ssl_certificate_key /etc/nginx/privkey.pem;

    root /var/www/html;

    index index.html index.htm index.nginx-debian.html;

    server_name mediaserver;

        location / {
            proxy_pass http://localhost:3000;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            proxy_set_header Host $host;
            proxy_cache_bypass $http_upgrade;
        }
}

我跑了sudo systemctl restart nginx 来重启NGINX。 但问题是,当我在没有 https 的情况下访问我的服务器的 IP 地址时,它可以正常打开,但是当我使用 https 访问我的服务器的 IP 地址时,它说无法访问该站点。任何建议的修复?谢谢。

【问题讨论】:

  • 你只是路由http流量proxy_pass http://localhost:3000;
  • 如何路由 https 流量?我可以使用 https 链接添加另一个代理通行证吗?
  • 另外,我应该对 http 和 https 使用 2 种配置,还是只使用 1 种配置,如代码所示?

标签: node.js nginx


【解决方案1】:

您需要将流量路由到 https 而不是 http。我还添加了 proxy_redirect 指令,它将任何不安全的请求路由到 https。

编辑:我看到的另一个问题是您正在侦听错误的端口。它应该是443,而不是433。请注意,我将 proxy_redirect 更改为使用端口 80。

server {

    listen 80 default_server;
    listen [::]:80 default_server;

    listen 443 ssl default_server;
    listen [::]:443 ssl default_server;

    ssl_certificate /etc/nginx/fullchain.pem;
    ssl_certificate_key /etc/nginx/privkey.pem;

    root /var/www/html;

    index index.html index.htm index.nginx-debian.html;

    server_name mediaserver;

        location / {
          proxy_pass https://localhost:3000;
          proxy_http_version 1.1;
          proxy_set_header Upgrade $http_upgrade;
          proxy_set_header Connection 'upgrade';
          proxy_set_header Host $host;
          proxy_cache_bypass $http_upgrade;
          proxy_redirect http://localhost:80 https://localhost:3000;
        }
}

这里是相关NGINX documentation的链接。

【讨论】:

  • 这使得 https 仍然无法正常工作,并且 http 也无法正常工作。有什么原因会发生这种情况?
  • 谢谢,现在我的连接不是私有的?为什么会这样?
  • 你需要自己做一些研究。 cloudflare.com/learning/ssl/connection-not-private-explained, This error is caused by an issue with the website's SSL certificate – it's missing, or it's expired, or it wasn't issued by a legitimate certificate authority, or the client can't access it for some other reason. 因此,如果您使用的是自签名证书,也可能会发生这种情况。
  • 你为什么首先使用端口 3000?
  • 我为什么不呢?
猜你喜欢
  • 2016-08-09
  • 2016-07-05
  • 1970-01-01
  • 2020-06-06
  • 2020-04-26
  • 2014-09-06
  • 2023-03-25
  • 1970-01-01
相关资源
最近更新 更多