【问题标题】:Nginx PM2 NodeJS reverse-proxy with SSL gives HTTP 504带有 SSL 的 Nginx PM2 NodeJS 反向代理提供 HTTP 504
【发布时间】:2019-05-12 18:07:25
【问题描述】:

曾几何时,我为三个 NodeJssocket.io API 在同一个 Ubuntu 16.4 LTS VPS 上运行了一个很好的配置> 服务器带有 PM2 用于进程管理和 Nginx 用于反向代理到三个不同的子域。

我成功安装了来自 Let's encryptSSL 证书,并且所有子域都来自同一个域(比如说 exemple.com)并且应该重定向到 https

一旦我尝试为非 NodeJs 应用程序 (PHP/laravel) 添加第四个子域,反向代理不再通过,不幸的是我没有旧 Nginx 配置的备份。

现在,我正试图恢复我的 VPS 与三个旧 NodeJs 应用程序的和谐,但它给了我来自 Nginx504 Gateway Time-out

这是我的配置,我认为是相同的旧配置:

此配置在 chrome 上运行良好,但我正在尝试从移动和桌面应用程序访问我的 API。

  # HTTP — redirect all traffic to HTTPS
  server {
    listen 80;
    listen [::]:80 default_server ipv6only=on;
    return 301 https://$host$request_uri;
  }

  # App1 from port 3000 to sub1.exemple.com
  server {
    # Enable HTTP/2
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name sub1.exemple.com;

    # Use the Let’s Encrypt certificates
    ssl_certificate
    /etc/letsencrypt/live/sub1.exemple.com/fullchain.pem;
    ssl_certificate_key
    /etc/letsencrypt/live/sub1.exemple.com/privkey.pem;

    # Include the SSL configuration from cipherli.st
    include snippets/ssl-params.conf;

    location / {
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header X-Forwarded-Proto $scheme;
      proxy_set_header X-NginX-Proxy true;
      proxy_ssl_session_reuse off;
      proxy_set_header Host $http_host;
      proxy_cache_bypass $http_upgrade;

      proxy_pass http://localhost:3000/;
      proxy_redirect off;

      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection "upgrade";
  }
  }

  # App2 from port 4000 to sub2.exemple.com
  server {
    # Enable HTTP/2
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name sub2.exemple.com;

    # Use the Let’s Encrypt certificates
    ssl_certificate
    /etc/letsencrypt/live/sub2.exemple.com/fullchain.pem;
    ssl_certificate_key
    /etc/letsencrypt/live/sub2.exemple.com/privkey.pem;

    # Include the SSL configuration from cipherli.st
    include snippets/ssl-params.conf;

    location / {
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header X-Forwarded-Proto $scheme;
      proxy_set_header X-NginX-Proxy true;
      proxy_ssl_session_reuse off;
      proxy_set_header Host $http_host;
      proxy_cache_bypass $http_upgrade;

      proxy_pass http://localhost:4000/;
      proxy_redirect off;

      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection "upgrade";
    }
  }

  # App2 from port 5000 to sub3.exemple.com
  server {
    # Enable HTTP/2
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name sub3.exemple.com;

    # Use the Let’s Encrypt certificates
    ssl_certificate
    /etc/letsencrypt/live/sub3.exemple.com/fullchain.pem;
    ssl_certificate_key
    /etc/letsencrypt/live/sub3.exemple.com/privkey.pem;

    # Include the SSL configuration from cipherli.st
    include snippets/ssl-params.conf;

    location / {
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header X-Forwarded-Proto $scheme;
      proxy_set_header X-NginX-Proxy true;
      proxy_ssl_session_reuse off;
      proxy_set_header Host $http_host;
      proxy_cache_bypass $http_upgrade;

      proxy_pass http://localhost:5000/;
      proxy_redirect off;

      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection "upgrade";
    }
  }

更新以获取更多信息。

Nginx、NodeJs 和 PM2 不会给出任何错误。日志很干净。这是我在检查请求时得到的结果。

socket 请求成功:(wss:// & https://)

别人请求时失败:

我还想指出,每个子系统都安装了 SSL,应用程序稳定且在本地服务器上运行没有任何问题。

【问题讨论】:

    标签: node.js ssl nginx socket.io pm2


    【解决方案1】:

    我发现问题不在 不是 Nginx不是 PM2不是 Nodejs 也不是在 SSL,这一切都在我部署的应用程序中。 Mongodb 进程的一个问题导致他不自动启动。因此,应用程序接受第一个请求,因为它不需要数据库干预,并在超时后拒绝登录请求,因为应用程序已经崩溃但 PM2 重新启动它并且 Nginx 保持子域对请求开放。

    FF:因此,如果您经过这里,您可能需要检查您的应用环境。例如:SGBD、R/W 权限、API...

    希望这可以帮助任何有类似问题的人。

    【讨论】:

      猜你喜欢
      • 2021-05-26
      • 2017-09-27
      • 2016-03-31
      • 1970-01-01
      • 2023-03-04
      • 2020-06-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多