Home >Backend Development >PHP Tutorial >How to configure Nginx proxy server in Docker container to support HTTPS access to web services?
How to configure Nginx proxy server in Docker container to support HTTPS access to web services?
With the rapid development of the Internet, data transmission security issues have attracted more and more attention. In order to protect the data transmission security of web services, many websites have begun to use the HTTPS protocol for encrypted transmission. In the development and deployment process, using Docker containers to build and manage web services has become a common way. This article will introduce how to configure the Nginx proxy server in a Docker container to support HTTPS access to web services.
Step 1: Generate SSL certificate
First, we need to generate an SSL certificate to ensure the integrity and security of encrypted transmission. We can use self-signed certificates for testing and development purposes. Assume that the certificate files we want to generate are server.crt
and server.key
respectively.
Step 2: Create Dockerfile
Before starting, create a Dockerfile to build our Nginx proxy server container.
FROM nginx:latest # 复制SSL证书 COPY server.crt /etc/nginx/conf.d/server.crt COPY server.key /etc/nginx/conf.d/server.key # 复制Nginx配置文件 COPY nginx.conf /etc/nginx/conf.d/default.conf # 暴露HTTPS端口 EXPOSE 443
In the Dockerfile, we build our custom image from the official Nginx image. Then, we copy the SSL certificate file to the /etc/nginx/conf.d/
directory, and copy the Nginx configuration file to the /etc/nginx/conf.d/
directory Down. Finally, we declare the port to be exposed through the EXPOSE
command, here is port 443 (HTTPS default port).
Step 3: Create Nginx configuration file
Next, we need to create an Nginx configuration file to configure the proxy server. We can create a file named nginx.conf
locally and paste the following content into it:
server { listen 443; server_name yourdomain.com; ssl on; ssl_certificate /etc/nginx/conf.d/server.crt; ssl_certificate_key /etc/nginx/conf.d/server.key; location / { proxy_pass http://web-service-container: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; } }
In the above code, we have defined a server block that listens on port 443. Note that the server_name
directive should match your domain name. We enable SSL support through the ssl on
command and specify the path to the SSL certificate.
Then, in the location /
directive, we forward the incoming request to port 80 in the container named web-service-container
. You can replace this name with the container name or IP address you want to proxy.
Step 4: Build and run the Docker container
Now we can build and run our Docker container through the following command:
docker build -t nginx-proxy . docker run -d -p 443:443 --name nginx-proxy-container nginx-proxy
The above command will build a container named Docker image of nginx-proxy
and run the image as a container named nginx-proxy-container
. We map port 443 of the container to port 443 of the host.
So far, we have successfully configured the Nginx proxy server in the Docker container to support HTTPS access to the web service. You can now access your web service by visiting https://yourdomain.com
.
Summary
This article describes how to configure the Nginx proxy server in a Docker container to support HTTPS access to web services. By using SSL certificates and Nginx configuration files, we are able to protect the data transmission security of the web service. This Docker-based approach not only facilitates the development and deployment process, but also provides powerful code isolation and scalability. I hope this article is helpful to you, thank you for reading!
The above is the detailed content of How to configure Nginx proxy server in Docker container to support HTTPS access to web services?. For more information, please follow other related articles on the PHP Chinese website!