几天来,我拥有一台服务器,并在该服务器上成功运行了一些用 NodeJS 开发的 Web 应用程序。
一切正常,直到浏览器突然开始显示错误 502 错误网关 nginx/1.10.3尝试访问网站时。我没有进行任何可能导致此类错误的更改。
在网上查找资料,似乎这个错误与 Nginx 将请求定向到我的应用程序的端口的方式有关。我检查了我的 /etc/nginx/sites-available/default 中的配置,一切似乎都正确。这是我的配置的摘录:
# --------------------------
# WEBSITE 1 - www.mywebsite.com
# --------------------------
server {
listen 80;
if ($host = mywebsite.com) {
return 301 https://www.mywebsite.com$request_uri;
}
if ($host = www.mywebsite.com) {
return 301 https://www.mywebsite.com$request_uri;
}
server_name www.mywebsite.com mywebsite.com;
location /{
proxy_pass "http://127.0.0.1:3000";
}
}
server {
listen 443 ssl;
if ($host = mywebsite.com) {
return 301 https://www.mywebsite.com$request_uri;
}
server_name www.mywebsite.com mywebsite.com;
location /{
proxy_pass "http://127.0.0.1:3000";
}
# LetsEncrypt Certificates
ssl_certificate /etc/letsencrypt/live/mywebsite.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/mywebsite.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
# --------------------------
# WEBSITE 2 - www.mywebsite2.com
# --------------------------
server {
listen 80;
if ($host = mywebsite2.com) {
return 301 https://www.mywebsite2.com$request_uri;
}
if ($host = www.mywebsite2.com) {
return 301 https://www.mywebsite2.com$request_uri;
}
server_name www.mywebsite2.com mywebsite2.com;
location /{
proxy_pass "http://127.0.0.1:3001";
}
}
server {
listen 443 ssl;
if ($host = mywebsite2.com) {
return 301 https://www.mywebsite2.com$request_uri;
}
server_name www.mywebsite2.com mywebsite2.com;
location /{
proxy_pass "http://127.0.0.1:3001";
}
# [...] More LetsEncrypt Certificates, and more websites [...]
}
另外,我查看了 nginx错误日志文件,我可以看到自从发生此错误以来,每次访问该网站时都会写入此行:
[error] connect() failed (111: Connection refused) while connecting to upstream, client: XXX.XXX.XXX.XXX, server: www.mywebsite.com, request: "GET /aCustomUrl HTTP/1.1", upstream: "http://127.0.0.1:3000/aCustomUrl", host: "www.mywebsite.com"
您知道会发生什么吗?配置对我来说似乎没问题,而且几天前它就成功运行了。我尝试重新启动服务器,但没有帮助...
谢谢大家。
答案1
您的应用是否可直接通过http://127.0.0.1:3000?
尝试:
curl http://127.0.0.1:3000
它有回应嗎?
如果不是,502 Bad Gateway 错误可能意味着您的 nodejs 进程正在崩溃。也许是需要重建的包。比如 sqlite3。您最近升级过 nodejs 吗?