Welcome to the most active Linux Forum on the web.
Go Back > Articles > Jeremy's Magazine Articles
User Name


By jeremy at 2008-05-08 09:52
Take Off With Nginx
Linux Magazine
Jeremy Garcia

In the market for a high performance HTTP server and reverse proxy server? Take a look at "Engine X."

Nginx, pronounced "Engine X", is a high performance HTTP server and reverse proxy that is also able to proxy IMAP and POP3. Written by Igor Sysoev and licensed under a BSD variant, the official site is here.

While most of the official site and documentation is in Russian, an English language wiki is available. Nginx has a modular architecture and can serve static content extremely quickly using a high number of concurrent connections. It does accelerated reverse proxying without caching.

If a caching web accelerator is more what you’re looking for, you might look into Varnish, which we covered recently. (See Getting Glossy with Varnish.) Additional HTTP features include simple load balancing, fault tolerance, ACLs, flexible rewriting, FastCGI, and SSL.

Nginx also supports a variety of mail proxy features such as user redirection to IMAP/POP3 backends using an external HTTP authentication server, user authentication using an external HTTP authentication server, SSL, STARTTLS, and STLS. The mail proxy functionality is used in some very high volume implementations, including FastMail.FM and Zimbra.

Setting Up Nginx

Let’s get started with Nginx. After you download the latest tarball from the Nginx site, you can install nginx as follows:

$ ./configure --user=daemon --group=daemon && make

# make install
You’ll need to adjust the configuration flags depending on the functionality you plan on utilizing. For instance, if you’d like to use regular expressions in your configuration file, you’ll need to point configure to the PCRE library.

The default installation path for nginx is /usr/local/nginx. With nginx installed, it’s time to create a configuration file (default: /usr/local/nginx/conf/nginx.conf). This configuration file will serve static content for a single virtual host:

worker_processes  2;


error_loglogs/error.log  info;

events {

    worker_connections  1024;

    use epoll;


http {






    gzip_typestext/html text/plain txt/xml text/css application/x-javascript;

    server {


	access_loglogs/domain1.access.log main;

	location / {





You can use a regular expression along with the deny directive to block access to all subversion directories.

location ~ /.svn {

    deny  all;

You may want to serve all images with Expires and Cache-Control headers that will allow caching at the browser level, while not clogging the access log.

location ~* ^.+.(jpg|jpeg|gif|png|)$ {

    access_log        off;

    expires           30d;

To perform simple load balancing between three servers, one of which is more powerful. This will allow you to do that:

upstream my_cluster {

    server weight=3




server {

    listen    80;;

    access_log	logs/www.domain.access.log main;

    location / {


	proxy_set_headerHost $host;

	proxy_set_headerX-Real-IP $remote_addr;

	proxy_set_headerHostX-Forwarded-For $proxy_add_x_forwarded_for;


Once you’ve finalized your configuration, you should test the syntax with nginx -t. You’re now ready to start serving content. Don’t forget to add nginx to your startup scripts. One nice feature of nginx is that you can upgrade to a new binary without losing a single incoming request.

To accomplish this, replace the old nginx binary with a new one. Then send a USR2 signal to the nginx master process, which will rename its .pid file to .oldbin and then execute the new binary. You can now phase out the old binary by sending its master process a WINCH signal. Once all old worker process are done you can send the old master process a QUIT.

Nginx allows you to quickly and easily serve static content, while giving you the flexibility to proxy dynamic content and other requests to any number of backends. If you have a rapidly growing site, its small memory footprint, secure code and high concurrency could be a welcome addition to your infrastructure.

by syg00 on Fri, 2008-05-09 19:44
Deleted - not really relevant to the article.


All times are GMT -5. The time now is 04:48 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration