欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

docker ngnix容器代理本地服务:*1 connect() failed (111: Connection refused) while connecting to upstream

程序员文章站 2022-07-15 12:55:30
...
http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;

    server {
        listen       443 ssl;

        server_name  efuser.test.top;

        ssl_certificate /ssl/grr_ssl/Nginx/1_www.test.top_bundle.crt;
        ssl_certificate_key /ssl/grr_ssl/Nginx/2_www.test.top.key;

        ssl_session_timeout 5m;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
        ssl_prefer_server_ciphers on;

        location /  {
                    root /usr/share/nginx/html;
                    index index.html;
                    
        }
        location ^~/api/  {
                  proxy_pass http://127.0.0.1:3001/;          
                  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;
        }

    } 
}

重启docker ,nignx日志一直报错:

2020/10/21 06:49:55 [error] 21#21: *4 connect() failed (111: Connection refused) while connecting to upstream, client: 172.17.0.1, server: efuser.guorongrong.top, request: "GET /api/ HTTP/1.1", upstream: "http://127.0.0.1:3001/", host: ...

解决方案1:127.0.0.1指向的是 172.17.0.1 这个容器他 本身,指向的并不是服务器地址;proxy_pass http://127.0.0.1:3001/; 改成proxy_pass http://192.168.9.97:3001/; 解决问题。
解决方案2:--network host桥接模式

docker run \
  -p 80:80 \
  -p 443:443 \
  --name nginx-web \
  --network host \
  -v /home/nginx/www:/usr/share/nginx/html \
  -v /home/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \
  -v /home/nginx/logs:/var/log/nginx \
  -v /mnt/data:/mnt/data \
  -v /home/nginx/ssl:/ssl/ \
  -d nginx
  

相关标签: docker nginx