Linode VPS Hosting - Starting at $19.95 per month

Setting up 301 Redirects in Nginx

This may seem like a long explanation, but I have to bring all of this into context. I have recently started using cloud VPS hosting for both my client's websites and even more recently my own websites. The thing to realize about getting your "own server" is that nothing comes preconfigured and you have to setup everything you want to run on the server. This includes the LAMP stack and any other packages like Sendmail or Postfix for e-mail capabilities or other services. I came from using a shared Hostgator hosting account, which has served me very well up to my 13th multisite on my own server. My clients were another story though. I recommended Hostgator to a client that was looking for budget hosting, and we came to find out later that they will need to upgrade to something like a VPS to get the full capabilities out of their Drupal installation. I didn't want to repeat this situation, so I went with a 256MB cloud server from Rackspace for my next client, which seemed to be a great deal. Rackspace is also known for great uptime.

The particular plan I chose was billed by the hour, so it could easy be used to develop the site and trasfer it to another host later. It was ideal for this client, as we were in the process of signing up for a Chunkhost 512MB Chunk, but later found out that they were above capacity and were waiting for more hardware to arrive. This forced me to seek out another quality hosting service that was well priced and had the horsepower to run an Ubercart store. It did a great job, but only after optimizing the server for 256MB of RAM. This was something that I wasn't aware of coming from a shared hosting environment, but learned fairly quickly that it is very important to running a VPS hosting environment.

I chose the Ubuntu 9.10 distribution and started out by installing the Uncomplicated Firewall to setup my server's security. It is a much easier firewall utility to setup and use than IPTables to secure your Ubuntu server. After that I installed my LAMP stack. I was doing just fine for my first night of development and the server was running like a top. I went to bed that night feeling very accomplished as I setup a really nice web server with Drupal 6 up and running for my client and as well on my way to developing the features on the websites she wanted. The next day I got up and continued working on setting up the Ubercart store. As I continued to work that day, I noticed that the server was getting slower and slower to load my pages. This wasn't ideal as I was trying to build the site and it was slowing me down a little. I looked at my memory usage and it was peaked out and even using swap memory at this point. That wasn't good, and I needed to fix that as soon as possible. I quickly got on the live chat with Rackspace to see what the problem was with my server, and the tech said that my server wasn't optimized properly and that was why my Apache server was pushing my server into swapping.

Swapping is a geek term for using the swap file on your hard disk for memory. This is the Linux equivalent to the Windows page file. They write small blocks of information to the hard disk that would normally be stored in your RAM, but are used when you RAM is full.

This was not what I was envisioning coming from a shared hosting environment like Hostgator that only allowed me 64MB of RAM for my PHP service. I figured with 256MB of RAM the server would run like a hot rod. I thought wrong and it turns out that I needed to tone down the out of the box enterprise scalability of the Apache and MySQL servers. They alone were using up to 80% of my available RAM. It wasn't change to fix the settings, but even after I tweaked my settings to what I found to be the maximum to lower my memory footprint, the Apache web server just used too much RAM. The MySQL server was able to be tamed to a reasonable level, as I was only running 1 database so far. Even with just 1 website running, Apache was spawing process after process each time I connected to the site while I was developing and building out the site. After several pages I was finding myself unable to load a page without waiting 1 or 2 seconds. I guess this works fine on a server with more RAM available, but for a 256MB server Apache just wasn't cutting it. I started looking around to see what I could find that would be a much smaller memory footprint and I found 2 options. The first was Lighttpd. This was great and ran with a much smaller memory footprint overall, but the more research I did on it pointed to a memory leak issue. I kept looking.

I continued my search for the perfect web server for a 256MB VPS server and came across a post about how Nginx used low memory and was fairly easy to use. I decided to give it a shot and fell in love with it immediately. I had to setup PHP-CGI as my PHP processor, but in total, my web server and PHP-CGI processes were using about 1/3 of what Apache was using. It also seems to manage the processes much better, so that my server does not max out it's RAM over any period of time. It is now set to a good level for a small web server and continually has over 75mb of RAM available and never even uses any swap memory at this point. If you are going to be running a low memory server such as a 256MB Rackspace Cloud Server or similar host, you might want to look into running Nginx instead of Apache for your web server.

Now on to the good stuff! I have since been continuing to try and optimize the Nginx web server that I have running. One of the first things that jumped out at me was that obviously my .htaccess files weren't going to be doing anything on the Nginx server. This meant that my 301 redirects that I had setup weren't going to work anymore. I freaked out for a minute, but then figured that there has to be some sort of Nginx 301 redirect available. It turns out that there is! There seem to be multiple ways to do it, but I ended up choosing the if statement. It just seemed more logical to me. Nginx really takes the guessing out of how to redirect what to what, as the redirects are very easy to understand. Forget those outrageous .htaccess redirect statements.

Creating Nginx 301 Redirects

Below you can find a couple ways to create 301 redirects for your Nginx web server.

To create an Nginx 301 redirect from a non-www domain URL to the www domain URL, place the following code inside of  server {} in your config file. Replace your_domain.com with your actual domain.

if ($host = 'your_domain.com' ) {
    rewrite  ^/(.*)$  http://www.your_domain.com/$1  permanent;
 }

To create a Nginx 301 redirect from a www domain URL to the non-www domain URL, place the following code inside of the server {} code block in your site's Nginx config file. Again, replace your_domain.com with your actual domain.

if ($host = 'www.your_domain.com' ) {
    rewrite  ^/(.*)$  http://your_domain.com/$1  permanent;
 }

Alternatively, you can place a new server {} code block inside of your site's Nginx config file and create your Nginx 301 redirects like the following.

server { listen 80; server_name www.your_domain.com; rewrite ^/(.*) http://your_domain.com permanent; }

server { listen 80; server_name your_domain.com; rewrite ^/(.*) http://www.your_domain.com permanent; }

Nginx is an exciting new web server platform that has my full attention, and the attention of many others. It is giving the same, if not more powerful webpage serving with less memory usage than Apache, and is much easier to configure. Hopefully the Apache Foundation is taking notes, as the Nginx web server is amazingly powerful and could take over the market within optimized web server niche. I will definitely be covering more on Nginx including more help like this, so keep it locked. This is new and I feel there are a lot of people who want to learn about it, so I am going to provide as much information as possible.