Nginx Proxy Docker Container with External Config

Nginx provides an excellent docker image which, among other things, allows you to mount an external config file. I use this on my dev machine to quickly spin up and configure a reverse proxy for my applications. My config file is as such.

worker_processes 1;

events { worker_connections 1024; }

http {

    sendfile on;

    upstream app_servers {
        server; #IP:Port of my application

    server {

        server_name myapp.local

        listen 80;

        location / {

            proxy_pass         http://app_servers;
            proxy_redirect     off;
            proxy_set_header   Host $host;
            proxy_set_header   X-Real-IP $remote_addr;
            proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header   X-Forwarded-Host $server_name;


This file is stored in /Users/bashby/configs/nginx/reverse-proxy/nginx.conf. To start up my proxy I first need to make sure OSX's built in apache server isn't running via sudo service apachectl stop then I can execute the docker command to spin it up:

docker run --rm -p 80:80 -v /Users/bashby/configs/nginx/reverse-proxy/nginx.conf:/etc/nginx/nginx.conf:ro -d nginx

I like to keep my docker containers as transient as possible thus the --rm specification. Anyway I can now add myapp.local to my hosts file and go to town. This method is great because you can save and quickly change proxy configs. I was using homebrew's apache2.4 module for a while to do this but I really like using nginx in docker much better.

Written by Ben Ashby on 31 January 2017