Nginx 反向代理无法与容器通信

时间:2021-07-15 15:29:18

标签: docker nginx

我一直在尝试自学 Nginx。自然地,我想我应该使用 docker。我正在尝试使用 docker for windows 来做到这一点。最终将转移到 Linux 服务器。我觉得我很接近,但我在最后一个问题上被困住了。

reverseproxy_1  | 2021/07/14 22:37:31 [error] 31#31: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.18.0.1, server: , request: "GET /favicon.ico HTTP/1.1", upstream: "http://172.18.0.2:5000/favicon.ico", host: "localhost:4000", referrer: "http://localhost:4000/" 

大家有什么建议吗?我是新手,所以这可能是愚蠢的。我已经学习了几个教程,我真的觉得这应该行得通。

version: '3.7'
services:
        web:
            image: 'anatomy-lab2'
            container_name: 'AnatomyLabWeb'
            ports:
                - "5000:80"
            restart: always
        reverseproxy:
            image: nginx:alpine
            volumes:
                - ./nginx.conf:/etc/nginx/nginx.conf:ro
            ports:
                - '4000:4000'
            depends_on:
                - web
            restart: always

user nginx;

events {
    worker_connections 1000;
}
http {
    upstream web-api {
        server web:5000;
    }


    server {
        listen 4000;
        location / {
            proxy_pass http://web-api;
    }
  }
}
λ docker-compose up                                                                                                                                                                                                
Starting AnatomyLabWeb ... done                                                                                                                                                                                    
Starting anatomy-lab_reverseproxy_1 ... done                                                                                                                                                                       
Attaching to AnatomyLabWeb, anatomy-lab_reverseproxy_1                                                                                                                                                             
reverseproxy_1  | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration                                                                                                 
reverseproxy_1  | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/                                                                                                                        
reverseproxy_1  | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh                                                                                                            
reverseproxy_1  | 10-listen-on-ipv6-by-default.sh: info: IPv6 listen already enabled                                                                                                                               
reverseproxy_1  | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh                                                                                                                
reverseproxy_1  | /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh                                                                                                                
reverseproxy_1  | /docker-entrypoint.sh: Configuration complete; ready for start up                                                                                                                                
AnatomyLabWeb   | [04:56:26 WRN] Storing keys in a directory '/root/.aspnet/DataProtection-Keys' that may not be persisted outside of the container. Protected data will be unavailable when container is destroyed
.                                                                                                                                                                                                                  
AnatomyLabWeb   | [04:56:26 INF] User profile is available. Using '/root/.aspnet/DataProtection-Keys' as key repository; keys will not be encrypted at rest.                                                       
AnatomyLabWeb   | Hosting environment: Production                                                                                                                                                                  
AnatomyLabWeb   | Content root path: /app                                                                                                                                                                          
AnatomyLabWeb   | Now listening on: http://[::]:80                                                                                                                                                                 
AnatomyLabWeb   | Application started. Press Ctrl+C to shut down.                                                                                                                                                  
reverseproxy_1  | 2021/07/15 04:56:33 [error] 23#23: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.18.0.1, server: , request: "GET / HTTP/1.1", upstream: "http://172.18.
0.2:5000/", host: "localhost:4000"                                                                                                                                                                                 
reverseproxy_1  | 172.18.0.1 - - [15/Jul/2021:04:56:33 +0000] "GET / HTTP/1.1" 502 559 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"   
reverseproxy_1  | 2021/07/15 04:56:33 [error] 23#23: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.18.0.1, server: , request: "GET /favicon.ico HTTP/1.1", upstream: "htt
p://172.18.0.2:5000/favicon.ico", host: "localhost:4000", referrer: "http://localhost:4000/"                                                                                                                       
reverseproxy_1  | 172.18.0.1 - - [15/Jul/2021:04:56:33 +0000] "GET /favicon.ico HTTP/1.1" 502 559 "http://localhost:4000/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome
/91.0.4472.124 Safari/537.36"                                                                                                                                                                                      

我让 Web 应用程序自己运行得很好 (asp.net/kestrel)。但我似乎无法将其连接到 Nginx。

对此的任何想法都会很棒。我被困了很长时间。

1 个答案:

答案 0 :(得分:1)

问题来自

upstream web-api {
    server web:5000;
}

在 dockerized 环境中,web 容器侦听 :80,因此您需要像这样更改配置

upstream web-api {
    server web:80;
}