【问题标题】:nginx proxy_pass not work with hosts filenginx proxy_pass 不适用于主机文件
【发布时间】:2019-04-02 12:53:13
【问题描述】:

我有这个 docker-compose 配置(它是我的 nginx 服务的一部分)

  nginx-service:
    image: nginx:latest
    container_name: nginx-service
    command: /bin/bash -c "exec nginx -g 'daemon off;'"
    volumes:
      - ./nginx/nginx.conf:/etc/nginx/nginx.conf
      - ./nginx/sites-enabled:/etc/nginx/sites-enabled
    restart: always
    networks:
      system-network:
        aliases:
          - nginx-service.docker
    extra_hosts:
      - serverIp:${CURRENT_SERVER_IP}
    ports:
      - 80:80
      - 443:443
      - 8081:8081

在此服务器中有一个 docker 容器,该容器不在 docker-compose 网络中,并在必要时自动创建。

我希望服务器的当前 ip 在 .env 文件中,为此我使用 extra_hosts 和这个 nginx 配置作为代理:

server {
    listen 0.0.0.0:80;
    server_name first.example.com;

    location / {
        proxy_pass http://serverIp:8082/;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

这真的很管用!因为这个容器总是启动并且有 8082 端口。

但是这个配置不起作用,我无法修复它。你能告诉我如何解决这个问题吗?

server {
    listen 0.0.0.0:80;
    server_name second.example.com;
    client_max_body_size 1024m;

    location / {
        set $status_proxy 0;

        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_http_version 1.1;
        # proxy_set_header Connection "";

        proxy_connect_timeout       960s;
        proxy_send_timeout          960s;
        proxy_read_timeout          960s;
        send_timeout                960s;

        if ($cookie_testingPort ~* "(60[\d]{3})") {
            proxy_pass http://serverIp:$cookie_testingPort;
            set $status_proxy 1;
        }

        if ($status_proxy = 0) {
            return 400;
        }
    }
}

当我从 second.example.com 重定向时 - 尽管我在 cookie testingPort 中看到 docker 容器的正确端口和容器确实在服务器中具有 CURRENT_SERVER_IP,但 nginx 返回我“502 Bad Gateway”,该 CURRENT_SERVER_IP 在内部的主机文件中指定带有 NGINX 的容器

【问题讨论】:

  • if ($cookie_testingPort ~* "(60[\d]{3})") 是什么意思或者你想用它达到什么目的?您是否使用端口连接到second.example.com?
  • second.example.com - 此 URL 应将用户重定向到在从 6000 开始的特定端口范围上创建的容器,然后在这种情况下遵循 regExp。为了连接到它,我想使用 cookie 中的端口和在 hosts 文件中重新定义的地址。如果我正确指定了地址,而没有在主机中重新定义它,那么这个条件和重定向可以正常工作。但我想了解如何使其与主机文件中重新定义的 URL 一起工作
  • 如果我理解正确,您的变量$cookie_testingPort 未定义。您在定义该变量的位置是否有额外的代码?此外,您正则表达式检查“60”+ 3 位数字,因此“6000”将不匹配。
  • 对不起 60000 端口,我的意思是,$cookie_testingPort - 在提升时,nginx 确实是未定义的,但是当它被重定向到这个 URL 时,它是从用户那里读取的,然后在检查之后验证它应该被发送到容器,但此时它不能再读取记录在文件 hosts 中的信息

标签: docker nginx docker-compose


【解决方案1】:

为什么它不起作用

您正在设置proxy_pass http://serverIp:$cookie_testingPort,这意味着它具有动态/可变值。当 nginx 具有 proxy_pass 的动态/变量值时,它将尝试在运行时(根据传入请求)在 dns 服务器的帮助下解析该域名,并将忽略 /etc/hosts。为了让“运行时解析”起作用,您需要在 nginx 配置中设置 resolver {{ip}},并且该 ip 应该安装了 dns 服务器,但是:

  • 您不能使用resolver 127.0.0.1,因为您没有在该映像中安装 dns 服务器
  • 你不能使用resolver 127.0.0.11(当前docker-compose的网络dns),因为它对serverIp一无所知

解决方案一:

在此服务器中有一个 docker 容器,该容器不在 docker-compose 网络中,并在必要时自动创建。

由于“外部容器”是一个 docker 容器,它属于某个网络。如果您设法将 nginx-service 容器连接到该“外部容器”的网络 - 您应该能够设置 resolver 127.0.0.11 并通过其服务名称调用“外部容器”。您可以将 docker-compose 之外的网络称为“外部”。这里是 one random example 和 documentation 。

解决方案 2:

可以使用模板生成/etc/nginx/sites-enabled/second.example.com.conf

# filename: ./second.example.com.template

server {
    listen 0.0.0.0:80;
    server_name second.example.com;

    location / {
        set $status_proxy 0;

        proxy_set_header   Host             $host;
        proxy_set_header   X-Real-IP        $remote_addr;
        proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;

        if ($cookie_testingPort ~* "(60[\d]{3})") {
            proxy_pass http://${CURRENT_SERVER_IP}:$cookie_testingPort;
            set $status_proxy 1;
        }

        if ($status_proxy = 0) {
            return 400;
        }
    }
}
# part of docker-compose.yml

  nginx-service:
    ...
    image: nginx
    volumes:
      - ./second.example.com.template:/etc/nginx/conf.d/second.example.com.template
    environment:
      - "CURRENT_SERVER_IP=${CURRENT_SERVER_IP}"
    ports:
      - 80:80
    command: /bin/bash -c "envsubst '$$CURRENT_SERVER_IP' < /etc/nginx/conf.d/second.example.com.template > /etc/nginx/conf.d/second.example.com.conf && exec nginx -g 'daemon off;'"

发生了什么:

  • 安装/etc/nginx/conf.d/second.example.com.template
  • 在容器内设置环境变量CURRENT_SERVER_IP
  • 初始化envsubst:
    • 读取/etc/nginx/conf.d/second.example.com.template
    • 将 $CURRENT_SERVER_IP 替换为所需的 ip
    • 将结果保存为/etc/nginx/conf.d/second.example.com.conf
  • nginx 启动

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-22
    • 2013-09-12
    • 1970-01-01
    • 2016-01-30
    • 1970-01-01
    • 2018-10-30
    • 1970-01-01
    • 2020-11-01
    相关资源
    最近更新 更多