Docker nginx proxy_pass不能与主机文件一起使用

Docker nginx proxy_pass不能与主机文件一起使用,docker,nginx,docker-compose,Docker,Nginx,Docker Compose,我有这个docker compose配置(它是我的nginx服务的一部分) 在此服务器中有一个docker容器,它位于docker compose网络之外,并在必要时自动创建 我希望服务器的当前ip位于.env文件中,为此,我使用额外的\u主机和代理的nginx配置: server { listen 0.0.0.0:80; server_name first.example.com; location / { proxy_pass http://serv

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

在此服务器中有一个docker容器,它位于docker compose网络之外,并在必要时自动创建

我希望服务器的当前ip位于.env文件中,为此,我使用额外的\u主机和代理的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重定向时,nginx返回我“502坏网关”,尽管我在cookie testingPort中看到docker容器的正确端口,并且容器在服务器中确实具有当前的_server_IP,这是在nginx容器内部的hosts文件中指定的,为什么它不工作 您正在设置
proxy\u passhttp://serverIp:$cookie_testingPort
,这意味着它有一个动态/可变值。当nginx具有
代理传递的动态/变量值时,它将在运行时(根据传入请求)在dns服务器的帮助下尝试解析该域名,并将忽略
/etc/hosts
。为了使“运行时解析”工作,您需要在nginx config中设置
解析程序{{{ip}}
,并且该ip应该安装dns服务器,但是:

  • 您不能使用
    解析器127.0.0.1
    ,因为您没有在该映像中安装dns服务器
  • 您不能使用
    resolver 127.0.0.11
    (当前docker compose的网络dns),因为它对
    serverIp
解决方案1: 在此服务器中有一个docker容器,它位于docker compose网络之外,并在必要时自动创建

因为“外部容器”是docker容器,所以它属于某个网络。如果您设法将
nginx服务
容器连接到该“外部容器”的网络,您应该能够设置
resolver 127.0.0.11
,并通过其服务名称调用“外部容器”。您可以将docker compose之外的网络称为“外部”。下面是关于如何做到这一点的

解决方案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;
        }
    }
}
#docker-compose.yml的一部分
nginx服务:
...
图片:nginx
卷数:
-./second.example.com.template:/etc/nginx/conf.d/second.example.com.template
环境:
-“当前服务器IP=${CURRENT服务器IP}”
端口:
- 80:80
命令:/bin/bash-c“envsubt'$$CURRENT_SERVER_IP'/etc/nginx/conf.d/second.example.com.conf&&exec nginx-g‘daemon off;’
发生的情况是:

  • mount
    /etc/nginx/conf.d/second.example.com.template
  • 在容器内设置环境变量
    当前服务器\u IP
  • 初始化时
    envsubst
    • 读取
      /etc/nginx/conf.d/second.example.com.template
    • 用所需的IP替换$CURRENT_SERVER_IP
    • 将结果保存为
      /etc/nginx/conf.d/second.example.com.conf
  • nginx启动

如果($cookie\u testingPort~*“(60[\d]{3})”)
的含义是什么,或者您想用它实现什么?您是否使用端口连接到
second.example.com
?second.example.com-此URL应将用户重定向到一个容器,该容器是在6000个特定范围的端口上创建的,在此情况下遵循regExp。要连接到它,我想使用cookie中的端口和地址,地址在hosts文件中重新定义。如果我正确地指定了地址,而没有在主机中重新定义它,那么这个条件和重定向将正常工作。但是我想了解如何使其与主机文件中重新定义的URL一起工作如果我理解正确,您的变量
$cookie\u testingPort
未定义。在定义变量的地方有额外的代码吗?另外,您的正则表达式检查“60”+3位,因此“6000”将不匹配。对不起,60000端口,我的意思是$cookie\u testingPort-在提升时,nginx实际上是未定义的,但是,当它被重定向到此URL时,会从用户的cookie中读取它,然后在检查验证后,它应该被发送到容器,但此时它无法再读取文件主机中记录的信息
# 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;'"