【问题标题】:Docker Nginx Hosts Cannot Be ResolvedDocker Nginx 主机无法解析
【发布时间】:2019-12-24 16:45:39
【问题描述】:

我正在尝试连接到 docker 中的 proxypass 我已经设置但我不断收到:

8#8: *1 api.example.local could not be resolved (3: Host not found)

我可以通过在浏览器中转到http://api.example.com 来访问代理,但如果我通过 nginx 代理通行证则不能。我的nginx如下,请注意:

  • 禁用 ipv6
  • 我正在解析为 127.0.0.11

    server {
        listen   80;
        server_name  api.example.local;
        resolver 127.0.0.11 ipv6=off;
    
    location / {
        root    /code/api/public_html/;
        index  index.php index.html index.htm;
        try_files $uri $uri/ /index.php?rt=$uri&$args;
    }
    
    
    location ~ \.php$ {
        root    /code/api/public_html/;
        fastcgi_pass php:9000;
        fastcgi_index  index.php;
        #fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
        fastcgi_param  PATH_INFO        $fastcgi_path_info;
        fastcgi_param  ENV  development;
        fastcgi_param HTTPS off;
        fastcgi_read_timeout 9600;
    }
    

    }

    server {
        index index.php index.html;
        server_name www.example.local;
        resolver 127.0.0.11 ipv6=off;
        error_log  /var/log/nginx/error.log;
        access_log /var/log/nginx/access.log;
        root /code/main/public_html;
    
    location / {
        root    /code/main/public_html/;
        index  index.php index.html index.htm;
        try_files $uri $uri/ /index.php?rt=$uri&$args;
    }
    
    location /api {
        resolver 127.0.0.11 ipv6=off;
        proxy_pass_header  Set-Cookie;
        proxy_pass_header  P3P;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Fowarded-Host $host;
    
    
    
    set $upstream api.example.local;
    
        proxy_pass http://$upstream;
        proxy_connect_timeout 60;
        proxy_redirect     off;
    }
    
    location ~ \.php$ {
        root    /code/main/public_html/;
        fastcgi_pass php:9000;
        fastcgi_index  index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
        fastcgi_param  PATH_INFO        $fastcgi_path_info;
        fastcgi_param  ENV      development;
        fastcgi_param HTTPS off;
        fastcgi_read_timeout 300;
    }
    

    }

我的 /etc/hosts 中也有域。操作系统是 Ubuntu 18.04。

127.0.0.1       localhost
127.0.1.1       my-comp

127.0.0.1       www.example.local
127.0.0.1       api.example.local

我做错了什么?

【问题讨论】:

  • api.example.docker 是否解析为主机以外的其他内容?
  • 不,它没有。我也在上面添加了我的 /etc/hosts 。 @7_R3X
  • 127.0.0.11 位于主机网络中,因此 nginx 容器无法访问它。 Docker 创建了自己的网络(对我来说是 172.17.0.0/16 ),因此没有容器会识别超出此范围的主机。

标签: docker ubuntu nginx nginx-reverse-proxy


【解决方案1】:

nginx 容器和上游服务器需要在同一个 docker 网络上。

示例: 假设 nginx 容器在网桥上暴露了 443 端口,创建一个名为 nginx-network 的 docker 网络并将其加入该网络。 假设上游服务器是一个名为“mysite”的容器,将该容器加入 nginx 网络,然后 nginx 将能够解析 dns“mysite”。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-21
    • 2018-10-23
    • 1970-01-01
    • 1970-01-01
    • 2018-02-13
    • 2019-07-24
    • 2016-12-30
    相关资源
    最近更新 更多