Redirect traffic from HTTP to HTTPS Using Nginx

- 1 min

Let’s redirect all http traffic to https thus making all traffic more secure while getting a little extra love from Google.

Updated your configuration file

Add this to our current configuration file or make a new one.

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

Always test your configuration

ALWAYS check your configuration before reloading nginx. ALWAYS!

sudo service nginx configtest
# or
sudo /usr/sbin/nginx -t

For such settings to take effect you will have to reload Nginx again with the command:

sudo service nginx reload
# or
sudo /usr/sbin/nginx -s reload

Check the redirect is working as expected

We can use curl -I url-to-test to do this quickly and easily.

$ curl -I http://www.domain.com/about

And we should see something like this:

HTTP/1.1 301 Moved Permanently
Date: Tue, 15 Mar 2016 23:11:15 GMT
Content-Type: text/html
Location: https://www.domain.com/about
...
Sergei Filippov

Sergei Filippov

Web, development, family & life.

rss facebook twitter github youtube mail instagram linkedin google