Redirecting Requests with NGinx: Using NGinx configuration to quickly redirect traffic

A while back, I announced that I had moved my blog over to HTTPS. What about HTTP though? Do I serve content over HTTP still, or did I doom existing links to be forever broken?

As you could guess by the title of this post, neither of those was satisfactory. Instead, I added four lines to our NGinx configuration telling it to listen on port 80 and respond to every request with a permanent redirect pointing to the same path under

Beware, Thar Be Dragons

Before getting to the configuration, I’d like to take just a second to look at the status code we’re sending back though: 301 Moved Permanently. The keyword there is permanent. Unlike its sibling 302 Found, when we send this response back to a browser or to a crawler, it’s going to remember the response until someone clears a cache.

This also means you probably want to test this outside production. An accidental but permanent redirect is just a permanent as an intentional one. A misconfiguration here could essentially cause your site to become permanently unreachable.

TL;DR: Permanent is permanent. You don’t get to change it after the fact. Don’t test this in production.


So, where were we? Ah yes, the good part. The actual configuration. In my case, I added the following lines to my site configuration file in /etc/nginx/sites-enabled/default.

server {
	listen 80 default_server;
	return 301$request_uri;

And with that, we now are redirecting all of our existing HTTP traffic to HTTPS.