NGINX Redirect Tutorial

We recently assigned a few custom domains to rampages. That ended up causing two complications. The first was that some people were using https associated URLs previously (news to me). The always impressive Tim Owens (reclaim hosting support cannot be complimented enough) took care of that for me.

The second was the need to redirect some file level1 URLs to some new subdomains. I plain English, I needed to go to In any case, Tim gave me some tips and I sallied forth to do the redirects.2 This was my first time doing anything in NGINX so it was a learning experience. I document the steps here in the hope that it’ll be useful to someone else.

Keep in mind I’ve only done this on my server so I’m assuming it’s fairly broadly applicable but I can’t be sure of that. I also attempted to either explain or link to things that might be helpful to beginners (like myself).

Enter Terminal Land

Launch Terminal. I’m using iTerm thanks to Mark Luetke.

I’m using a pem file to authenticate which is nice. Read more about pem authentication here. Do keep in mind you need your pem file permissions to be -rw—— (400 I think) even on your local computer or you’ll get denied. Read about chmod here if you need to.

ssh -i /Users/yourname/location/identity.pem

Now you should be snug in the warm embrace of your server. We wish to go to where the NGINX files live.

In my case that’s done like so . . .

cd /etc/nginx/sites-enabled

My file is called wordpress and I want to open it in Nano and I want to do that with sudo privliges. If you don’t, you’ll find any changes you try to make will not save.

sudo nano wordpress

Your next goal is to find the server block that deals with the domain you’re attempting to deal with. In my case I was looking for something with server_name up near the top. The “block” information should be indented and within curly brackets (these things { }) so it’s fairly easy to scan. I use ctrl+V to move down pages.

Once you find the right block, you’ll go to the bottom of the text and write your if statements. I was doing two redirects and wrote the following (making sure it was within the closing curly bracket).

if ( $request_filename ~ focused-inquiry/ ) {
        rewrite ^ permanent;
if ( $request_filename ~ core-curriculum-bis/bis-interdisciplinary-studies/ ) {
        rewrite ^ permanent;

With those changes made, you needed to save the file and exit Nano.


You should now be back in terminal and you need to reload NGINX. IT IS ESSENTIAL that you do this as sudo. If not, it will fail. The indicator of this failure may not be obvious (particularly if your terminal window is very wide and you might spend a good deal of time trying to fix something that was never applied3).

sudo service nginx reload

The changes should apply immediately.

1 I’m not sure that’s the right terminology

2 I was initially confused about whether I was doing this in htaccess or not. Some of our sites are still using Apache and htaccess. Rampages is not. I was also unclear previously about whether htaccess had any connection to NGINX or not. It does not. 🙂 Put this in my public failure CV.

3 Another failure for a real failure CV