How to setup a Docker Nginx reverse proxy server example

How to configure a Docker Nginx reverse proxy

To create, configure and run a Docker Nginx reverse proxy image, follow these steps:

  1. Download the official Nginx Docker image from Docker Hub
  2. Run the Nginx Docker image as a container
  3. Copy the Docker container’s Nginx config file to your local file system
  4. Add proxy_pass entries that point to your backend origin servers
  5. Copy the config file back into the Nginx Docker container
  6. Reload the Nginx configuration and test the setup
  7. Create a new Docker Nginx image based on the validated container

Start with the official Nginx image

To configure an Nginx reverse proxy in Docker, you first need to download and run the official Nginx image from Docker Hub.

sudo docker run -d --name nginx-base -p 80:80 nginx:latest

By default, the Nginx Docker image is configured as a file server, not a reverse proxy or a load balancer.

To setup the Nginx Docker image as a reverse proxy, edits must be made to container’s default.conf file.

Copy the Nginx config file from Docker

Issue the following command to copy the Nginx configuration file in the Docker container to the Desktop folder of the host machine’s local file system:

sudo docker cp nginx-base:/etc/nginx/conf.d/default.conf ~/Desktop/default.conf

Reverse proxy example setup

A reverse proxy works by handling client requests, and then forwarding those requests to another server running in the backend. The backend origin server processes the request and provides a response back to Nginx, which then send the response back to the client.

For this example, we have an Apache Tomcat instance acting as the backend origin server. Apache Tomcat will be handling requests on the following URL:

http://192.168.246.131:8080/sample

We do not want clients connecting to the backend server on port 8080. Instead, we want all requests to first go through the Nginx server on the following URL:

http://localhost/sample

Nginx will act as a reverse proxy, forwarding all requests that it receives with the context root /sample to the corresponding application running on Apache Tomcat.

Nginx reverse proxy example

This Docker Nginx reverse proxy example uses Tomcat as the origin server, but any backend server will work.

Nginx and Docker reverse proxy configuration

To proxy requests, Nginx will need to add a new location entry to the default.conf file that maps aps the /sample URI to the backend server through a proxy_pass setting.

The Nginx reverse proxy entry will look as follows:

# Docker nginx reverse proxy proxy_pass entry
location /sample{
  proxy_pass http://192.168.246.131:8080/sample;
}

Nginx Docker reverse proxy config file

The complete default.conf file for the Docker Nginx reverse proxy example reads as follows:

# Complete Nginx Docker reverse proxy config file
server {
  listen 80;
  listen [::]:80;
  server_name localhost;

  location / {
    root /usr/share/nginx/html;
    index index.html index.htm;
  }

  location /sample {
    proxy_pass http://192.168.246.131:8080/sample;
  }

  error_page 500 502 503 504 /50x.html;
  location = /50x.html {
    root /usr/share/nginx/html;
  }
} # End of Docker Nginx reverse proxy example file

Copy the Nginx config file back to Docker

With the config file edited locally to turn Nginx into a reverse proxy, the file must now be copied into the Nginx Docker container.

sudo docker cp ~/Desktop/default.conf nginx-base:/etc/nginx/conf.d/

With the file copied into the container, validate and reload the Docker Nginx reverse proxy configuration:

sudo docker exec nginx-base nginx -t
sudo docker exec nginx-base nginx -s reload

Now test the Docker Nginx reverse proxy example. When you access http://localhost/sample, Nginx should render the content from the Tomcat application.

If requests first go through Nginx and are then handled by a backend server like Apache Tomcat, the Nginx reverse proxy in Docker is working correctly.

Create a new Docker Nginx reverse proxy image

With the Docker Nginx reverse proxy container working, create a new Docker image based on the container’s configuration:

sudo docker commit nginx-base nginx-proxy

This will create a new Docker image named nginx-proxy. You can confirm the existence of this new image containing your Docker Nginx reverse proxy image by listing all of the Docker images:

[email protected]:~/Desktop$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx-proxy latest d3b8cb6941b4 14 seconds ago 142MB
sample latest 32f5030ab445 26 hours ago 680MB
tomcat latest c795915cb678 8 days ago 680MB
nginx latest 0e901e68141f 8 days ago 142MB

Nginx reverse proxy server Dockerfile

An alternative way to create a new Docker Nginx reverse proxy image is to use a Dockerfile that loads the edited default.conf file every time the image is built. This can be preferable if the Nginx reverse proxy Docker image is regularly reconfigured.

The content of the Nginx reverse proxy Dockerfile is as follows:

FROM nginx:latest
COPY default.conf /etc/nginx/conf.d/default.conf

New Nginx Docker image creation

When the file is saved, run the docker build command in the same folder as the Dockerfile and the default.conf file

sudo docker build -t nginx-reverse-proxy .

This build command will create a new Docker image named reverse-proxy.

[email protected]:~/Desktop$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx-reverse-proxy latest 25e3f852189f 36 seconds ago 142MB
nginx-proxy latest d3b8cb6941b4 7 minutes ago 142MB
sample latest 32f5030ab445 26 hours ago 680MB
tomcat latest c795915cb678 8 days ago 680MB
nginx latest 0e901e68141f 8 days ago 142MB

When the new image runs, the Docker Nginx reverse proxy will be active.

And that’s how easy it is to create a Docker Nginx reverse proxy server that can intercept client requests and pass them on to application servers running in the backend.

SearchAppArchitecture
SearchSoftwareQuality
SearchCloudComputing
SearchSecurity
SearchAWS
Close