Public | Automated Build

Last pushed: 7 days ago
Short Description
Automated Nginx reverse proxy for docker containers
Full Description

nginx-proxy sets up a container running nginx and docker-gen. docker-gen generates reverse proxy configs for nginx and reloads nginx when containers are started and stopped.

See Automated Nginx Reverse Proxy for Docker for why you might want to use this.


To run it:

$ docker run -d -p 80:80 -v /var/run/docker.sock:/tmp/docker.sock:ro jwilder/nginx-proxy

Then start any containers you want proxied with an env var

$ docker run -e  ...

The containers being proxied must expose the port to be proxied, either by using the EXPOSE directive in their Dockerfile or by using the --expose flag to docker run or docker create.

Provided your DNS is setup to forward to the a host running nginx-proxy, the request will be routed to a container with the VIRTUAL_HOST env var set.

Image variants

The nginx-proxy images are available in two flavors.


This image uses the debian:jessie based nginx image.

$ docker pull jwilder/nginx-proxy:latest


This image is based on the nginx:alpine image. Use this image to fully support HTTP/2 (including ALPN required by recent Chrome versions). A valid certificate is required as well (see eg. below "SSL Support using letsencrypt" for more info).

$ docker pull jwilder/nginx-proxy:alpine

Docker Compose

version: '2'
    image: jwilder/nginx-proxy
    container_name: nginx-proxy
      - "80:80"
      - /var/run/docker.sock:/tmp/docker.sock:ro

    image: jwilder/whoami
    container_name: whoami
      - VIRTUAL_HOST=whoami.local
$ docker-compose up
$ curl -H "Host: whoami.local" localhost
I'm 5b129ab83266

IPv6 support

You can activate the IPv6 support for the nginx-proxy container by passing the value true to the ENABLE_IPV6 environment variable:

$ docker run -d -p 80:80 -e ENABLE_IPV6=true -v /var/run/docker.sock:/tmp/docker.sock:ro jwilder/nginx-proxy

Multiple Ports

If your container exposes multiple ports, nginx-proxy will default to the service running on port 80. If you need to specify a different port, you can set a VIRTUAL_PORT env var to select a different one. If your container only exposes one port and it has a VIRTUAL_HOST env var set, that port will be selected.

Multiple Hosts

If you need to support multiple virtual hosts for a container, you can separate each entry with commas. For example,,, and each host will be setup the same.

Wildcard Hosts

You can also use wildcards at the beginning and the end of host name, like * or*. Or even a regular expression, which can be very useful in conjunction with a wildcard DNS service like, using ~^foo\.bar\..*\.xip\.io will match, and all other given IPs. More information about this topic can be found in the nginx documentation about server_names.

Multiple Networks

With the addition of overlay networking in Docker 1.9, your nginx-proxy container may need to connect to backend containers on multiple networks. By default, if you don't pass the --net flag when your nginx-proxy container is created, it will only be attached to the default bridge network. This means that it will not be able to connect to containers on networks other than bridge.

If you want your nginx-proxy container to be attached to a different network, you must pass the --net=my-network option in your docker create or docker run command. At the time of this writing, only a single network can be specified at container creation time. To attach to other networks, you can use the docker network connect command after your container is created:

$ docker run -d -p 80:80 -v /var/run/docker.sock:/tmp/docker.sock:ro \
    --name my-nginx-proxy --net my-network jwilder/nginx-proxy
$ docker network connect my-other-network my-nginx-proxy

In this example, the my-nginx-proxy container will be connected to my-network and my-other-network and will be able to proxy to other containers attached to those networks.

SSL Backends

If you would like the reverse proxy to connect to your backend using HTTPS instead of HTTP, set VIRTUAL_PROTO=https on the backend container.

uWSGI Backends

If you would like to connect to uWSGI backend, set VIRTUAL_PROTO=uwsgi on the
backend container. Your backend container should than listen on a port rather
than a socket and expose that port.

Default Host

To set the default host for nginx use the env var for example

$ docker run -d -p 80:80 -e -v /var/run/docker.sock:/tmp/docker.sock:ro jwilder/nginx-proxy

Separate Containers

nginx-proxy can also be run as two separate containers using the jwilder/docker-gen
image and the official nginx image.

You may want to do this to prevent having the docker socket bound to a publicly exposed container service.

You can demo this pattern with docker-compose:

$ docker-compose --file docker-compose-separate-containers.yml up
$ curl -H "Host: whoami.local" localhost
I'm 5b129ab83266

To run nginx proxy as a separate container you'll need to have nginx.tmpl on your host system.

First start nginx with a volume:

$ docker run -d -p 80:80 --name nginx -v /tmp/nginx:/etc/nginx/conf.d -t nginx

Then start the docker-gen container with the shared volume and template:

$ docker run --volumes-from nginx \
    -v /var/run/docker.sock:/tmp/docker.sock:ro \
    -v $(pwd):/etc/docker-gen/templates \
    -t jwilder/docker-gen -notify-sighup nginx -watch /etc/docker-gen/templates/nginx.tmpl /etc/nginx/conf.d/default.conf

Finally, start your containers with VIRTUAL_HOST environment variables.

$ docker run -e  ...

SSL Support using letsencrypt

letsencrypt-nginx-proxy-companion is a lightweight companion container for the nginx-proxy. It allow the creation/renewal of Let's Encrypt certificates automatically.

SSL Support

SSL is supported using single host, wildcard and SNI certificates using naming conventions for
certificates or optionally specifying a cert name (for SNI) as an environment variable.

To enable SSL:

$ docker run -d -p 80:80 -p 443:443 -v /path/to/certs:/etc/nginx/certs -v /var/run/docker.sock:/tmp/docker.sock:ro jwilder/nginx-proxy

The contents of /path/to/certs should contain the certificates and private keys for any virtual
hosts in use. The certificate and keys should be named after the virtual host with a .crt and
.key extension. For example, a container with should have a and file in the certs directory.

If you are running the container in a virtualized environment (Hyper-V, VirtualBox, etc...),
/path/to/certs must exist in that environment or be made accessible to that environment.
By default, Docker is not able to mount directories on the host machine to containers running in a virtual machine.

Diffie-Hellman Groups

If you have Diffie-Hellman groups enabled, the files should be named after the virtual host with a
dhparam suffix and .pem extension. For example, a container with
should have a file in the certs directory.

Wildcard Certificates

Wildcard certificates and keys should be named after the domain name with a .crt and .key extension.
For example would use cert name and


If your certificate(s) supports multiple domain names, you can start a container with CERT_NAME=<name>
to identify the certificate to be used. For example, a certificate for * and *
could be named shared.crt and shared.key. A container running with
and CERT_NAME=shared will then use this shared cert.

How SSL Support Works

The SSL cipher configuration is based on mozilla nginx intermediate profile which
should provide compatibility with clients back to Firefox 1, Chrome 1, IE 7, Opera 5, Safari 1,
Windows XP IE8, Android 2.3, Java 7. The configuration also enables HSTS, and SSL
session caches.

The default behavior for the proxy when port 80 and 443 are exposed is as follows:

  • If a container has a usable cert, port 80 will redirect to 443 for that container so that HTTPS
    is always preferred when available.
  • If the container does not have a usable cert, a 503 will be returned.

Note that in the latter case, a browser may get an connection error as no certificate is available
to establish a connection. A self-signed or generic cert named default.crt and default.key
will allow a client browser to make a SSL connection (likely w/ a warning) and subsequently receive
a 500.

To serve traffic in both SSL and non-SSL modes without redirecting to SSL, you can include the
environment variable HTTPS_METHOD=noredirect (the default is HTTPS_METHOD=redirect). You can also
disable the non-SSL site entirely with HTTPS_METHOD=nohttp, or disable the HTTPS site with
HTTPS_METHOD=nohttps. HTTPS_METHOD must be specified on each container for which you want to
override the default behavior. If HTTPS_METHOD=noredirect is used, Strict Transport Security (HSTS)
is disabled to prevent HTTPS users from being redirected by the client. If you cannot get to the HTTP
site after changing this setting, your browser has probably cached the HSTS policy and is automatically
redirecting you back to HTTPS. You will need to clear your browser's HSTS cache or use an incognito
window / different browser.

Basic Authentication Support

In order to be able to secure your virtual host, you have to create a file named as its equivalent VIRTUAL_HOST variable on directory

$ docker run -d -p 80:80 -p 443:443 \
    -v /path/to/htpasswd:/etc/nginx/htpasswd \
    -v /path/to/certs:/etc/nginx/certs \
    -v /var/run/docker.sock:/tmp/docker.sock:ro \

You'll need apache2-utils on the machine where you plan to create the htpasswd file. Follow these instructions

Custom Nginx Configuration

If you need to configure Nginx beyond what is possible using environment variables, you can provide custom configuration files on either a proxy-wide or per-VIRTUAL_HOST basis.

Replacing default proxy settings

If you want to replace the default proxy settings for the nginx container, add a configuration file at /etc/nginx/proxy.conf. A file with the default settings would
look like this:

# HTTP 1.1 support
proxy_http_version 1.1;
proxy_buffering off;
proxy_set_header Host $http_host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $proxy_connection;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $proxy_x_forwarded_proto;
proxy_set_header X-Forwarded-Ssl $proxy_x_forwarded_ssl;
proxy_set_header X-Forwarded-Port $proxy_x_forwarded_port;

# Mitigate httpoxy attack (see README for details)
proxy_set_header Proxy "";

NOTE: If you provide this file it will replace the defaults; you may want to check the .tmpl file to make sure you have all of the needed options.

NOTE: The default configuration blocks the Proxy HTTP request header from being sent to downstream servers. This prevents attackers from using the so-called httpoxy attack. There is no legitimate reason for a client to send this header, and there are many vulnerable languages / platforms (CVE-2016-5385, CVE-2016-5386, CVE-2016-5387, CVE-2016-5388, CVE-2016-1000109, CVE-2016-1000110, CERT-VU#797896).


To add settings on a proxy-wide basis, add your configuration file under /etc/nginx/conf.d using a name ending in .conf.

This can be done in a derived image by creating the file in a RUN command or by COPYing the file into conf.d:

FROM jwilder/nginx-proxy
RUN { \
      echo 'server_tokens off;'; \
      echo 'client_max_body_size 100m;'; \
    } > /etc/nginx/conf.d/my_proxy.conf

Or it can be done by mounting in your custom configuration in your docker run command:

$ docker run -d -p 80:80 -p 443:443 -v /path/to/my_proxy.conf:/etc/nginx/conf.d/my_proxy.conf:ro -v /var/run/docker.sock:/tmp/docker.sock:ro jwilder/nginx-proxy


To add settings on a per-VIRTUAL_HOST basis, add your configuration file under /etc/nginx/vhost.d. Unlike in the proxy-wide case, which allows multiple config files with any name ending in .conf, the per-VIRTUAL_HOST file must be named exactly after the VIRTUAL_HOST.

In order to allow virtual hosts to be dynamically configured as backends are added and removed, it makes the most sense to mount an external directory as /etc/nginx/vhost.d as opposed to using derived images or mounting individual configuration files.

For example, if you have a virtual host named, you could provide a custom configuration for that host as follows:

$ docker run -d -p 80:80 -p 443:443 -v /path/to/vhost.d:/etc/nginx/vhost.d:ro -v /var/run/docker.sock:/tmp/docker.sock:ro jwilder/nginx-proxy
$ { echo 'server_tokens off;'; echo 'client_max_body_size 100m;'; } > /path/to/vhost.d/

If you are using multiple hostnames for a single container (e.g.,, the virtual host configuration file must exist for each hostname. If you would like to use the same configuration for multiple virtual host names, you can use a symlink:

$ { echo 'server_tokens off;'; echo 'client_max_body_size 100m;'; } > /path/to/vhost.d/
$ ln -s /path/to/vhost.d/ /path/to/vhost.d/

Per-VIRTUAL_HOST default configuration

If you want most of your virtual hosts to use a default single configuration and then override on a few specific ones, add those settings to the /etc/nginx/vhost.d/default file. This file
will be used on any virtual host which does not have a /etc/nginx/vhost.d/{VIRTUAL_HOST} file associated with it.

Per-VIRTUAL_HOST location configuration

To add settings to the "location" block on a per-VIRTUAL_HOST basis, add your configuration file under /etc/nginx/vhost.d
just like the previous section except with the suffix _location.

For example, if you have a virtual host named and you have configured a proxy_cache my-cache in another custom file, you could tell it to use a proxy cache as follows:

$ docker run -d -p 80:80 -p 443:443 -v /path/to/vhost.d:/etc/nginx/vhost.d:ro -v /var/run/docker.sock:/tmp/docker.sock:ro jwilder/nginx-proxy
$ { echo 'proxy_cache my-cache;'; echo 'proxy_cache_valid  200 302  60m;'; echo 'proxy_cache_valid  404 1m;' } > /path/to/vhost.d/app.example.com_location

If you are using multiple hostnames for a single container (e.g.,, the virtual host configuration file must exist for each hostname. If you would like to use the same configuration for multiple virtual host names, you can use a symlink:

$ { echo 'proxy_cache my-cache;'; echo 'proxy_cache_valid  200 302  60m;'; echo 'proxy_cache_valid  404 1m;' } > /path/to/vhost.d/app.example.com_location
$ ln -s /path/to/vhost.d/ /path/to/vhost.d/

Per-VIRTUAL_HOST location default configuration

If you want most of your virtual hosts to use a default single location block configuration and then override on a few specific ones, add those settings to the /etc/nginx/vhost.d/default_location file. This file
will be used on any virtual host which does not have a /etc/nginx/vhost.d/{VIRTUAL_HOST}_location file associated with it.


Before submitting pull requests or issues, please check github to make sure an existing issue or pull request is not already open.

Running Tests Locally

To run tests, you need to prepare the docker image to test which must be tagged jwilder/nginx-proxy:test:

docker build -t jwilder/nginx-proxy:test .  # build the Debian variant image

and call the test/ script.

Then build the Alpine variant of the image:

docker build -f Dockerfile.alpine -t jwilder/nginx-proxy:test .  # build the Alpline variant image

and call the test/ script again.

If your system has the make command, you can automate those tasks by calling:

make test

You can learn more about how the test suite works and how to write new tests in the test/ file.

Docker Pull Command
Source Repository

Comments (36)
5 months ago

Need help from experts here.

I have two servers running exact copy of jwilder/nginx-proxy.

One will pass virtual host information as see in this log.

nginx-proxy | nginx.1 | - - [24/Oct/2016:15:33:48 +0000] "GET / HTTP/1.1" 302 0 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36"
nginx-proxy | nginx.1 | - - [24/Oct/2016:15:33:49 +0000] "GET /wp-admin/install.php HTTP/1.1" 200 3120 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36"

Another one will only pass the ip address so I can't make virtual host to work on this server.
Here is the log.

nginx-proxy | nginx.1 | - - [24/Oct/2016:15:43:41 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36" "-"

Both servers running Debian Jessie with latest update.

a year ago

Unfortunately this did not work for me. The generated nginx.conf upstream contexts were missing their IP addresses. This was likely due to the containers I was trying to reverse proxy to being part of my user-defined, isolated network. A github issue I found seems to indicate that was the cause. In the end I just setup an nginx image myself manually to do the proxy -- which honestly is pretty straight forward (just look at the nginx conf templates that this image uses.)

a year ago

Hi jwilder.

Would you mind providing it for ARM arch ? I'm busy making it work on my raspberry running HypriotOS, but I'ld prefer getting this upstream.

And many thanks for the great job man!

a year ago

Hey! I'm having an issue setting up a forward to Graham Pugh's Munki-Do container ( This container exposes port 8000, and in the docker run command I'm setting the VIRTUAL_HOST to an arbitrary value that's setup on my local DNS server (-e VIRTUAL_HOST=munki-do) and the PORT to 8087 (-e VIRTUAL_PORT=8087). My entire run command is as follows:

sudo docker run -d \
--name Munki-Do \
-p 8087:8000 \
-e VIRTUAL_HOST=munki-do \
-e VIRTUAL_PORT=8087 \
--restart=always \

However, when I attempt to access the container via a browser, I get the following errors in the docker logs:

nginx.1 | 2015/12/04 12:19:27 [error] 49#49: *72818 no live upstreams while connecting to upstream, client:, server: munki-do, request: "GET / HTTP/1.1", upstream: "http://munki-do/", host: "Munki-Do"

Any idea what this means?


a year ago

I think I handled It... I put "client_max_body_size 15000M;"
into nginx.conf
It seems to work...

a year ago

I confirm owncloud works fine alone

a year ago

Hello, thank you for your great container. I am using owncloud and I spend my day at trying to make it accept files that are more than 2mb. I modified php.ini, checked apache, tried an owncloud-nginx container : no change. And I can't get what's wrong with your container, but the error "Request entity too large" appears with all owncloud versions. Can you give me a lift out of hell please ? Running docker on ubuntu server 14.04.3 64bits 3GHz x2 6Go RAM, I would prefer to use the official owncloud container.

Thanks a lot by advance for your help

a year ago

I am using the nginx-proxy to proxy a magento container (using nginx within the magento container) with SSL. I was able to set up the containers and run the magento installation routine via nginx-proxy with an SSL connection. After completing the installation I get a ERR_TOO_MANY_REDIRECTS error message whenever I try to acccess the magento frontend or backend.

This seems to be weird since there is obviously no problem during installation, so I assume there must be something I am not getting yet. I assume that there are some rewrite rules which mess up the architecture, but I cannot figure out what's wrong.

This is my configuration of the magento nginx:

# The default server
server {

listen   443;

ssl on;


ssl_certificate        /etc/certs/;
ssl_certificate_key    /etc/certs/;

#charset koi8-r;

root   /var/www;
index  index.html index.htm index.php;

location / {

    index index.html index.php; ## Allow a static html file to be shown first

    try_files $uri $uri/ @handler; ## If missing pass the URI to Magento's front handler

    expires 30d; ## Assume all files are cachable


## These locations would be hidden by .htaccess normally

location ^~ /app/                { deny all; }

location ^~ /includes/           { deny all; }

location ^~ /lib/                { deny all; }

location ^~ /media/downloadable/ { deny all; }

location ^~ /pkginfo/            { deny all; }

location ^~ /report/config.xml   { deny all; }

location ^~ /var/                { deny all; }

location /var/export/ { ## Allow admins only to view export folder

    auth_basic           "Restricted"; ## Message shown in login window

    auth_basic_user_file htpasswd; ## See /etc/nginx/htpassword

    autoindex            on;


location  /. { ## Disable .htaccess and other hidden files

    return 404;


location @handler { ## Magento uses a common front handler

    rewrite / /index.php;


location ~ .php/ { ## Forward paths like /js/index.php/x.js to relevant handler

    rewrite ^(.*.php)/ $1 last;

    fastcgi_read_timeout 500;


# pass the PHP scripts to FastCGI server listening on
location ~ \.php$ {
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    include        fastcgi_params;


I added the following evironment variables to the magento docker container:

Furthermore I added the ssl certificates to the nginx proxy as well as to the magento nginx configuration. So each container (nginx proxy and magento) contain this certificates.

The idea is that the nginx proxy forwards all requests from to the magento container via an SSL connection.

The magento container does not expose any ports to the public but the nginx proxy links to the container (for security reasons).

I have been desperately working on this for several days now and greatly appreciate any hint. Could there be any issue with the certificates? E.g. if the forwarded request from the nginx proxy coontains a different server name and there cannot verify with the certificates on magento?! But if so, how could that be resolved?

Thanks Peter

2 years ago

I have a container that exposes two different ports, for example 22 and 80.
How can I set a virtual host for each port? -> 22 -> 80

2 years ago

BTW, I just tested with another docker container... smooth... well done ;)