【问题标题】:Use Docker --net=host and connect to other containers by hostname使用 Docker --net=host 并通过主机名连接到其他容器
【发布时间】:2017-07-23 14:48:26
【问题描述】:

我想设置一个 Nginx 反向代理,它工作正常,但如果我设置 network_mode: "host" 它会停止工作,因为它无法找到其他 docker 容器的主机名。我有一个web 容器和一个nginx 容器。

我收到以下错误: reverseproxy_1 | nginx: [emerg] host not found in upstream "web:80" in /etc/nginx/nginx.conf:10

我的 Nginx 配置文件是:

worker_processes 1;

events { worker_connections 1024; }

http {

    sendfile on;

    upstream docker-web {
        server web:80;
    }

    server {
        listen 8080;

        location / {
            proxy_pass         http://docker-web;
            proxy_redirect     off;
            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_set_header   X-Forwarded-Host $server_name;
        }
    }
  }

我的 docker-compose.yml 文件是:

version: '2'
services:
  redis:
    image: "redis:alpine"
  web:
    depends_on:
      - redis
    build: .\app
    volumes:
      - .\app:/code
    restart: always
  reverseproxy:
    image: reverseproxy
    network_mode: "host"
    ports:
      - 8080:8080
    depends_on:
      - web

我需要将network_mode设置为host,否则X-Forwarded-For会出错。

【问题讨论】:

    标签: nginx docker docker-compose nginx-reverse-proxy


    【解决方案1】:

    我设法通过使用 Linux 主机而不是 Windows 来使其工作,这意味着我不需要使用 network_mode:"host"。我还必须将我的 Python 代码更改为

    request.environ.get('HTTP_X_REAL_IP', request.remote_addr)
    

    来自

    request.environ['REMOTE_ADDR']
    

    【讨论】:

    • 嗨,那里的答案完整吗?我也在尝试记录客户端 IP,但我在这里过得不好。
    • 老兄。碉堡了。我一直在努力获取 IP 地址,并且还在寻找主机网络解决方案。你用这个救了我的命!
    猜你喜欢
    • 1970-01-01
    • 2018-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-19
    • 2016-07-29
    • 2016-01-25
    相关资源
    最近更新 更多