简体   繁体   English

使用nginx作为反向代理时,连接到上游时连接被拒绝

[英]Connection refused while connecting to upstream when using nginx as reverse proxy

The setup is as follows: I have a Gunicorn/Django app running on 0.0.0.0:8000 that is accessible via the browser. 设置如下:我有一个运行在0.0.0.0:8000上的Gunicorn / Django应用程序,可以通过浏览器访问。 To serve static files I am running nginx as a reverse proxy. 为了提供静态文件,我将nginx作为反向代理运行。 /etc/nginx/nginx.conf is configured to forward requests as follows: /etc/nginx/nginx.conf配置为转发请求,如下所示:

server {
    location /static/ {
        alias /data/www/;
    }


    # Proxying the connections
    location / {
        proxy_set_header   Host $host;
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;

        proxy_redirect     off;
        proxy_pass         http://0.0.0.0:8000;
    }
}

and my docker-compose.yml file is as follows: 和我docker-compose.yml文件如下:

version: '3.3'

services:
  web:
    restart: always
    build: ./web
    expose:
      - "8000"
    ports:
      - "8000:8000"
    volumes:
      - staticdata:/usr/src/app/static_files
    command: gunicorn wsgi:application --workers 2 --bind 0.0.0.0:8000
    depends_on:
      - postgres

  nginx:
    restart: always
    build: ./nginx
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - staticdata:/data/www
    depends_on:
      - web

  postgres:
    image: postgres:9.2
    restart: always
    volumes:
      - pgdata:/var/lib/postgresql/data
    ports:
      - "5432:5432"

volumes:
  staticdata:
  pgdata:

When I visit 0.0.0.0:8000 via the browser the application works fine (albeit without serving static files), but when I visit 127.0.0.1:80 I get the following error: 当我通过浏览器访问0.0.0.0:8000 ,应用程序工作正常(尽管没有提供静态文件),但是当我访问127.0.0.1:80我收到以下错误:

nginx_1     | 2017/09/17 13:59:46 [error] 6#6: *5 connect() failed (111: Connection refused) while connecting to upstream, client: 172.18.0.1, server: , request: "GET / HTTP/1.1", upstream: "http://0.0.0.0:8000/", host: "127.0.0.1"

I know that this error indicates that the server running on 0.0.0.0:8000 is not accepting requests, but since I can visit it via the browser I am a bit confused. 我知道这个错误表明在0.0.0.0:8000上运行的服务器不接受请求,但由于我可以通过浏览器访问它,我有点困惑。

Thank you in advance. 先感谢您。

Change your proxy_pass from 从中更改您的proxy_pass

proxy_pass         http://0.0.0.0:8000;

to

proxy_pass         http://web:8000;

Your nginx needs to forward to request the web container 您的nginx需要转发才能请求Web容器

Edit-1: Explanation 编辑-1:解释

0.0.0.0 is a special IP address which is used to refer to any available interface on the machine. 0.0.0.0是一个特殊的IP地址,用于指代机器上的任何可用接口。 So if your machine has a loopback (lo), ethernet (eth0), Wifi (wlan0) with respective IP as 127.0.0.1 , 192.168.0.100 , 10.0.0.100 . 所以,如果你的机器有一个回环(LO),以太网(eth0的),无线上网(为wlan0)与相应的IP为127.0.0.1192.168.0.10010.0.0.100

So now while listening for incoming connection you can choose any of the above IP 因此,现在在侦听传入连接时,您可以选择上述任何IP

gunicorn wsgi:application --workers 2 --bind 10.0.0.100:8000

This will only be reachable from your Wifi network. 这只能通过您的Wifi网络访问。 But other machines on Lan network can't visit it. 但Lan网络上的其他机器无法访问它。 So if you want your app to listen on any available network on the machine you use a special IP 0.0.0.0 . 因此,如果您希望应用程序在计算机上的任何可用网络上进行侦听,请使用特殊的IP 0.0.0.0 This means bind on all network interfaces 这意味着在所有网络接口上绑定

gunicorn wsgi:application --workers 2 --bind 0.0.0.0:8000

Now when you access the app using http://0.0.0.0 it is equivalent to using 127.0.0.1 . 现在,当您使用http://0.0.0.0访问应用程序时,它相当于使用127.0.0.1 So your proxy_pass http://0.0.0.0:8000; 所以你的proxy_pass http://0.0.0.0:8000; is equivalent to proxy_pass http://127.0.0.1:8000; 相当于proxy_pass http://127.0.0.1:8000;

So when you run that in nginx container, it passes on the request on port 8000 of the same container and there is nothing running on 8000 in your nginx container. 因此,当您在nginx容器中运行它时,它会在同一容器的端口8000上传递请求,并且在您的nginx容器中没有任何内容在8000上运行。 So you need to send that request to the your gunicorn container. 因此,您需要将该请求发送到您的gunicorn容器。 This is reachable using the service name web in docker-compose . 这可以使用docker-compose的服务名称web访问。

See the below article for more details https://docs.docker.com/compose/networking/ 有关详细信息,请参阅以下文章https://docs.docker.com/compose/networking/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Docker Nginx反向代理返回502错误的网关“连接到上游时连接被拒绝” - Docker nginx reverse proxy returns 502 bad gateway “connection refused while connecting to upstream” 连接到上游时 docker nginx 连接被拒绝 - docker nginx connection refused while connecting to upstream Docker 上的 Nginx(连接到上游时拒绝连接) - Nginx on Docker (Connection Refused while connecting to upstream) docker nginx 代理 nginx connect() 在连接到上游时失败(111:连接被拒绝) - docker nginx proxy nginx connect() failed (111: Connection refused) while connecting to upstream Nginx+PHP-FPM:连接上游时连接被拒绝 (502) - Nginx+PHP-FPM: connection refused while connecting to upstream (502) 连接到上游时,nginx&nodejs:connect()失败(111:连接被拒绝) - nginx & nodejs: connect() failed (111: Connection refused) while connecting to upstream 为什么我的连接在连接到上游时被拒绝? Nginx 错误502 - Why is my connection refused while connecting to upstream? Nginx Error 502 Nginx:(111:连接被拒绝)同时连接到上游 wordpress 和 docker - Nginx: (111: Connection refused) while connecting to upstream wordpress & docker nginx 连接()失败(111:连接被拒绝)同时连接到上游 [Mastodon] - nginx connect() failed (111: Connection refused) while connecting to upstream [Mastodon] nginx反向代理上游在docker-compose中失败并显示连接被拒绝的消息 - nginx reverse proxy upstream fails in docker-compose with connection refused message
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM