HTTPS for Nginx on Debian and Ubuntu

Last Updated:


These commands were tested on default installations of Debian 11 and Ubuntu 20.04 in May 2022. If you experience issues, Contact Ryan and report an issue

(Optional) Upgrade Packages

Make sure your machine has the latest packages installed and reboot.

    sudo DEBIAN_FRONTEND="noninteractive" apt-get update;
    sudo DEBIAN_FRONTEND="noninteractive" apt-get --with-new-pkgs upgrade -y;
    sudo reboot;

Upload Site Files

For example purposes, we'll add one static file. You can download your own site's files instead if you have some.

    sudo mkdir -p /var/www/;
    echo "


" | sudo tee /var/www/;

Generate Diffie-Hellman Parameters

We generate a new set of parameters unique to your machines. This improves security because usually a default set of DH parameters are used by everyone that installs Nginx. We'll use this file in a little bit.

# Generate Diffie-Hellman Parameters
sudo openssl dhparam -out /etc/ssl/dhparam.pem 2048;

Copy TLS Certificate Files

Here we copy the needed TLS key and certificate files from previous lessons. Then we ensure the permissions are correct on both files. You might need to upload your certificate files onto the machine if you bought a certificate or generated a free certificate on another machine.

    # Edit the file names to something that
    # makes sense in your situation.

    # Copy Your TLS Key
    cp MY_CERTIFICATE.key /etc/ssl/private/MY_CERTIFICATE.key;
    chmod 600 /etc/ssl/private/MY_CERTIFICATE.key;

    # Copy Your TLS Certificate
    cp MY_CERTIFICATE.pem /etc/ssl/certs/MY_CERTIFICATE.pem;
    chmod 644 /etc/ssl/certs/MY_CERTIFICATE.pem;

Installing Nginx

We install Nginx and remove one of its default site configuration files. We'll be adding our own site configuration soon, and don't need the default.

    # Install Nginx
    sudo apt-get install -y nginx;
    sudo rm /etc/nginx/sites-enabled/default;

    # Want to Check Nginx Version?
    # nginx -v;
    # Our version: nginx/1.18.0

HTTP to HTTPS Redirect

For our first site configuration, we redirect any insecure HTTP traffic on port 80 to HTTPS. Pay close attention to the highlighted lines and make changes so it matches your domain.

    # Filename: /etc/nginx/sites-available/
    server {
      listen       80;
      charset      utf-8;
      root         /var/www/;
      index        index.html index.htm;

      # Redirect to HTTPS
      return       301$request_uri;

HTTPS Configuration

Now we add the main HTTPS site configuration. Here's the full configuration file, and we'll break it down section by section. Pay close attention to the highlighted lines and make changes so it matches your domain.

Lines 17-20: For enhanced security at the risk of your site breaking for some visitors, you can use the stronger ciphers instead.

Line 24: We're specifying Nginx should use the unique DH Parameters we generated earlier.

Line 30: When you're sure everything is working, you can enable this line to increase your site's grade on Qualys' SSLLabs from an "A" to an "A+" rating.

    # Filename: /etc/nginx/sites-available/
    server {
        listen       443 ssl;
        charset      utf-8;
        root         /var/www/;
        index        index.html index.htm;

        # SSL/TLS Settings
        ssl_protocols              TLSv1.2 TLSv1.3;
        ssl_certificate            /etc/ssl/certs/MY_CERTIFICATE.pem;
        ssl_certificate_key        /etc/ssl/private/MY_CERTIFICATE.key;
        ssl_session_timeout        1d;
        ssl_session_cache          shared:SSL:50m;
        ssl_session_tickets        off;
        ssl_prefer_server_ciphers  on;
        # For better security, you can use the below strong ciphers instead
        # But beware, it might break the site for some of your visitors

        # Generate with:
        # openssl dhparam -out /etc/ssl/dhparam.pem 2048;
        ssl_dhparam /etc/ssl/dhparam.pem;

        # Optional
        # Forces TLS for all visitors for 1 year
        # Only enable this when you are sure everything is working well
        # Required for an "A+" Grade
        #add_header 'Strict-Transport-Security' 'max-age=31536000; includeSubDomains; preload' always;


Enabling the Sites

Here we "link" the HTTP and HTTPS configuration files (with symlinks) to tell Nginx we want to enable these site configurations. If you're not familiar with symlinks, think of them like creating a shortcut to another file.

    sudo ln -s "/etc/nginx/sites-available/"  /etc/nginx/sites-enabled/;
    sudo ln -s "/etc/nginx/sites-available/" /etc/nginx/sites-enabled/;

Testing Our Configuration

It's always a good idea to check our configurations and see if there are any errors. We do this with the command "nginx -t" (the "t" is for "test").

    # Test the Nginx configuration
    sudo nginx -t;

    # Output should be similar to:
    # nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
    # nginx: configuration file /etc/nginx/nginx.conf test is successful

Starting Nginx

We now need to make sure Nginx is running. First, we make sure Nginx is "enabled" which means Nginx will start on boot of the machine. Then, we "restart" Nginx which will restart it if it's running or if it's not running, start it. We prefer "restart" because it works in either case.

    # Run Nginx automatically at boot by "enabling" it
    sudo systemctl enable nginx;

    # Start or Restart Nginx Now
    sudo systemctl restart nginx;

You're Done!

Your new site should be available and running. To test it in your browser, make sure you set up a DNS "A" Record on your domain that contains your machine's IP. When you visit the domain, the site should load correctly and should also be ready to test on Qualys SSLLabs where you'll get an "A" rating! (or "A+" if you enabled the setting in the configuration).