Rewriting URLs with Nginx

Rewriting URLs with Apache HTTPD can be pretty ugly. With Nginx it is a breeze. Take a look to this example:

server {
 listen 80;
 rewrite ^/(.*)$1 permanent;

This will not just redirect your old URL to your new URL. It passes all the parameter to your new URL, too. If you are migrating a Web Application to new URL, this what you want. All old bookmarked URL are still working with this.

Load balancing with Nginx And Unicorn

Nginx is a pretty awesome lightweight Web Server for Linux. You can use it to deliver static web content. But you can use at as a load balancer, too. If you have a Ruby on Rails Application running on multiple unicorn servers, than Nginx can do the load balancing for the unicorn servers.

Just add the following lines to your “/etc/nginx/conf.d/default.conf”:

upstream unicorns { 
  server XXX.XXX.XXX.XXX:8080; 
  server <IP_ADDRESS>:<PORT>; 

XXX stands for an IP Address. You can add as many server lines as you want. The “ip_hash” is not mandatory. This just if you want to force nginx to send the requests from a particular user every time to the same app server. If your Web Application is REST ful and Stateless you don’t need it. Otherwise you should add the line.

Now you just have to add one block for your domain:

server {
  listen 80;
  location / { 
    proxy_pass http://unicorns; 

And that’s it.

One more thing. If you want limit the file upload size or you have problems with big HTML5 AJAX requests, you should add a little bit more. Here is a more complete example:

server {
  listen 80;
  client_max_body_size 1M; 
  proxy_buffer_size 128k; 
  proxy_buffers 4 256k; 
  proxy_busy_buffers_size 256k; 
  location / { 
    proxy_pass http://unicorns; 

Bad Gateway with NGinx & Unicorn

I am using NGinx as proxy to load balance the traffic. Behind Nginx their are some unicorn servers. On Friday I did some changes on the server and I realized that I am always running into a Bad Gateway error. The way from Nginx to unicorn worked. But the way back not really.

I googled for it and I found out that the problem is the buffer size on NGinx. After setting the buffer sizes up like this here, it worked fine for me.

proxy_buffer_size   128k;
proxy_buffers   4 256k;
proxy_busy_buffers_size   256k;

You have to add this to your default.conf file in “/etc/nginx/conf”.

Installing Nginx 1.0.2 on Yellow Dog Linux

I just got an Yellow Dog Linux (YDL) up and running on Amazon EC2. YDL is based on Red Hat. The installation tool is called “yum”. It is similar to debians “apt-get” tool, but it is based on RPMs.

Anyway. You can install nginx via yum with this command:

sudo yum install nginx

But this is not the newest version. If you want the newest version you should install it from the source code. But at first you need to install some additional tools.

sudo yum install gcc
sudo yum install pcre
sudo yum install pcre-devel
sudo yum install zlib
sudo yum install zlib-devel
sudo yum install make

Now you can download the newest version with weget from the nginx page:

After the download you should unpack the *tar.gz file und navigate into the directory. Now you can install it with this 3 commands:

sudo ./configure
sudo make
sudo make install

If everything works fine it is installed here:


Now you can add “/usr/local/nginx/sbin” to your PATH or you just create a symlink in “/usr/bin”

cd /usr/bin
ln -s /usr/local/nginx/sbin/nginx

For security reason you should create a nginx group.

sudo useradd -s /sbin/nologin -r nginx
sudo usermod -a -G nginx <USERNMAE>

And you should have a place where you deploy your webapps.

sudo mkdir /var/www
sudo chgrp -R nginx /var/www
sudo chmod -R 755 /var/www

Now you can start nginx with this command:

sudo nginx

And stop it with this command:

sudo nginx -s stop