How to use nginx with docker as a reverse proxy

倾然丶 夕夏残阳落幕 提交于 2020-01-14 02:52:24

问题


I have a django app and running it with gunicorn on localhost:8000 I have configs for nginx to use it as a reverse proxy.

    upstream django {
 #  fail_timeout=0 means we always retry an upstream even if it failed
 #  to return a good HTTP response (in case the Unicorn master nukes a
 #  single worker for timing out).

  server localhost:8000 fail_timeout=0;
}

I know how to expose 80 port and run nginx in container, but I don't understand how to connect gunicorn running on localhost and nginx in container.


回答1:


You'll need to utilize the IP of the bridge created by docker. There is a good article on Docker explaining this: https://docs.docker.com/v1.6/articles/networking/

When Docker starts, it creates a virtual interface named docker0 on the host machine. It randomly chooses an address and subnet from the private range defined by RFC 1918 that are not in use on the host machine, and assigns it to docker0.

If we take a look at the IP address assigned to docker0 (sudo ip addr show docker0) we could use this as the IP address to communicate with the host from within a docker container.

    upstream django {
 #  fail_timeout=0 means we always retry an upstream even if it failed
 #  to return a good HTTP response (in case the Unicorn master nukes a
 #  single worker for timing out).

  server IP_OF_DOCKER0:8000 fail_timeout=0;
}

I haven't tested the above but I believe it should work. If not you may need to bind gunicorn to the docker0 IP as well.

This answer has some good insight into this process as well... From inside of a Docker container, how do I connect to the localhost of the machine?




回答2:


A better approach might be "dokerize" the django application too, build a network between the dokerized nginx and the dockerized django application then expose the http port from the dockerized nginx to all interfaces.

Here is a good post about this, maybe you can take some hints from it :)



来源:https://stackoverflow.com/questions/27421792/how-to-use-nginx-with-docker-as-a-reverse-proxy

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