Nginx proxy and load-balancing overview

Nginx proxy and load-balancing overview

Nginx Proxy and Upstream Modules

Nginx continues to gain market share and is probably the most viable open-source HTTP server currently competing against Apache today. With its asynchronous, event-driven architecture, Nginx is able to scale extremely well and can outperform Apache under heavy loads. Couple this with its support for proxying WebSocket protocol, and you’ve got a solid choice for a reverse proxy or load balancer.

These functions are derived from their respective modules, and both Proxy and Upstream are commonly included with system packages. You can confirm that these packages were included by checking the configuration arguments with nginx -V. If the output includes upstream and proxy references, you’re ready to configure them.


Nginx Upstream Configuration

Upstream configuration consists of defining the reverse proxy or load-balancing servers, the balancing methods, and associated server weights if you want to change the default of 1. Requests will be distributed between the servers using a weighted round-robin balancing method by default.

You can also configure how many times a server fails prior to being skipped (max_fails, default is 1) for a specific duration (fail_timeout, defaults is 10 seconds, and used by max_fails to determine how long before the failure limit resets itself). A backup server can be defined to be used when the primary servers fail.

upstream load_balancing_alpha {
 # OPTIONAL load balancing methods
 # these directives are mutually exclusive
 # use server with least active connections
 # least_conn;
 # distribute requests based on client IP addresses
 # ip_hash;
 server weight=3;
 server max_fails=2 fail_timeout=30;
 # OPTIONAL backup server in case the two above fail
 server backup;

When using ip_hash, one or more of the load-balancing servers can be marked as permanently unavailable by following the server name with down. It doesn’t use the max_fails or fail_timeout directives.

Nginx Proxy Configuration



With just a few lines, Nginx’s proxy module can be configured to operate as forward/standing or reverse proxy. Both use the proxy_pass directive in the location context.


location / {
    # OPTIONAL nameserver used to resolve upstream servers
    # resolver
    proxy_pass http://load_balancing_alpha;

This may look familiar if you’ve worked with FastCGISCGImemcached, or uwsgi.

There are many additional directives available, that can be used for more advanced configuration.

Forward proxy server

  • Accessing blocked sites over SSL
  • Serving geo-restricted content by identifying servers within the approved location
  • Caching proxied content to improve speeds to frequently used sites

Reverse proxy server

  • Balancing loads between multiple disparate servers
  • Redirecting traffic to a secondary server when undergoing maintenance or testing on your main one
  • Providing a layer of protection for a real server by allowing access only through designated Nginx servers

Written by

The author didnt add any Information to his profile yet

Leave a Reply

Your email address will not be published. Required fields are marked *