Skip to content

Instantly share code, notes, and snippets.

@v0lkan
Created October 1, 2022 01:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save v0lkan/3a67529433ef5f50442b63448b7c16fd to your computer and use it in GitHub Desktop.
Save v0lkan/3a67529433ef5f50442b63448b7c16fd to your computer and use it in GitHub Desktop.
Add Self-Signed Certificate to Enable SSL on NGINX

Based on this Digital Ocean tutorial

Create an /etc/nginx/snippets directory if it doesn’t exist:

sudo mkdir /etc/nginx/snippets

Create a self signed .key and .crt file first:

sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 \
-keyout /etc/ssl/private/nginx-selfsigned.key \
-out /etc/ssl/certs/nginx-selfsigned.crt

Then do some Diffie-Heilmann-fu:

sudo openssl dhparam -out /etc/ssl/certs/dhparam.pem 2048

Create an /etc/nginx/snippets if it doesn’t exist:

sudo mkdir /etc/nginx/snippets

Then put these two files there:

# /etc/nginx/snippets/self-signed.conf
ssl_certificate /etc/ssl/certs/nginx-selfsigned.crt;
ssl_certificate_key /etc/ssl/private/nginx-selfsigned.key;
# /etc/nginx/snippets/ssl-params.conf

# from https://cipherli.st/
# and https://raymii.org/s/tutorials/Strong_SSL_Security_On_nginx.html

ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_ciphers "EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH";
ssl_ecdh_curve secp384r1;
ssl_session_cache shared:SSL:10m;
ssl_session_tickets off;
ssl_stapling on;
ssl_stapling_verify on;
resolver 8.8.8.8 8.8.4.4 valid=300s;
resolver_timeout 5s;
# Disable preloading HSTS for now.  You can use the commented out header line that includes
# the "preload" directive if you understand the implications.
#add_header Strict-Transport-Security "max-age=63072000; includeSubdomains; preload";
add_header Strict-Transport-Security "max-age=63072000; includeSubdomains";
add_header X-Frame-Options DENY;
add_header X-Content-Type-Options nosniff;

ssl_dhparam /etc/ssl/certs/dhparam.pem;

Then update your /etc/nginx/nginx.conf to use these values:

    …
    server {
        # listen       80;
        # listen       [::]:80;

	      listen 443 ssl http2;
	      listen [::]:443 ssl http2;
	      include snippets/self-signed.conf;
	      include snippets/ssl-params.conf;
     …   
     }
     …

That’s about it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment