Redirect HTTP requests to HTTPS with Nginx

Redirect HTTP requests to HTTPS with Nginx

I recently moved my blog away from GitHub pages to Ghost, and had to set up HTTPS redirects for the same. This is how you redirect HTTP requests to HTTPS with Nginx.

You will likely have two site configuration files in your /etc/nginx/sites-enabled configuration directory. I have a www.asad.pw.conf and a www.asad.pw-ssl.conf file.

In the non-SSL configuration file, this is what I set:

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

    server_name www.asad.pw;

    location / {
        return 301 https://$server_name$request_uri;
    }
}

**Note: ** You might want to use return 302 until you're sure that the redirection is working as expected because 301 redirects can be hard to clear from your browser.

And that's it! All your http requests will now be redirected to https, along with any query string parameters or URL path details.