带 SSL 的 Nginx PM2 NodeJS 反向代理提供 HTTP 504

Nginx PM2 NodeJS reverse-proxy with SSL gives HTTP 504

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

我成功安装了 SSL 证书 让我们加密 并且所有子域都来自同一个域(假设 exemple.com ) 并且应该 重定向到 https.

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

现在,我正试图恢复我的 VPS 与三个旧的 NodeJs 应用程序的和谐,但它给了我 504 网关超时 来自 Nginx.

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

此配置在 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 没有给出任何错误。日志是干净的。这是我在检查请求时得到的。

套接字请求成功: (wss:// & https://)

其他人请求失败:

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

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

FF: 所以如果你路过这里你可能需要检查你的应用环境。例如:SGBD、R/W 权限、API...

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