Skip to content

Nginx

August 8, 2012

Purpose of Nginx

Nginx is an open source virtual server which allow you to redirect a request coming into a web server to either another port on the same server to to a completely different host entirely.

What I used it for in the past

I used Nginx to route requests coming into port 80 on a server to another port (8000) on the same server where a python django web application was running.

Installation

Red Hat Linux:
$ sudo rpm -Uhv nginx-release-rhel-5-0.e15.ngx.noarch.rmp
$ sudo yum install nginx
Debian Linux:
$ apt-get update
$ apt-get upgrade --show-upgraded
$ apt-get install nginx
Other:

Guide to other installation methods

Basic Nginx Commands

These are commands that you would run from a linux command line to control Nginx.

 Start Nginx – Needed after installation
$ sudo /usr/sbin/nginx
Stop Nginx
$ sudo /usr/sbin/nginx -s stop
Restart Nginx – done when configuration settings are changed
$ sudo /usr/sbin/nginx -s reload

Location of the Configuration File

$ sudo nano /etc/nginx/conf.d/default.conf

Routing Requests to another port

server {
      listen 80;
      server_name localhost;

      location /test {
            # add proxy headers
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_set_header X-NginX-Proxy true;
            # Here is where you specify where to route the http request to.
            proxy_pass http://127.0.0.1:8000/;
            proxy_redirect off;
      }

      location / {
          # add proxy headers
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_set_header Host $http_host;
          proxy_set_header X-NginX-Proxy true;
          # Here is where you specify where to route the http request to.
          proxy_pass http://127.0.0.1:8001/;
          proxy_redirect off;
      }

      error_page 500 502 503 504 /50x.html;
      location = /50x.html {
           root /usr/share/nginx/html;
      }
}

In the above code you can see that Nginx is listening to requests coming in from port 80 (http) of the localhost (127.0.0.1). Say the web address of the server nginx is running on is at http://something.com/, everything after the final forward slash will then be compared to each location variable withing the server brackets and then execute that code inside that location bracket. What this means is that if you were to send a request to the server by typing in the url http://something.com/test and nginx was listening on port 80, then everything after the “http://something.com/” would be taken and compared to the content after each location tag. In the case above that would be “/test” and “/” in that order. Once a match is found or it comes across the first “/” tag then it will execute the commands inside that location tags brackets. so in the case of http://something.com/test, the request would be routed to http://127.0.0.1:8000/ in every other case the request would be routed to http://127.0.0.1:8001/.

Routing Requests to another Server

To route a request to another server you would follow the same format as in “Routing Requests to another port” above. Except in this case, you would change the proxy_pass variable value to be a different host and port.

In addition. You dont need any of the proxy_set_header attributes. These can be removed.

Routing Requests to get Resources on the Server

server {
     listen     80;
     server_name  localhost;

      location / {
            root /usr/assets/;      #This is the location that any requests will be routed to.
      }

      error_page    500 502 503 504 /50x.html;
      location = /50x.html {
           root   /usr/share/nginx/html;
      }
}

This is again similar to the “Routing Requests to another port” section but instead of using an attribute like proxy_pass to redirect the requests path you instead simply list the user that you want the request to have the permissions (in this case root) of and the path on the server you want to route the request to (in this case /usr/assets/).
So if you had a file at the location /usr/assets/picture.png and you sent a request to this server with the url http://something.com/picture.png, the image would appear in the browser.

Useful Links

http://en.wikipedia.org/wiki/Nginx
http://wiki.nginx.org/Install

Advertisements

From → Installation

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: