user1685095
user1685095

Reputation: 6121

How to use nginx with docker as a reverse proxy

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.

Upvotes: 1

Views: 1427

Answers (2)

Marvin Velasquez
Marvin Velasquez

Reputation: 66

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 :)

Upvotes: 0

Kevin Cherepski
Kevin Cherepski

Reputation: 1483

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?

Upvotes: 2

Related Questions