【问题标题】:Cannot listen to https on port 5050 in NGINX无法在 NGINX 的 5050 端口上监听 https
【发布时间】:2021-06-01 23:51:42
【问题描述】:

我有一个 nodejs 应用程序,用作监听端口 5050 的网络服务器

我已经创建了证书并配置了 NGINX,它适用于对标准端口 (https://x.x/) 的正常 https 调用

如果我使用普通的 http://x.x:5050 调用来调用端口 5050,它也可以工作,但使用 https://x.x:5050/conf 调用我会得到:此站点无法提供安全连接

在 NGINX 配置文件下方: (网站名称已更改)

server {

        root /var/www/x.x/html;
        index index.html index.htm index.nginx-debian.html;

        server_name x.x www.x.x;

        location / {
                try_files $uri $uri/ =404;
        }

        location /conf {
               proxy_pass http://localhost:5050;
               try_files $uri $uri/ =404;
        }

        location /wh {
               proxy_pass http://localhost:5050;
               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;
        }

    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/x.x/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/x.x/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot


}

我在这里做错了什么?

【问题讨论】:

    标签: node.js nginx https digital-ocean


    【解决方案1】:

    您将 nginx 配置为充当反向代理,将来自 https://example.com/whatever 的传入请求转发到 http://localhost:5050/whatever。你说你做得对,它有效。好的。 (让那个工作是 xxx 脖子上的一个臭名昭著的痛苦。)

    你没有配置 nginx 监听 5050 端口。你也不应该;这是它用来将请求传递给你的 nodejs 程序的端口。您无法将 5050 端口的请求转发到 5050 端口。如果您尝试让 nodejs 和 nginx 都侦听 5050 端口,则其中一个会在您启动服务器时收到 EADRINUSE 错误。

    您的 nodejs 程序在端口 5050 上侦听 http 请求,而不是 https 请求。您不能轻易让它在同一端口上同时侦听 http 和 https。您的 nodejs 程序在 nginx 后面时,不应包含任何 https 服务器,仅包含 http。 (您正在让 nginx 进行艰苦的加密工作来处理 https,并让 nodejs 处理您的请求。)

    您也不希望您的 nodejs 程序直接侦听来自服务器外部的仅 http 请求。因为网络蠕变。

    如果您可以阻止从本地主机以外的任何地方访问端口 5050,您就可以宣布您的服务器配置任务成功。你can do this使用

    server.listen({
      host: 'localhost',
      port: 5050, ...
    });```
    

    在你的 nodejs 程序中。或者,您可以配置服务器的防火墙以阻止除 https(和 ssh,以便您管理它)以外的任何端口上的传入请求。 Digital Ocean 在这一点上有一个useful tutorial

    【讨论】:

    • 显然从 "https://x.x:5050/conf" 到 http:// 的转发不起作用。然而,对 http://x.x:5050/conf 的调用是有效的。此外,如果我正确理解了反向代理,那么我就不需要 nodejs 服务器来监听 https
    • 正确。将https://example.com:5050的请求转发到http://localhost:5050是不正确的。
    猜你喜欢
    • 1970-01-01
    • 2017-07-12
    • 2013-04-07
    • 1970-01-01
    • 1970-01-01
    • 2013-04-12
    • 2020-05-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多