How to Set up Nginx as Reverse Proxy for Docker Container

Nginx is one of the most popular webserver and reverse proxy, with high performance metrics. You typically use this for load balancing, or caching, and serving up static content. Nginx together with Docker, an application platform for containerizing applications, makes it possible to deploy scalable applications in a performant environment.

Follow this article to be able to set Nginx as a reverse proxy for your Docker containers, and run your apps correctly!

Prerequisites

Before proceeding with this setup, please ensure you have the above configurations in place.

  1. Unix-based / Virtual Machine (like Ubuntu / CentOS).
  2. Access to the server via root or sudo.
  3. Thus,docker and docker-compose are installed in your server.
  4. Familiar with Nginx and Docker.

Setting Up Nginx Reverse Proxy for Docker

These are the steps to configure Nginx reverse proxy for Docker containers:

Step 1: Install Docker and Docker Compose

First, if you have not already installed Docker, refer to our guide on how to install Docker on the Ubuntu Linux system. After that install Docker Compose by typing the following command in the terminal:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
sudo curl -L "https://github.com/docker/compose/releases/download/1.27.4/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo curl -L "https://github.com/docker/compose/releases/download/1.27.4/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo curl -L "https://github.com/docker/compose/releases/download/1.27.4/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
docker compose

Docker Compose is a tool for defining and running multi-container Docker applications. Apply executable permissions to the Docker Compose binary:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
sudo chmod +x /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose

Verify Installation with the following command:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
docker --version
docker --version
docker --version
docker version

Verify the Docker Compose installation:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
docker-compose --version
docker-compose --version
docker-compose --version
docker compose version

Step 2: Create a Docker Network

To allow Nginx and your application containers to communicate, create a Docker network:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
sudo docker network create nginx_reverse_proxy
sudo docker network create nginx_reverse_proxy
sudo docker network create nginx_reverse_proxy
docker network

Step 3: Setup Application Container

Next, create a simple application container to demonstrate the reverse proxy setup. For this example, we will use a basic Nginx container:

Create a Docker Compose file named docker-compose.yaml with the following content:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
version: '3'
services:
app:
image: nginx
container_name: my_app
networks:
- nginx_reverse_proxy
volumes:
- ./app:/usr/share/nginx/html
ports:
- "8080:80"
networks:
nginx_reverse_proxy:
external: true
version: '3' services: app: image: nginx container_name: my_app networks: - nginx_reverse_proxy volumes: - ./app:/usr/share/nginx/html ports: - "8080:80" networks: nginx_reverse_proxy: external: true
version: '3'
services:
  app:
    image: nginx
    container_name: my_app
    networks:
      - nginx_reverse_proxy
    volumes:
      - ./app:/usr/share/nginx/html
    ports:
      - "8080:80"
networks:
  nginx_reverse_proxy:
    external: true
docker compose yaml

Create a directory for your application and a simple index.html file:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
mkdir app && echo "Hello, World!" > app/index.html
mkdir app && echo "Hello, World!" > app/index.html
mkdir app && echo "Hello, World!" > app/index.html

Start the application container:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
sudo docker-compose up -d
sudo docker-compose up -d
sudo docker-compose up -d
pulling nginx

Step 4: Configure Nginx Reverse Proxy

Now, set up Nginx as a reverse proxy for your application container:

Install Nginx on your server with the following command:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
sudo apt-get install nginx
sudo apt-get install nginx
sudo apt-get install nginx
install nginx

Create an Nginx configuration file for the docker reverse proxy:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
sudo nano /etc/nginx/conf.d/reverse-proxy.conf
sudo nano /etc/nginx/conf.d/reverse-proxy.conf
sudo nano /etc/nginx/conf.d/reverse-proxy.conf

Add the following content to the docker Nginx proxy configuration file:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
server {
listen 80;
location / {
proxy_pass http://my_app:80;
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-Proto $scheme;
}
}
server { listen 80; location / { proxy_pass http://my_app:80; 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-Proto $scheme; } }
server {
    listen 80;
    location / {
        proxy_pass http://my_app:80;
        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-Proto $scheme;
    }
}
reverse proxy configuration

Save and exit the file. Test the Nginx configuration:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
sudo nginx -t
sudo nginx -t
sudo nginx -t

Reload Nginx to apply the new configuration:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
sudo systemctl reload nginx
sudo systemctl reload nginx
sudo systemctl reload nginx

Step 5: Verify Setup

To verify that Nginx is correctly set up as a reverse proxy for your Docker container, open a web browser and navigate to your server’s IP address. You should see the “Hello, World!” message from the Nginx container.

Step 6: Secure Setup with SSL

To secure your setup with SSL, follow these steps to obtain and configure an SSL certificate using Let’s Encrypt.

Install Certbot, the Let’s Encrypt client:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
sudo apt-get install certbot python3-certbot-nginx
sudo apt-get install certbot python3-certbot-nginx
sudo apt-get install certbot python3-certbot-nginx

Obtain an SSL certificate for your domain:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com

Follow the prompts to complete the installation and configuration. Certbot will automatically configure Nginx to use the SSL certificate. Your Nginx configuration file should now look like this:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
server {
listen 80;
server_name yourdomain.com www.yourdomain.com;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl;
server_name yourdomain.com www.yourdomain.com;
ssl_certificate /etc/letsencrypt/live/yourdomain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/yourdomain.com/privkey.pem;
location / {
proxy_pass http://my_app:80;
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-Proto $scheme;
}
}
server { listen 80; server_name yourdomain.com www.yourdomain.com; return 301 https://$host$request_uri; } server { listen 443 ssl; server_name yourdomain.com www.yourdomain.com; ssl_certificate /etc/letsencrypt/live/yourdomain.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/yourdomain.com/privkey.pem; location / { proxy_pass http://my_app:80; 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-Proto $scheme; } }
server {
    listen 80;
    server_name yourdomain.com www.yourdomain.com;
    return 301 https://$host$request_uri;
}
server {
    listen 443 ssl;
    server_name yourdomain.com www.yourdomain.com;
    ssl_certificate /etc/letsencrypt/live/yourdomain.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/yourdomain.com/privkey.pem;
    location / {
        proxy_pass http://my_app:80;
        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-Proto $scheme;
    }
}

Test the Nginx configuration again then reload Nginx to apply the changes.

Conclusion

After successfully setting up Nginx as a reverse proxy for the Docker container. This setup not only enhances the performance and scalability of applications but also adds a layer of security with SSL encryption. By leveraging the power of Nginx and Docker you can deploy and manage your applications more efficiently and effectively. Keep experimenting with different configurations to optimize your docker Nginx reverse proxy setup further.

Experience top-notch security and performance with Ultahost’s VPS with DDoS protection. Safeguard your server while maximizing speed and efficiency for your Nginx reverse proxy Docker setup. Choose Ultahost for unparalleled reliability and peace of mind.

FAQ

What is a reverse proxy?

Related Post

How to Set Up and Use Private Docker Registry

Setting up a private Docker registry can significantly ...

node js docker

How to Build a Node.js App with Docker

Docker is known as the industry leader when it comes to...

How to Set Up an Nginx Reverse Proxy

A reverse proxy server acts as an intermediary between ...

How to Install NGINX on Ubuntu 22.04

NGINX, which is known for its great performance, scalab...

Install RabbitMQ Linux

How to Install RabbitMQ in Linux

RabbitMQ is a free and open-source messaging system tha...

How to Set Up Laravel in Docker

Laravel is a PHP based web framework known for its clea...

Leave a Comment