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:

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 Set Up an Nginx Reverse Proxy

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

How to Limit Docker Memory and CPU Usage

Docker is a powerful tool that simplifies software deve...

How To Remove Docker Images, Volumes and Cont

Docker is a popular containerization platform that allo...

How to Redirect HTTP to HTTPS in Nginx

Nginx is a powerful and versatile web server that provi...

How to Check and Manage Logs on Docker Compo

Managing logs in Docker Compose is essential for develo...

How to Share Data Between Docker Containers

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

Leave a Comment