What Nginx Can Do Without Third‑Party Modules: Reverse Proxy, Load Balancing, HTTP Server, and Forward Proxy
This article explains the core capabilities of Nginx without third‑party modules, covering reverse proxy, load balancing (including round‑robin, weight, ip_hash, fair, and url_hash), static HTTP serving with dynamic/static separation, forward proxy configuration, and hot‑reload commands.
Preface
This article focuses on what Nginx can handle without loading third‑party modules; it may not be exhaustive, but it reflects the author’s personal experience.
What Nginx Can Do
1. Reverse proxy
2. Load balancing
3. HTTP server (including static‑dynamic separation)
4. Forward proxy
The following sections describe each function in detail.
Reverse Proxy
Reverse proxy is one of the most common uses of Nginx. It receives external requests and forwards them to internal servers, returning the response to the client.
Below is a simple reverse‑proxy configuration:
server {
listen 80;
server_name localhost;
client_max_body_size 1024M;
location / {
proxy_pass http://localhost:8080;
proxy_set_header Host $host:$server_port;
}
}After saving the configuration and starting Nginx, accessing http://localhost will be proxied to http://localhost:8080 .
Load Balancing
Load balancing distributes requests across multiple backend servers. Nginx supports several built‑in strategies and two common third‑party ones.
1. RR (Round‑Robin, default)
Requests are distributed sequentially; unavailable servers are automatically removed.
Simple configuration:
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;
}
}The core of this configuration is the upstream block shown above.
2. Weight
Assigns a weight to each server; higher weight receives more requests.
upstream test {
server localhost:8080 weight=9;
server localhost:8081 weight=1;
}In ten requests, roughly nine go to 8080 and one to 8081 .
3. ip_hash
Hashes the client IP so that a given client always reaches the same backend, useful for session‑based applications.
upstream test {
ip_hash;
server localhost:8080;
server localhost:8081;
}4. fair (third‑party)
Distributes requests based on backend response time; faster servers receive more traffic.
upstream backend {
fair;
server localhost:8080;
server localhost:8081;
}5. url_hash (third‑party)
Hashes the request URI, directing the same URL to the same backend, which is helpful for caching.
upstream backend {
hash $request_uri;
hash_method crc32;
server localhost:8080;
server localhost:8081;
}Choose the strategy that best fits your scenario; note that fair and url_hash require third‑party modules.
HTTP Server
Nginx can serve static resources directly. A basic static‑file server configuration looks like this:
server {
listen 80;
server_name localhost;
client_max_body_size 1024M;
location / {
root e:\wwwroot;
index index.html;
}
}Visiting http://localhost will return e:\wwwroot\index.html . For dynamic sites, you can separate static and dynamic requests:
upstream test {
server localhost:8080;
server localhost:8081;
}
server {
listen 80;
server_name localhost;
location / {
root e:\wwwroot;
index index.html;
}
location ~ \.(gif|jpg|jpeg|png|bmp|swf|css|js)$ {
root e:\wwwroot;
}
location ~ \.(jsp|do)$ {
proxy_pass http://test;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root e:\wwwroot;
}
}This setup serves static files directly while forwarding JSP/DO requests to a backend (e.g., Tomcat).
Forward Proxy
A forward proxy sits between the client and the origin server. Nginx can act as a forward proxy, though HTTPS support is limited.
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;
}
}Configure the DNS resolvers and the listening port, then configure your browser or proxy client to use IP:81 as the proxy.
Final Remarks
Nginx supports hot reload, allowing configuration changes without stopping the service. Reload commands are:
nginx -s reloadOn Windows:
nginx.exe -s reloadFeel free to like or share if you find this guide helpful.
Java Captain
Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.
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.