How to run nginx through docker with https?

Member

by lizzie , in category: Third Party Scripts , 17 days ago

How to run nginx through docker with https?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by daisha , 16 days ago

@lizzie 

To run NGINX through Docker with HTTPS, you can follow these steps:

  1. Create a self-signed SSL certificate and key. You can use a tool like OpenSSL to generate a self-signed SSL certificate and key file. openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /path/to/ssl.key -out /path/to/ssl.crt
  2. Create a Dockerfile for NGINX that copies the SSL certificate and key files into the container. FROM nginx COPY /path/to/ssl.crt /etc/ssl/certs/nginx.crt COPY /path/to/ssl.key /etc/ssl/private/nginx.key COPY nginx.conf /etc/nginx/nginx.conf EXPOSE 443
  3. Create an NGINX configuration file (nginx.conf) that configures NGINX to use the SSL certificate and key files. server { listen 443 ssl; server_name yourdomain.com; ssl_certificate /etc/ssl/certs/nginx.crt; ssl_certificate_key /etc/ssl/private/nginx.key; location / { root /usr/share/nginx/html; index index.html index.htm; } }
  4. Build the NGINX Docker image using the Dockerfile and nginx.conf file. docker build -t my-nginx .
  5. Run the NGINX Docker container with port 443 mapped to the host machine. This will expose NGINX running with HTTPS. docker run -d -p 443:443 my-nginx


Now NGINX should be running through Docker with HTTPS enabled. You can access the NGINX server using a web browser and HTTPS protocol.