How to Set up Nginx as Reverse Proxy for Docker Container

Nginx, a high-performance web server and reverse proxy is widely used for load balancing, caching, and serving static content. Combining Nginx with Docker a platform for containerizing applications creates a robust environment for deploying scalable applications.

In this article, we will cover the steps to set up Nginx as a reverse proxy for Docker containers, ensuring your applications run smoothly and efficiently.

Prerequisites

Before diving into the setup, ensure you have the following prerequisites:

  1. A server or virtual machine running a Unix-like operating system such as Ubuntu and CentOS.
  2. Root or sudo access to the server.
  3. Docker and Docker Compose are installed on the server.
  4. Basic knowledge of Nginx and Docker.

Setting Up Nginx Reverse Proxy for Docker

Following are the steps described below to set up 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:

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:

sudo chmod +x /usr/local/bin/docker-compose

Verify Installation with the following command:

docker --version
docker version

Verify the Docker Compose installation:

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:

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:

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:

mkdir app && echo "Hello, World!" > app/index.html

Start the application container:

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:

sudo apt-get install nginx
install nginx

Create an Nginx configuration file for the docker reverse proxy:

sudo nano /etc/nginx/conf.d/reverse-proxy.conf

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

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:

sudo nginx -t

Reload Nginx to apply the new configuration:

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:

sudo apt-get install certbot python3-certbot-nginx

Obtain an SSL certificate for your domain:

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:

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?
Why use Nginx as a reverse proxy for Docker?
Do I need Docker installed to set up Nginx as a reverse proxy?
Can I use Nginx for multiple Docker containers?
What ports should I use for Nginx and Docker containers?
Do I need a domain name for the setup?
Is SSL/TLS supported with Nginx as a reverse proxy?

Related Post

How to Share Data Between Docker Containers

Docker is a containerization platform that enables us t...

How to Install NGINX on Ubuntu 22.04

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

How to Install Docker on Debian

Docker is a powerful containerization platform that all...

How to Set Docker Environment Variables

Docker has revolutionized how we build, deploy, and run...

How to Override ENTRYPOINT Using docker run

Docker containers make it easy to run applications in i...

How to Set Up an Nginx Reverse Proxy

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

Leave a Comment