NGINX as a Reverse Proxy
[ 76 ]
The third load-balancing algorithm supported by the default upstream module,
least connections, is activated by the least_conn directive. This algorithm is
designed to distribute the load evenly among upstream servers, by selecting the
one with the fewest number of active connections. If the upstream servers do not
all have the same processing power, this can be indicated using the weight
parameter to the server directive. The algorithm will take into account the
differently-weighted servers when calculating the number of least connections.
Types of upstream servers
An upstream server is a server to which NGINX proxies a connection. This can be on
a different physical or virtual machine, but doesn't have to be. The upstream server
may be a daemon listening on a UNIX domain socket for connections on the local
machine or could be one of many on a different machine listening over TCP. It may
be an Apache server, with multiple modules to handle different kinds of requests,
or a Rack middleware server, providing an HTTP interface to Ruby applications.
NGINX can be configured to proxy to each of them.
Single upstream server
The Apache web server is used in common hosting scenarios to serve static files as
well as multiple types of interpreted files. The extensive documentation and how-to's
(found online) help users to get up-and-running quickly with their favorite CMS.
Unfortunately, the typical Apache configuration, due to resource limits, is not able to
handle many simultaneous requests. NGINX, though, is designed to handle this kind
of traffic and performs very well with little resource consumption. Since most CMSs
come pre-configured for Apache, integrating the use of .htaccess files for extended
configuration, the easiest way to take advantage of NGINX's strengths is for NGINX
to simply proxy connections to an Apache instance:
server {
location / {
proxy_pass http://localhost:8080;
}
}