Redirecting any HTTP request to HTTPS with NGINX, is as easy as listening to port 80 and redirecting (301 Moved Permanently) to the equivalent HTTPS URL.

The TLS certificate used for HTTPS on vallentin.io, was acquired from Let's Encrypt which is a free SSL/TLS certificate authority.

server {
    listen 80;
    server_name vallentin.io *.vallentin.io;
    return 301 https://vallentin.io$request_uri;
}

server {
    listen 443 ssl;
    server_name vallentin.io *.vallentin.io;

    ssl_certificate     /etc/letsencrypt/live/vallentin.io/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/vallentin.io/privkey.pem;

    add_header Strict-Transport-Security "max-age=31536000";

    # Rest of the Server Configuration
}

Change the domains and filenames as needed.

Redirect www to non-www

Redirecting www to non-www and vice versa, can be done using a similar approach. Instead of being port specific, have a server block that catches www and non-www server_names and then redirect to the desired www or non-www equivalent URL.

The following snippet redirects any HTTP to HTTPS and www to non-www.

server {
    listen 80;
    server_name vallentin.io www.vallentin.io;
    return 301 https://vallentin.io$request_uri;
}

server {
    listen 443 ssl;
    server_name www.vallentin.io;

    ssl_certificate     /etc/letsencrypt/live/vallentin.io/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/vallentin.io/privkey.pem;

    add_header Strict-Transport-Security "max-age=31536000";

    return 301 https://vallentin.io$request_uri;
}

server {
    listen 443 ssl;
    server_name vallentin.io;

    ssl_certificate     /etc/letsencrypt/live/vallentin.io/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/vallentin.io/privkey.pem;

    add_header Strict-Transport-Security "max-age=31536000";

    # Rest of the Server Configuration
}

Change the domains and filenames as needed.

Search Engine Optimization

Google's ranking algorithm takes into account whether a website uses HTTPS. As such in terms of page ranking, it is possible to benefit by using or even better forcing HTTPS.