Use the Nginx Proxy and Web Server to Boost Performance

Nginx is said to be fast. Apache is said to be powerful. For that reason Nginx is often used to serve up static content while Apache and other servers are given the work that requires heavy lifting.

Nginx is an open source proxy and web server that is used for serving up static content super-fast. It uses less memory that Apache and does not have the same memory-related capacity issues as Apache. Dropbox, Netflix, and Facebook all use Nginx. The Nginx Company says that 40% of the web sites at Amazon AWS use Nginx too.

What Nginx does best is proxy http traffic to application servers and Apache. It does not have all the plugin modules that Apache has—such as mod_jk and mod_php to execute php scripts or attach to Tomcat or other application servers—so Apache, FastCGI, or other servers and processes are usually given that task.

The problem with Apache is that it consumes a lot of memory, under heavy load it begins to slow down or just run out of resources. When it hits the limit set by the administrator, it will refuse new connections. If Apache is configured in pre-fork mode, then one process is spawned for each user connection. If it is configured in worker mode, then when a new process is spawned, it can handle more than one user, because it creates multiple threads inside the one process. Pre-forked mode uses more memory; worker mode can only use thread-safe modules, so it cannot (or should not be configured) load, for example, process mod_php.

Nginx handles processes and threads differently. You set it up in a master-slave configuration with the Nginx process running a configurable number of worker (slave) processes. Each of these worker processes handle multiple web requests. The gain in efficiency over Apache comes from by handling multithreading in a better manner. (Nginx is fairly new, Apache has been around since 1995. So the Nginx programmers have had some time to come up with better [new] algorithms based on what has been learned and observer in the older product and as more has been learned about server I/O in general.)

The open-source version of Nginx is free. You can also purchase Nginx Plus, which includes support. Nginx allows 10 times as many users as the free product, says Nginx. (That’s sort of annoying to put some kind of governor on the free version.)
Nginx has grown in popularity. It is now the #2 web server used on the internet says Digital Ocean. Nginx has attracted venture capital funding, as the open source project wants to become a money-making business.

Nginx includes these features:

• Low memory footprint Web server
• Proxy for PHP. Ruby, Java (application servers), FastCGI, uwsgi, and HTTP (web) servers
• Supports Memcache and WebSocket
• Provides edge caching and HTTP compression
• Provides SSL and SPDY acceleration
• Load Balancing
• On-the-fly configuration
• Remote syslog logging for security and performance monitoring
• Video streaming of different formats
• Export performance monitoring information in JSON format in real time

Let’s briefly explore some of these features and show how easy it is to set up. (The Apache syntax is fairly easy too.)

Load Balancing
Like most load balancing hardware, Nginx software supports these load balancing configurations:
• Round-robin—pick backend servers in rotation
• Least-connected—pick the least busy server
• Ip-hash—used when the user connection should be sticky, meaning the application requires that the user session is processed by the same back end server so that it does not, for example, lose a session cookie that is tracking what the user wants to do next.
The configuration is made with an easy-to-understand, Java-like curly-brace (“{}”) syntax. Here we show a couple of examples.

FastCGI
Like Apache, Nginx has a various set of modules that you load at runtime.
One of them is the ngx_http_fastcgi_module. It lets Nginx proxy requests to a FastCGI server to handle PHP, Ruby, .Net, Perl, and other programming languages. FastCGI is an improvement over regular CGI, because it reuses CGI processes instead of creating new ones.
Here is a sample configuration:
server {
location / {
fastcgi_pass localhost:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param QUERY_STRING $query_string;
}

location ~ .(gif|jpg|png)$ {
root /data/images;
}
}
This request sends all requests except images to the FastCGI server running on port 9000. It uses regular expressions to parse the HTTP headers and content to determine what to send where.

Proxy Server
A proxy server is one that passes traffic through itself to another server. Both Nginx and Apache can function as a proxy servers. The proxy server generally decides where to traffic depending on the source ip addresses, type of request (image, video, PHP), specific URIs, and other. It has the ability to process, for example, static content (meaning ordinary HTML pages, with no buttons or input fields) and pass through requests for dynamic content to a back end server.
Here is a sample configuration for how to use Nginx as a proxy server to serve up static content on Nginx and pass through other requests to a back end server.

server {
location / {
proxy_pass http://localhost:8080/;
}

location ~ .(gif|jpg|png)$ {
root /data/images;
}
}
In this case image files (.gif, jpg, or png) are retrieved from the local file system /data/images and all other requests are sent to the server listening on port 8080. (It makes sense to store the images on the edge of the application and retrieve them from storage there instead of making another hop across the infrastructure.)

SSL
Nginx server can offload the work of SSL decryption and encryption work; so it is an SSL accelerator. This requires the installation of OpenSSL, an open-source encryption tool used my most encryption processes. Multiplying and dividing the large numbers used by SSL encryption (e.g. 2048) bit and running the SHA-1 and other algorithms takes CPU time.

Apache
A common Nginx configuration is to use Nginx for the proxy server and to handle static content and then Apache the application server. For example, you could have Nginx handle HTML files and Apache handle PHP or pass through Java and JSP requests through to something like the Oracle Weblogic server.
So it you want to boost the speed of your application, you might consider using the Nginx web and proxy server. Download it for free and play around with it for free and install it in your lab. The only difficult is how to pronounce the name (I have no clue.)