Setup NGINX Reverse Proxy with SSL on Docker in Minutes

Published on

Are you tired of dealing with complicated server configurations and time-consuming setup processes? Look no further! In this blog post, we will guide you through setting up NGINX reverse proxy with SSL on Docker in no time. With this setup, you can effortlessly handle multiple applications on a single server, saving you valuable time and effort.

NGINX reverse proxy is a fantastic tool for handling the routing of incoming traffic to different backend applications. By setting up SSL, you can ensure secure and encrypted communication between your server and clients. Docker, on the other hand, makes it a breeze to package and distribute applications, offering an efficient and isolated environment for running your services.

So, without further ado, let's dive into the steps to set up NGINX reverse proxy with SSL on Docker:

Step 1: Install Docker The first step is to install Docker on your machine. Docker provides installation packages for various operating systems, so simply choose the appropriate one for your setup.

Step 2: Prepare a Docker Compose file Create a new directory for your NGINX reverse proxy setup and navigate into it. Create a file named docker-compose.yml and open it in your favorite text editor.

In this file, we will define the services we want to run – in this case, NGINX and our backend application. Here's an example of a basic Docker Compose file for our setup:

version: '3'
services:
  nginx:
    image: nginx
    ports:
      - 80:80
      - 443:443
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf
      - ./certs:/etc/nginx/certs
    networks:
      - backend
  backend:
    image: your_backend_image_name
    # Add necessary configuration for your backend service
    networks:
      - backend
networks:
  backend:

Step 3: Configure NGINX In the same directory where the docker-compose.yml file is located, create another directory called certs. Inside the certs directory, place your SSL certificate files (e.g., your_domain.crt and your_domain.key).

Next, create an nginx.conf file in the same directory as the docker-compose.yml file. In this file, we will define the NGINX server configuration. Here's an example configuration to get you started:

events {
  worker_connections  1024;
}

http {
  server {
    listen 80;
    server_name your_domain.com;
    return 301 https://$host$request_uri;
  }

  server {
    listen 443 ssl;
    server_name your_domain.com;

    ssl_certificate /etc/nginx/certs/your_domain.crt;
    ssl_certificate_key /etc/nginx/certs/your_domain.key;

    location / {
      proxy_pass http://backend:your_backend_port;
      proxy_set_header Host $host;
      proxy_set_header X-Real-IP $remote_addr;
    }
  }
}

Make sure to replace your_domain.com with your actual domain and your_backend_port with the port your backend service is running on.

Step 4: Run docker-compose From your terminal, navigate to the directory where the docker-compose.yml file is located. Run the following command to start the NGINX reverse proxy and your backend service:

docker-compose up -d

The -d flag runs the containers in the background, allowing you to continue using your terminal.

And that's it! You now have a fully functional NGINX reverse proxy with SSL setup running on Docker. Any incoming traffic to the specified domain will be routed to your backend application over an encrypted connection.

Setting up NGINX reverse proxy with SSL using Docker has never been easier. With just a few simple steps, you can save yourself from the hassle of manual server configurations and enjoy a faster and more secure workflow. So, go ahead and give it a try – you'll be up and running in minutes!