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

落花浮王杯 提交于 2019-12-03 18:14:25

Change your proxy_pass from

proxy_pass         http://0.0.0.0:8000;

to

proxy_pass         http://web:8000;

Your nginx needs to forward to request the web container

Edit-1: Explanation

0.0.0.0 is a special IP address which is used to refer to any available interface on the machine. 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.

So now while listening for incoming connection you can choose any of the above IP

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

This will only be reachable from your Wifi network. But other machines on Lan network can't visit it. So if you want your app to listen on any available network on the machine you use a special 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. So your proxy_pass http://0.0.0.0:8000; is equivalent to 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. So you need to send that request to the your gunicorn container. This is reachable using the service name web in docker-compose.

See the below article for more details https://docs.docker.com/compose/networking/

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!