NGINX Reverse Proxy for Docker

By James Kenny
2 min read

Table of Contents

This post is a guide on setting the NGINX Config to work as a reverse proxy to our web application inside a docker container. When you use docker to host your web applications and services. It's handy to be able to use a reverse proxy to pass the traffic on port 80 and route it to the right container.

One cool thing about NGINX is we can running docker and static sites on the same linux box. It's just a case of having different NGINX config files.

I've setup a web application built in .net core and running inside a container. On my linux server I create a docker instance and set it to run on port 8080.

docker run --name some-app1 -d -p 8080:80 some-app1

Once the docker container is running we can go ahead and create a NGINX config for the reverse proxy.

Reverse Proxy Config

First in the folder we need to create our config.

/etc/nginx/site-available

The following is our reverse proxy config for a website I have called 'touroperator.io'

server {
  listen 80;

  listen [::]:80;

  server_name touroperator.io www.touroperator.io;

   set $upstream 127.0.0.1:8080;

 location / {
 
 proxy_pass_header Authorization;
 proxy_pass http://$upstream;
 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_http_version 1.1;
 proxy_set_header Connection "";
 proxy_buffering off;
 client_max_body_size 0;
 proxy_read_timeout 36000s;
 proxy_redirect off;
 }
}

The config is doing a number of things.

  • Listen for traffic on Port 80
  • Only deal with requests for the touroperator.io or www.touroperator.io domain name
  • Upstream we set it to 127.0.0.1 and port 8080 so it's sending traffic to it's own network card on port 8080. This is the same port our docker image is running on.
  • Next in Location we pass a number of bits of information up the pipe to the website.

Note: this type of config can be used for load balancing and other things. As I explore this deeper more posts will follow.

Next we need to create a short cut in the 'site-enabled' folder.

ln -s /etc/nginx/sites-available/touroperator.io /etc/nginx/sites-enabled/touroperator.io

Remember to change the file name to your own.

Lastly reload the NGINX server

nginx -s reload

And point your DNS and you are running. The traffic will be routed to the docker container.

Tagged in:

NGINX, Docker

Last Update: August 21, 2018

About the Author