Home Backend Development PHP Tutorial How to configure Nginx proxy server and enable SSL using Docker container?

How to configure Nginx proxy server and enable SSL using Docker container?

Sep 05, 2023 pm 04:45 PM
docker nginx ssl

How to configure Nginx proxy server and enable SSL using Docker container?

How to configure Nginx proxy server and enable SSL using Docker container?

In today's network environment, security has become an issue that cannot be ignored. In order to protect the secure transmission of data, enabling SSL certificates has become a necessary step. Using Docker containers to configure Nginx proxy servers has become a trend. In this article, we will explain how to use Docker containers to configure Nginx proxy server and enable SSL.

Step 1: Install Docker
First, we need to install Docker. You can install it by referring to Docker's official installation guide depending on your operating system.

Step 2: Create an Nginx configuration file
Before configuring the Nginx proxy server, we need to create a configuration file to define our proxy rules. Create a file named nginx.conf and paste the following content into it:

worker_processes auto;

events {
  worker_connections 1024;
}

http {
  server {
    listen 80;
    server_name example.com;
    
    location / {
      proxy_pass http://backend;
      proxy_set_header Host $host;
    }
  }
}
Copy after login

The above configuration file specifies that the Nginx proxy server listens to port 80 and proxy all requests to A backend server named backend.

Step 3: Create a Docker image
Next, we need to create a Docker image to run the Nginx proxy server. In the command line, execute the following command:

docker build -t nginx-proxy .
Copy after login

This command will read the Dockerfile file in the current directory and automatically build a Docker image according to the instructions in it, named nginx-proxy.

Step 4: Run the Docker container
There is an official Nginx proxy server image named jwilder/nginx-proxy on Docker Hub. We can use this image to run an Nginx proxy server container.

In the command line, execute the following command:

docker run -d -p 80:80 -p 443:443 -v /var/run/docker.sock:/tmp/docker.sock:ro --name nginx-proxy nginx-proxy
Copy after login

This command will create a Docker container named nginx-proxy and map the host's 80 port to The container's port 80 and port 443 are mapped to the container's port 443. -v /var/run/docker.sock:/tmp/docker.sock:roThis option is to enable the Docker container to read the Docker socket file on the host.

Step 5: Configure SSL Certificate
In order to enable SSL, we need to configure an SSL certificate for our proxy server. We can use a free SSL certificate provided by Let's Encrypt.

In the command line, execute the following command:

docker run -d -p 80:80 -p 443:443 --name nginx-proxy nginx-proxy
Copy after login

This command will re-create a Docker container named nginx-proxy and map the host's 80 port To port 80 of the container, port 443 is mapped to port 443 of the container.

Then, execute the following command to generate an SSL certificate:

docker exec nginx-proxy /app/certbot-setup.sh
Copy after login

The script will automatically install the certbot client and request the Let's Encrypt server to generate an SSL certificate.

Step 6: Configure proxy rules
Finally, we need to configure our proxy rules. We can use Docker's environment variables to define our proxy rules.

In the command line, execute the following command:

docker run -d -p 80:80 -p 443:443 -e "VIRTUAL_HOST=example.com" -e "LETSENCRYPT_HOST=example.com" --name backend your-backend
Copy after login

This command will create a Docker container named backend and map the host's 80 port to the container's Port 80 and port 443 are mapped to port 443 of the container. -e "VIRTUAL_HOST=example.com" -e "LETSENCRYPT_HOST=example.com"These two options will configure our proxy rules.

After completing the above steps, our Nginx proxy server can perform secure proxying through SSL. You can customize more proxy rules and add more containers to meet your needs.

Hope this article is helpful to you. If you have any questions, please feel free to leave a comment below and I will try to answer them. thanks for reading!

The above is the detailed content of How to configure Nginx proxy server and enable SSL using Docker container?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1656
14
PHP Tutorial
1257
29
C# Tutorial
1229
24
How to exit the container by docker How to exit the container by docker Apr 15, 2025 pm 12:15 PM

Four ways to exit Docker container: Use Ctrl D in the container terminal Enter exit command in the container terminal Use docker stop <container_name> Command Use docker kill <container_name> command in the host terminal (force exit)

How to copy files in docker to outside How to copy files in docker to outside Apr 15, 2025 pm 12:12 PM

Methods for copying files to external hosts in Docker: Use the docker cp command: Execute docker cp [Options] <Container Path> <Host Path>. Using data volumes: Create a directory on the host, and use the -v parameter to mount the directory into the container when creating the container to achieve bidirectional file synchronization.

How to restart docker How to restart docker Apr 15, 2025 pm 12:06 PM

How to restart the Docker container: get the container ID (docker ps); stop the container (docker stop <container_id>); start the container (docker start <container_id>); verify that the restart is successful (docker ps). Other methods: Docker Compose (docker-compose restart) or Docker API (see Docker documentation).

How to check the name of the docker container How to check the name of the docker container Apr 15, 2025 pm 12:21 PM

You can query the Docker container name by following the steps: List all containers (docker ps). Filter the container list (using the grep command). Gets the container name (located in the "NAMES" column).

How to start containers by docker How to start containers by docker Apr 15, 2025 pm 12:27 PM

Docker container startup steps: Pull the container image: Run "docker pull [mirror name]". Create a container: Use "docker create [options] [mirror name] [commands and parameters]". Start the container: Execute "docker start [Container name or ID]". Check container status: Verify that the container is running with "docker ps".

How to start mysql by docker How to start mysql by docker Apr 15, 2025 pm 12:09 PM

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

How to view logs from docker How to view logs from docker Apr 15, 2025 pm 12:24 PM

The methods to view Docker logs include: using the docker logs command, for example: docker logs CONTAINER_NAME Use the docker exec command to run /bin/sh and view the log file, for example: docker exec -it CONTAINER_NAME /bin/sh ; cat /var/log/CONTAINER_NAME.log Use the docker-compose logs command of Docker Compose, for example: docker-compose -f docker-com

How to update the image of docker How to update the image of docker Apr 15, 2025 pm 12:03 PM

The steps to update a Docker image are as follows: Pull the latest image tag New image Delete the old image for a specific tag (optional) Restart the container (if needed)

See all articles