Nginx Reverse Proxy, Load Balancing, and URL Rewrite Guide
This article explains how Nginx can act as a reverse proxy, distribute traffic with various load‑balancing methods, serve static assets separately from dynamic back‑ends, and rewrite friendly URLs, providing clear configuration snippets for each technique.
This article introduces the concepts of reverse proxy, load balancing, static‑dynamic separation, and URL rewrite using Nginx, and provides practical configuration examples.
Reverse Proxy When a client sends a request, it first reaches a gateway (e.g., Nginx), which forwards the request to a Tomcat server. The client never contacts Tomcat directly, improving security. The opposite is a forward proxy, which the client configures to access external resources.
Example configuration for a simple reverse proxy:
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://www.baidu.com;
}
}Accessing http://192.168.33.10 will be proxied to Baidu’s homepage.
Load Balancing Load balancing distributes requests across multiple backend servers to avoid overload on a single node. A basic round‑robin algorithm can be expressed as:
request_target = request_index % server_countConfiguration example using the upstream directive:
http {
upstream alls {
server 192.168.33.10:80;
server 192.168.34.10;
server 192.168.35.10;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://alls;
}
}
}Load‑Balancing Strategies
Weight distribution – assign higher weights to more powerful servers.
IP hash – route the same client IP to the same backend (helps session persistence).
Least connections – forward to the server with the fewest active connections.
Response time – prefer servers with shorter response times.
Directed traffic – hash a specific URL to a particular server.
Weight‑based example:
http {
upstream alls {
server 192.168.33.10:80 weight=8;
server 192.168.34.10:80 weight=5;
server 192.168.35.10:80 weight=2;
}
...
}To disable a server, add the down parameter; to mark a server as backup, add backup .
Static‑Dynamic Separation Static resources (css, js, images) can be served directly by Nginx, reducing load on Tomcat. Example:
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://192.168.33.10:8080;
}
location ~*/(css|js|img) {
root html;
}
}URL Rewrite Nginx can rewrite friendly URLs to internal routes. The following rule rewrites /123.html to /shop?shopId=123 :
server {
listen 80;
server_name localhost;
location / {
rewrite ^/([0-9]+).html$ /shop?shopId=$1 break;
proxy_pass http://192.168.33.10:8080;
}
}This technique is widely used by e‑commerce sites to present clean URLs while routing requests to dynamic handlers.
Java Tech Enthusiast
Sharing computer programming language knowledge, focusing on Java fundamentals, data structures, related tools, Spring Cloud, IntelliJ IDEA... Book giveaways, red‑packet rewards and other perks await!
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.