DEV Community

Roberto Conte Rosito
Roberto Conte Rosito

Posted on • Originally published at blog.robertoconterosito.it on

Setup HTTPS with Nginx and Docker

Welcome back to my dev notes!

Today I was in trouble for a while because I wanted to setup HTTPS with Nginx and Docker and I completely forgot how to do it. So I decided to write down this guide to help me in the future and maybe it will help you too.

I have a Docker container running a PHP application served by Nginx and I wanted to setup HTTPS with a self-signed certificate because I need to test some OAuth flows.

Follow these steps to setup HTTPS with Nginx and Docker:

Create a self-signed certificate

openssl x509 -outform pem -in localhost.crt -out localhost.pem

Enter fullscreen mode Exit fullscreen mode

Edit nginx configuration

Please take attention to the ssl_certificate and ssl_certificate_key files paths.

server {
 listen 443 ssl;
 server_name localhost;
 ssl_certificate /etc/nginx/ssl/localhost.crt;
 ssl_certificate_key /etc/nginx/ssl/localhost.key;
 ...
}

Enter fullscreen mode Exit fullscreen mode

Restart docker

If you are a perfectionist like me you can also add the certificate to your system keychain to avoid browser warnings. Using Mac OS you can do it like this:

sudo security add-trusted-cert -d -r trustRoot -k /Library/Keychains/System.keychain localhost.crt

Enter fullscreen mode Exit fullscreen mode

Enjoy!

Billboard image

Deploy and scale your apps on AWS and GCP with a world class developer experience

Coherence makes it easy to set up and maintain cloud infrastructure. Harness the extensibility, compliance and cost efficiency of the cloud.

Learn more

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more