Using Nginx as reverse proxy (HTTP/HTTPS) for load-balancing multiple Apache servers

Try it in our public cloud & Get $5 Credit

How do you pronounce Nginx, and what exactly is it? Nginx is pronounced “engine-ex,” and it basically serves as a reverse proxy server. Before understanding what a reverse proxy server is, you need to know what a proxy server is. As the name proxy implies, a server is acting as a substitute. In the case of a proxy server, it accepts requests from clients and gets resources from servers. The proxy server then sends them to the client. How can you reverse that? With a reverse proxy server, the client doesn’t know it is talking to the proxy. The client makes a request to the server, hits the proxy instead, and the proxy passes the request along to the server. The server sends the response to the proxy, the proxy sends the response to the client as if it came from the server, and the client is none the wiser.

Why might you use a reverse proxy server? It is a good way to manage load balancing. If one client thinks it is talking to one server, imagine what happens when 100 clients hit that server? What about 1,000 or 10,000? Or, what if it is Christmas and you’re Amazon, and perhaps it’s 1 million or more? That one server can’t handle the whole load. You need to spread the load around, but you don’t want to have to tell each client to talk to a different server. From the clients’ perspective, they all need to talk to the same server. By introducing a reverse proxy server as a load balancer, you can distribute these requests across multiple servers. The clients don’t care as long as they get their responses. The clients still think it is just one machine. But the reverse proxy server is able to spread the load across multiple servers, so response times can stay optimal without having to push any changes out to clients. In short, the load balancer spreads the workload across multiple systems to avoid overloading a single resource.

That’s where Nginx comes in. It is an open-source, high-performance reverse proxy server that is used as a load balancer. Once configured, requests come in and the load-balancing methodology takes over to spread the workload. You can use round-robin technique to just loop through all the different servers for each request, or you can take into account which server has the fewest active connections, or you can just hash the IP address to spread things around.

One nice thing that Nginx adds is session persistence. While HTTP is stateless, Nginx manages its way around that to preserve some cached data such as for a shopping cart of long results pagination. And it will make sure that repeated requests from the same client will be routed to the same shared server to actually maintain session-specific data.

There is much more to Nginx than meets the eye. Even if you’re not in a load-balancing situation, using Nginx in front of, say, an Apache server can prove helpful for high-demand servers. Nginx offers its own caching mechanism. For a somewhat static page, Apache could handle the rendering of the first request. Nginx would then send the response back to the first client. If clients 2 through 10,000 came in quickly behind it, the Apache server would be none the wiser, as the Nginx server could just send back the same response to them all, provided all are within the maximum age time you’ve set.

There are pros and cons to using anything. In the case of Nginx, if you’re a low-traffic site, there is no benefit to all the headaches of configuring a secondary server. Apache servers serve about 40 percent of the web. Nginx is on perhaps 15 percent. It may have many big-name clients such as Netflix and Facebook, but it doesn’t have the audience, wide appeal and long history of Apache. A recent case study (http://nginx.com/resources/references/) says it’s on 40 percent of the busiest web sites, but who says Apache’s 40 percent of the whole web isn’t more than 40 percent of the busiest? Given that some sites pair Nginx with Apache, it wouldn’t surprise me that the Apache percentage would be even higher. Of course, it isn’t all bad. You can use Nginx to proxy not just web requests but also email, too. It streams media as well as standard web pages. No wonder Netflix is such a big user here. It uses memory much more efficiently than Apache without needing to create separate processes per request. It even has tools for activity monitoring.

Most people have high hopes of hitting it big — of possibly having that next Pet Rock, Rubik’s Cube, or Dancing Elmo, where everyone wants one and your website will be overrun with orders. With something like Nginx, you can help make sure your website can handle the demands and spread the load across as many servers as you end up needing. Speed is king to getting those orders, and using a reverse proxy server to help with load balancing goes a long way to getting things done quickly.

If you are interested in Nginx as an http server, I recommend you to have a look at this benchmark on Nginx server compared to other alternatives.