landing.example.com:10000上,我有一个运行良好的Web服务器,这是一个公开端口10000的Docker容器。它的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"

最佳答案

尝试这个:

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或主机名)
并确保也转发 header ,以便服务器回答知道要回答的人。

10-01 05:12