nginx给出502坏网关时如何调试?

lf3rwulv  于 2023-03-01  发布在  Nginx
关注(0)|答案(1)|浏览(117)

landing.example.com:10000上有一个工作正常的Web服务器,它是一个Docker容器,暴露端口10000,IP是172.17.0.2
我想要的是在端口80上有一个nginx反向代理,并根据他们访问的URL将访问者发送到不同的Docker容器。

server {
    listen 80;
    server_name landing.example.com;

    location / {
        proxy_pass http://172.17.0.2:10000/;
    }

    access_log /landing-access.log;
    error_log  /landing-error.log info;
}

执行此操作时,我得到502 Bad Gateway,日志显示

2016/04/14 16:58:16 [error] 413#413: *84 connect()
failed (111: Connection refused) while connecting to upstream, client:
xxx.xxx.xxx.xxx, server: landing.example.com, request: "GET / HTTP/1.1",
upstream: "http://172.17.0.2:10000/", host: "landing.example.com"
inb24sb2

inb24sb21#

服务器没有应答,因为它没有被定义为上游。
试试这个:

upstream my_server {
   server 172.17.0.2:10000;
}

server {
   listen 80;
   server_name landing.example.com;
   location / {
      proxy_pass                  http://my_server;
      proxy_set_header            Host $host;
      proxy_set_header            X-Real-IP $remote_addr;
      proxy_http_version          1.1;
      proxy_set_header            X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header            X-Forwarded-Proto http;
      proxy_redirect              http:// $scheme://;
   }
}

在这里,您定义了上游服务器(通过IP或主机名定义您的服务器),并确保也转发报头,以便应答服务器知道该向谁应答。

相关问题