我正在部署我的项目,我没有遇到任何问题,直到我决定为我的数字海洋 droplet 购买一个域名,我将 namecheap 域名中的记录添加到我的 droplet 中,我正在处理它,然后突然当我想要访问 myproject/api 时它现在显示502 错误 我会给你所有的信息,这样你也许可以帮助我
1 ) mongoDB atlas 状态:All good
;我甚至尝试了“ npm start ”来查看 DB 是否无法连接,但它连接得很好。我在 .env 中刷新了连接字符串,它仍然可以连接,没有问题。
2)Nginx error.log 显示这些信息我不太明白,我猜这是关键点:
86 connect() failed (111: Connection refused) while connecting to upstream, client: 164.92.206.160, server: _, request: "GET /api/category/cinema HTTP/1.1", upstream: "http://[::1]:8000/api
3)前端完美地显示在我的项目域名网址上,它开始显示502 错误当我使用我的数据库/服务器单击某些东西时。
4)我多次重建并重新启动了前端和后端 + nginx
5)我更新了端点和.env变量以匹配我的新域名,并且它可以正常工作,只有API方面出现问题
6)nginx配置文件:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
server_name _;
location /api {
proxy_pass http://localhost:8000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
location / {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
我几乎尝试了所有方法,但它仍然无法像以前那样工作。
答案1
正如链接线程在评论中指出的那样,您的上游服务器可能没有监听http://[::1]:8000
。
请注意,[::1]
是 IPv6 环回地址(127.0.0.1
与 IPv4 类似),因此如果上游仅为 IPv4,则应127.0.0.1
在配置文件中明确写入,而不是localhost
。