Home Backend Development PHP Tutorial How to configure Nginx proxy in Docker for SSL encryption protection?

How to configure Nginx proxy in Docker for SSL encryption protection?

Sep 05, 2023 am 08:42 AM
docker nginx ssl

How to configure Nginx proxy in Docker for SSL encryption protection?

How to configure Nginx proxy in Docker to achieve SSL encryption protection?

With the development of the Internet, data security issues have become increasingly prominent. In order to ensure data security, SSL (Secure Sockets Layer) has become an essential encryption protocol. When using Nginx for proxy, configuring an SSL certificate is a basic security operation. This article will introduce how to configure Nginx proxy in Docker to achieve SSL encryption protection.

1. Install Docker and Nginx

First, we need to install Docker and Nginx on the server. You can install it accordingly according to your own operating system and distribution version.

2. Generate SSL certificate

In order to configure Nginx's SSL encryption, we need to generate an SSL certificate. A self-signed certificate can be generated with the following command:

$ openssl req -new -newkey rsa:2048 -days 365 -nodes -x509 -keyout server.key -out server.crt
Copy after login

This will generate a self-signed SSL certificate.

3. Create a Docker image

Next, we need to create a Docker image for Nginx. You can create a new Dockerfile file with the following content:

FROM nginx:latest
COPY nginx.conf /etc/nginx/nginx.conf
COPY server.crt /etc/nginx/server.crt
COPY server.key /etc/nginx/server.key
EXPOSE 80
EXPOSE 443
Copy after login

In this Dockerfile, we copy the Nginx configuration file nginx.conf and the SSL certificates server.crt and server.key to the corresponding directory in the image. And specify the ports that the container needs to listen to: 80 and 443.

4. Configure Nginx proxy

Next, we need to configure Nginx proxy settings. In the nginx.conf file, you can configure it according to the following example:

worker_processes auto;
events {}

http {
  server {
    listen 80;
    server_name example.com;
    location / {
      proxy_pass http://backend;
    }
  }

  server {
    listen 443 ssl;
    server_name example.com;
    ssl_certificate /etc/nginx/server.crt;
    ssl_certificate_key /etc/nginx/server.key;

    location / {
      proxy_pass http://backend;
    }
  }
}

upstream backend {
  server backend_host:backend_port;
}
Copy after login

In the above configuration, first we define an upstream named backend to specify the address and port of the backend server. Then, we configured two server blocks, one listening on port 80 and the other listening on port 443. In the server block of port 443, we used the specified SSL certificate and private key, and set the proxy forwarding address.

5. Build and run the Docker container

Finally, we need to build and run the Docker container.

First, use the following command to build the Docker image:

$ docker build -t nginx-ssl .
Copy after login

Then, use the following command to run the Docker container:

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

This will map the 80 port and 443 port in the container to the corresponding port of the host, and the name of the container is nginx-ssl.

6. Test Verification

Now, we can access the Nginx proxy by entering the server’s IP address or domain name.

Enter http://example.com in the browser, you can see that the Nginx proxy has been successfully configured.

At the same time, we can also try to access through https://example.com. If the browser displays a secure connection, it means that SSL encryption has taken effect.

Summary

Configuring Nginx proxy in Docker for SSL encryption protection is not difficult. By generating SSL certificates, creating Docker images, configuring Nginx agents, and running Docker containers, you can easily add SSL encryption to Nginx to improve data security.

The above is the detailed content of How to configure Nginx proxy in Docker for SSL encryption protection?. 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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

Pi Node Teaching: What is a Pi Node? How to install and set up Pi Node? Pi Node Teaching: What is a Pi Node? How to install and set up Pi Node? Mar 05, 2025 pm 05:57 PM

Detailed explanation and installation guide for PiNetwork nodes This article will introduce the PiNetwork ecosystem in detail - Pi nodes, a key role in the PiNetwork ecosystem, and provide complete steps for installation and configuration. After the launch of the PiNetwork blockchain test network, Pi nodes have become an important part of many pioneers actively participating in the testing, preparing for the upcoming main network release. If you don’t know PiNetwork yet, please refer to what is Picoin? What is the price for listing? Pi usage, mining and security analysis. What is PiNetwork? The PiNetwork project started in 2019 and owns its exclusive cryptocurrency Pi Coin. The project aims to create a one that everyone can participate

How to install deepseek How to install deepseek Feb 19, 2025 pm 05:48 PM

There are many ways to install DeepSeek, including: compile from source (for experienced developers) using precompiled packages (for Windows users) using Docker containers (for most convenient, no need to worry about compatibility) No matter which method you choose, Please read the official documents carefully and prepare them fully to avoid unnecessary trouble.

Deploy JavaEE applications using Docker Containers Deploy JavaEE applications using Docker Containers Jun 05, 2024 pm 08:29 PM

Deploy Java EE applications using Docker containers: Create a Dockerfile to define the image, build the image, run the container and map the port, and then access the application in the browser. Sample JavaEE application: REST API interacts with database, accessible on localhost after deployment via Docker.

How to install Docker extension in vscode Steps to install Docker extension in vscode How to install Docker extension in vscode Steps to install Docker extension in vscode May 09, 2024 pm 03:25 PM

1. First, after opening the interface, click the extension icon button on the left 2. Then, find the search bar location in the opened extension page 3. Then, enter the word Docker with the mouse to find the extension plug-in 4. Finally, select the target plug-in and click the right Just click the install button in the lower corner

WordPress site file access is restricted: Why is my .txt file not accessible through domain name? WordPress site file access is restricted: Why is my .txt file not accessible through domain name? Apr 01, 2025 pm 03:00 PM

Wordpress site file access is restricted: troubleshooting the reason why .txt file cannot be accessed recently. Some users encountered a problem when configuring the mini program business domain name: �...

Why does an error occur when installing an extension using PECL in a Docker environment? How to solve it? Why does an error occur when installing an extension using PECL in a Docker environment? How to solve it? Apr 01, 2025 pm 03:06 PM

Causes and solutions for errors when using PECL to install extensions in Docker environment When using Docker environment, we often encounter some headaches...

How to make PHP5.6 and PHP7 coexist through Nginx configuration on the same server? How to make PHP5.6 and PHP7 coexist through Nginx configuration on the same server? Apr 01, 2025 pm 03:15 PM

Running multiple PHP versions simultaneously in the same system is a common requirement, especially when different projects depend on different versions of PHP. How to be on the same...

How to run the h5 project How to run the h5 project Apr 06, 2025 pm 12:21 PM

Running the H5 project requires the following steps: installing necessary tools such as web server, Node.js, development tools, etc. Build a development environment, create project folders, initialize projects, and write code. Start the development server and run the command using the command line. Preview the project in your browser and enter the development server URL. Publish projects, optimize code, deploy projects, and set up web server configuration.

See all articles