NGINX virtual host with redirects from http to https and force www

When you want to host a website with nginx and you need SSL certificate you can use certbot to generate the certificate.

Generate SSL certificate

#!/bin/bash

set -e -o pipefail

DOMAIN="example.com"              # change to your domain
EMAIL="myemail@example.com"       # change to your email address

sudo certbot certonly \
  --manual \
  --preferred-challenges=dns \
  --email kisphp+${DOMAIN}@gmail.com \
  --server https://acme-v02.api.letsencrypt.org/directory \
  --agree-tos \
  -d "*.${DOMAIN}" \
  -d $DOMAIN

For this command you'll need to have access to the DNS for the domain and add the _acme-challenge.{domain} as TXT record.

Create virtual host for www

server {
    server_name www.example.com;

    root /path/to/server/example.com/public;

    index index.php;

    location / {
        # try to serve file directly, fallback to index.php
        try_files $uri /index.php$is_args$args;
    }

    location ~* \.(?:css|js)$ {
        expires 1h;
        add_header Cache-Control "public";
    }

    # PROD
    location ~ ^/index\.php(/|$) {
        fastcgi_pass unix:/var/run/php/php-fpm.sock;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;

        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        fastcgi_param DOCUMENT_ROOT $realpath_root;

        internal;
    }

    # return 404 for all other php files not matching the front controller
    # this prevents access to other php files you don't want to be accessible.
    location ~ \.php$ {
        return 404;
    }

    # Deny access to hidden files
    location ~ /\. {
        access_log off;
        log_not_found off;
        deny all;
    }

    error_log /var/log/nginx/example-com-error.log;
    access_log /var/log/nginx/example-com-access.log;

    listen 443 ssl http2; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}

server {
    if ($host = www.example.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    listen 80;
    listen 

    server_name www.example.com;
    return 404; # managed by Certbot
}

Create virtual host for redirects to www

server {

    server_name example.com;

    error_log /var/log/nginx/example-com-error.log;
    access_log /var/log/nginx/example-com-access.log;

    listen 
    listen 443 ssl http2; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

    return 301 https://www.example.com$request_uri;
}
server {
    if ($host = example.com) {
        return 301 https://www.example.com$request_uri;
    }

    listen 80;
    listen 

    server_name example.com;
    return 404; # managed by Certbot
}

Create symbolic links for the domains

ln -s /etc/nginx/sites-available/www.example.com /etc/nginx/sites-enabled/www.example.com
ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/example.com

Test nginx configuration

nginx -t

If everything is fine, restart nginx

/etc/init.d/nginx restart

Done!