Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] nginx反代失败502 Bad Gateway #1594

Closed
3 tasks done
WinterBokeh opened this issue May 18, 2023 · 7 comments
Closed
3 tasks done

[Bug] nginx反代失败502 Bad Gateway #1594

WinterBokeh opened this issue May 18, 2023 · 7 comments

Comments

@WinterBokeh
Copy link

WinterBokeh commented May 18, 2023

反馈须知

  • 我确认已经在 常见问题 中搜索了此次反馈的问题,没有找到解答;
  • 我确认已经在 Issues 列表(包括已经 Close 的)中搜索了此次反馈的问题,没有找到解答。
  • 我确认已经在 Vercel 使用教程 中搜索了此次反馈的问题,没有找到解答。

描述问题

本意是想实现二级域名访问本服务的功能, chat.xxx.xxx。
使用nginx对本项目进行反代,访问遇到502,查看nginx日志,报错如下:

[error] 76#76: *969 no live upstreams while connecting to upstream, client: xxx, server: xx.xx.xx, request: "GET / HTTP/1.1", upstream: "http://localhost/", host: "xx.xx.xx"
[error] 76#76: *972 connect() failed (111: Connection refused) while connecting to upstream, client: xxx, server: xx.xx.xx, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:3000/", host: "xx.xx.xx"

服务本身没问题,防火墙开放3000端口,绕过nginx,直接域名:3000可以正常访问。
nginx本身也没问题,一起反代的其他项目在正常使用。

如何复现

docker部署nginx和本项目,并开放了相关端口。
部署指令:

docker run -d --name chat -p 3000:3000 \
   -e OPENAI_API_KEY="sk-xxx" \
   -e CODE="xxx" \
   --net=host \
   -e PROXY_URL="http://127.0.0.1:7890" \
   yidadaa/chatgpt-next-web

经测试,即使去掉--net=host \仍然出现同样问题。

nginx配置文件:

#chat.conf
server {
    listen 80;
    server_name chat.xxx.xxx;

    location / {
        proxy_pass http://localhost:3000/;
        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_cache off;
        proxy_buffering off;
        chunked_transfer_encoding on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 300;
      }
}

#nginx.conf
http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    client_max_body_size 50M;
    client_body_buffer_size 50M;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

一些必要的信息

  • 系统:[linux]
  • 浏览器: [chrome, safari]
  • 版本: [435e82c]
  • 部署方式:[docker服务器部署]
  • nginx/1.23.4
@yinm0591
Copy link
Contributor

docker run 加上--network="host" 参数
nginx的proxy_pass地址改成 http://[::1]:3000/;

试试是否解决问题

@Yidadaa
Copy link
Collaborator

Yidadaa commented May 18, 2023

#1569

@WinterBokeh
Copy link
Author

WinterBokeh commented May 18, 2023

已解决。
参考#1569
把proxy_pass的localhost改成宿主机的局域网ip即可。

@lin-1259
Copy link

lin-1259 commented Jul 2, 2023

已解决。 参考#1569 把proxy_by的localhost改成宿主机的局域网ip即可。

老哥,proxy_by这个在哪修改,遇到同样的问题了

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically.


resolved. Refer to #1569 to change the localhost of proxy_by to the local area network ip of the host machine.

Brother, where to modify proxy_by, I have encountered the same problem

@WinterBokeh
Copy link
Author

WinterBokeh commented Jul 16, 2023

Bot detected the issue body's language is not English, translate it automatically.

resolved. Refer to #1569 to change the localhost of proxy_by to the local area network ip of the host machine.

Brother, where to modify proxy_by, I have encountered the same problem

Bot detected the issue body's language is not English, translate it automatically.

resolved. Refer to #1569 to change the localhost of proxy_by to the local area network ip of the host machine.

Brother, where to modify proxy_by, I have encountered the same problem

In nginx config file. My pen mistake is proxy_pass instead of proxy_by, and now my original reply has been corrected.
Here is my config file for your reference:

server {
    listen 80;
    server_name chat.xxx.xxx;

    access_log /root/chat_access.log;
    error_log /root/chat_error.log;

    location / {
        proxy_pass http://172.17.0.15:3000/;
        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_cache off;
        proxy_buffering off;
        chunked_transfer_encoding on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 300;
      }
}

172.17.0.15 is my IPv4 address for eth0.

@lin-1259
Copy link

机器人检测到问题正文的语言不是英语,自动翻译它。

解决了参考#1569将proxy_by的localhost修改为主机的局域网ip。

兄弟,在哪里修改proxy_by,我也遇到过同样的问题

机器人检测到问题正文的语言不是英语,自动翻译它。

解决了参考#1569将proxy_by的localhost修改为主机的局域网ip。

兄弟,在哪里修改proxy_by,我也遇到过同样的问题

在nginx配置文件中。我的笔误是proxy_pass而不是proxy_by,现在我原来的回复已经被纠正了。 下面是我的配置文件供您参考:

服务器{
    listen 80;
    服务器名称chat.xxx.xxx;

    access_log/root/chat_access.log;
    error_log/root/chat_error.log;

    位置/ {
        代理_通行证http://172.17.0.15:3000/;
        proxy_set_header主机$host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_cache off;
        proxy_buffering off;
        chunked_transfer_encoding on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 300
      }
}

172.17.0.15是eth0的IPv4地址。

谢谢老哥,解决问题啦

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants