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.
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://localhostwill 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
weightparameter.
<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
fairand
url_hashrequire additional installation.
3. HTTP Server
Nginx can serve static files directly. The following configuration serves files from
e:\wwwrootand sets
index.htmlas 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
resolverdirective specifies DNS servers for the proxy, and the
listenport 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>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.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.