簡體   English   中英

在 lxc 容器內由 PM2 管理的 NodeJS 服務器的 502 錯誤網關

[英]502 Bad Gateway for NodeJS server managed by PM2 inside a lxc container

我有一個運行 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訪問它時, sub.mydomain.com應用程序返回一個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的符號鏈接。

我已經在互聯網上搜索了解決此問題的方法,但似乎沒有任何效果。 我錯過了什么?

您的幫助將不勝感激。 謝謝。

這里的問題是對子域的請求沒有被定向到 lxc 容器。 我通過在my-site server 塊中添加以下內容解決了這個問題。

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 會抱怨說,當您運行nginx -t時,它必須忽略我在my-site服務器塊中設置的服務器,否則,它工作得很好。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM