Skip to content

Instantly share code, notes, and snippets.

@LfJohnVo
Last active January 20, 2023 20:49
Show Gist options
  • Select an option

  • Save LfJohnVo/7b8328ea396f6e24f05a50e3a7c3bc02 to your computer and use it in GitHub Desktop.

Select an option

Save LfJohnVo/7b8328ea396f6e24f05a50e3a7c3bc02 to your computer and use it in GitHub Desktop.
Add ssl certificate to nginx in docker
Step 01: Open ports
Open the docker-compose file (docker-compose.yml) and find Nginx image configurations. add SSL secure ports.
Note 1: Also you need to know, HTTP listen from PORT:80 and HTTP(s) listen from 443
Note 2: If you are using EC2 server to run your docker swarm, make sure that you have enabled HTTPS ports. If not, add a security group by adding HTTPS ports to it.
nginx:
image : your_nginx_image/nginx:latest
ports :
- “80:80”
- “443:443”
Step 02: Mount certificates into Nginx image
You need to copy your certificate files into the Nginx container.
nginx:
image : your_nginx_image/nginx:latest
ports :
- “80:80”
- “443:443”
volumes:
- /data/certs:/etc/nginx/certs
Step 03: Change Nginx configuration file.
Normally your basic configurations look like this. Now we have certifications and keys inside the docker container
server {
listen 80:
server_name www.yoursite.com;
location / {
proxy_pass http://frontend:500
error_log /var/log/front_end_errors.log;
}
}
But if you set up SSL on the application, You have to pass additional parameters and values.
You should listen port which is 80 to 443 and add SSL certificate and Key with it like below.
server {
listen 443 ssl;
server_name www.yoursite.com;
ssl_certificate /etc/nginx/certs/your_site_crt_file.crt;
ssl_certificate_key /etc/nginx/certs/your_site_crt_file.key;
location / {
proxy_pass http://frontend:5000/;
error_log /var/log/front_end_errors.log;
}
}
Additional section:
You should build a Nginx image by adding your new configuration. Your Dockerfile configuration should be like this. While building image we copy Nginx configuration into the image.
FROM nginx
COPY nginx.conf /etc/nginx/conf.d/nginx.conf
After changing the Nginx configuration, build the image with your application and do the docker-compose up.
Now you can access your application via https://www.yoursite.com from a web browser.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment