Operations 11 min read

Unlock Nginx: Reverse Proxy, Load Balancing & Static Serving Without Add‑ons

This article explains how Nginx can function as a reverse proxy, load balancer, HTTP server with static‑file handling, and forward proxy without relying on third‑party modules, providing configuration examples and discussing built‑in load‑balancing strategies such as round‑robin, weight, ip_hash, fair, and url_hash.

Efficient Ops
Efficient Ops
Efficient Ops
Unlock Nginx: Reverse Proxy, Load Balancing & Static Serving Without Add‑ons

Introduction

This article focuses on what Nginx can accomplish without loading third‑party modules. It covers the author’s personal experience and may not be exhaustive.

Nginx can do: Reverse proxy Load balancing HTTP server (including static‑file separation) Forward proxy

1. Reverse Proxy

A reverse proxy forwards client requests to internal servers and returns the responses. It is the most common use case for Nginx.

<code>server {
    listen       80;
    server_name  localhost;
    client_max_body_size 1024M;

    location / {
        proxy_pass http://localhost:8080;
        proxy_set_header Host $host:$server_port;
    }
}
</code>

After saving the configuration and starting Nginx, accessing

http://localhost

will proxy to

http://localhost:8080

.

2. Load Balancing

Nginx can distribute traffic among multiple backend servers. It supports three built‑in strategies and two common third‑party ones.

2.1 RR (Round‑Robin, default)

Requests are assigned to backends in order; unavailable servers are automatically skipped.

<code>upstream test {
    server localhost:8080;
    server localhost:8081;
}
server {
    listen       81;
    server_name  localhost;
    client_max_body_size 1024M;

    location / {
        proxy_pass http://test;
        proxy_set_header Host $host:$server_port;
    }
}
</code>

2.2 Weighted Round‑Robin

Assigns request probability proportionally to the

weight

parameter.

<code>upstream test {
    server localhost:8080 weight=9;
    server localhost:8081 weight=1;
}
</code>

2.3 ip_hash

Ensures a client always reaches the same backend based on the client IP hash, useful for session‑affine applications.

<code>upstream test {
    ip_hash;
    server localhost:8080;
    server localhost:8081;
}
</code>

2.4 fair (third‑party)

Distributes requests according to backend response time; the fastest server receives more traffic.

<code>upstream backend {
    fair;
    server localhost:8080;
    server localhost:8081;
}
</code>

2.5 url_hash (third‑party)

Routes requests based on the hash of the request URL, useful for caching scenarios.

<code>upstream backend {
    hash $request_uri;
    hash_method crc32;
    server localhost:8080;
    server localhost:8081;
}
</code>

Only the built‑in strategies are covered here; third‑party modules such as

fair

and

url_hash

require additional installation.

3. HTTP Server

Nginx can serve static files directly. The following configuration serves files from

e:\wwwroot

and sets

index.html

as the default page.

<code>server {
    listen       80;
    server_name  localhost;
    client_max_body_size 1024M;

    location / {
        root   e:\wwwroot;
        index  index.html;
    }
}
</code>

Static‑file handling can be combined with reverse proxying to separate dynamic and static resources:

<code>upstream test{
    server localhost:8080;
    server localhost:8081;
}

server {
    listen       80;
    server_name  localhost;

    location / {
        root   e:\wwwroot;
        index  index.html;
    }

    # Static files handled by Nginx
    location ~ \.(gif|jpg|jpeg|png|bmp|swf|css|js)$ {
        root   e:\wwwroot;
    }

    # Dynamic requests forwarded to Tomcat
    location ~ \.(jsp|do)$ {
        proxy_pass  http://test;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   e:\wwwroot;
    }
}
</code>

4. Forward Proxy

A forward proxy sits between the client and the origin server. Nginx can act as a forward proxy, though it does not support HTTPS out of the box.

<code>resolver 114.114.114.114 8.8.8.8;
server {
    resolver_timeout 5s;
    listen 81;
    access_log  e:\wwwroot\proxy.access.log;
    error_log   e:\wwwroot\proxy.error.log;

    location / {
        proxy_pass http://$host$request_uri;
    }
}
</code>

The

resolver

directive specifies DNS servers for the proxy, and the

listen

port is used by client applications to configure the proxy.

5. Final Notes

Nginx supports hot reload, allowing configuration changes without stopping the service.

<code>nginx -s reload</code>

On Windows the command is:

<code>nginx.exe -s reload</code>
operationsLoad BalancingconfigurationNginxhttp serverReverse Proxy
Efficient Ops
Written by

Efficient Ops

This public account is maintained by Xiaotianguo and friends, regularly publishing widely-read original technical articles. We focus on operations transformation and accompany you throughout your operations career, growing together happily.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.