【问题标题】:502 Bad Gateway for NodeJS server managed by PM2 inside a lxc container502 Bad Gateway for NodeJS 服务器由 lxc 容器内的 PM2 管理
【发布时间】:2020-02-08 14:08:50
【问题描述】:

我有一个运行 Ubuntu 18.04 的数字海洋水滴,里面是一个 lxc 容器。我在那个容器中有两个应用程序。

第一个应用程序(客户端)位于/var/www/html,第二个是位于/var/www/my-site/ 的NodeJS 应用程序。容器内的 Node 应用程序由 pm2 管理,到目前为止一切似乎都运行良好,因为当我在容器终端输入 curl http://localhost:3000 时,我得到了所需的输出。

/etc/nginx/sites-available 下的主液滴(不是容器)内,我有以下两个服务器块-defaultmy-site

当我尝试通过我的域通过浏览器访问第一个应用程序时,它运行良好,但当我尝试通过sub.mydomain.com 访问它时,NodeJS 应用程序返回一个502 Bad Gateway。容器内的pm2 start 告诉我节点应用程序状态为online

这是我的default 服务器块文件。这行得通。当我访问 mydomain.com 时,我的网站显示正常。

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

server {
    # Enable HTTP/2
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name mydomain.com;
    # Use the Let’s Encrypt certificates
    ssl_certificate /etc/letsencrypt/live/mydomain.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/mydomain.com/privkey.pem;
    # Include the SSL configuration from cipherli.st
    include snippets/ssl-params.conf;

    root /var/www/html;

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

    location / {
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://container_ip_address /;
    }
}

现在这里是另一个服务器块 - my-site

# Upstream config
upstream site_upstream {
    server 127.0.0.1:3000;
    keepalive 64;
}

server {
    # Enable HTTP/2
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name sub.mydomain.com www.sub.mydomain.com;
    root /var/www/my-site;

    # Use the Let’s Encrypt certificates
    ssl_certificate /etc/letsencrypt/live/mydomain.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/mydomain.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-NginX-Proxy true;
        proxy_pass http://site_upstream;
        proxy_ssl_session_reuse off;
        proxy_set_header Host $http_host;
        proxy_cache_bypass $http_upgrade;
        proxy_redirect off;
    }
}

我在我的域的 DNS 设置中为我的子域设置了 A 记录,设置为我的 droplet 的 IP 地址,并且我还为 my-site 服务器块创建了一个到 /etc/nginx/sites-enabled 的符号链接。

我已经在互联网上搜索了解决此问题的方法,但似乎没有任何效果。我错过了什么?

您的帮助将不胜感激。谢谢。

【问题讨论】:

    标签: node.js nginx digital-ocean lxc


    【解决方案1】:

    这里的问题是对子域的请求没有被定向到 lxc 容器。 我通过在my-site 服务器块中添加以下内容解决了这个问题。

    location / {
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass http://container_ip/;
        }
    

    之后,我在下一个位置块中添加了一个星号。

    location /* {
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-NginX-Proxy true;
            proxy_pass http://site_upstream;
            proxy_ssl_session_reuse off;
            proxy_set_header Host $http_host;
            proxy_cache_bypass $http_upgrade;
            proxy_redirect off;
        }
    

    解决此问题的另一种方法是在默认服务器块的server_name 指令中包含子域。这行得通,但唯一的问题是,当你运行nginx -t 时,nginx 会抱怨它必须忽略我在my-site 服务器块中设置的服务器,否则,它工作得很好。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-26
      • 2017-01-06
      • 2022-07-14
      • 2021-09-11
      • 2018-01-20
      • 2017-05-10
      • 2021-03-29
      • 2019-03-14
      相关资源
      最近更新 更多