SoFunction
Updated on 2025-03-02

Nginx configuration SSL supports the entire https process (docker version)

Configuring Nginx in Docker to use SSL (i.e. HTTPS) mainly involves several steps:

Prepare SSL certificates and keys, write Nginx configuration files to use these certificates, and run Nginx through Docker containers.

Here is a detailed step-by-step guide:

1. Prepare SSL certificate and key

First, you need to have a pair of SSL certificates (.crt or .pem files) and a key (.key file).

These files can be self-signed (for testing purposes only) or purchased from a Certificate Authority (CA).

If you don't have a certificate yet, you can use OpenSSL to generate a self-signed certificate (for testing only):

openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout  -out 

When executing this command, you will need to fill in some information about your "domain" (although this information is not important for self-signed certificates).

2. Write Nginx configuration file

In Docker, the Nginx configuration file is usually placed outside the Docker image and passed it to the container via the Docker command or the Docker Compose file.

Here is an example of a basic Nginx configuration file that configures SSL:

server {  
    listen 443 ssl;  
    server_name ;  
  
    ssl_certificate /etc/nginx/ssl/;  
    ssl_certificate_key /etc/nginx/ssl/;  
  
    location / {  
        root   /usr/share/nginx/html;  
        index   ;  
    }  
  
    error_page   500 502 503 504  /;  
    location = / {  
        root   /usr/share/nginx/html;  
    }  
}

In this configuration, /etc/nginx/ssl/ and /etc/nginx/ssl/ are the paths to the SSL certificate and keys, and these files need to be placed in the Docker container where Nginx can access.

3. Create a Docker image or use an existing image

  • If you don't have a Docker image for Nginx, you can use the official Nginx image on Docker Hub.
  • But usually you need to add your Nginx configuration file and SSL certificate/key files to the image, or pass them as volumes to the container via the Docker command.

4. Run Nginx using Docker

Here is an example of running an Nginx container using the Docker command and passing an SSL certificate and configuration file:

docker run --name my-nginx \  
    -v /path/to/your/:/etc/nginx/:ro \  
    -v /path/to/your/ssl/:/etc/nginx/ssl/:ro \  
    -v /path/to/your/ssl/:/etc/nginx/ssl/:ro \  
    -p 443:443 \  
    nginx

Please make sure to replace /path/to/your/… with your actual file path.

This command starts an Nginx container that uses the configuration file and SSL certificate/key file you provide.

5. Test HTTPS connection

Now you can test your HTTPS connection by accessing it in your browser (note that you replace it with your actual domain).

If everything is set up correctly, you should be able to access your website safely via HTTPS.

Notice:

  • If you are using a self-signed certificate, the browser may display a warning because the certificate is not issued by a trusted certificate authority.
  • This is normal when testing with a self-signed certificate.

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.