【发布时间】: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 下的主液滴(不是容器)内,我有以下两个服务器块-default 和my-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