Master Nginx: Comprehensive Guide to Reverse Proxy, Load Balancing, and Configuration
This article provides a complete walkthrough of Nginx, covering its roles as a reverse proxy, load balancer, static file server, and forward proxy, along with detailed configuration examples for global settings, HTTP blocks, server blocks, location directives, upstream modules, and stream handling.
What can Nginx do?
1.1 Reverse proxy – Accept external requests, forward them to internal servers, and return the responses to clients.
1.2 Load balancing – Distribute requests among two or more backend servers using various strategies such as round‑robin (RR), weighted, ip_hash, fair (third‑party), and url_hash (third‑party).
1.3 HTTP server (static/dynamic separation) – Serve static resources directly and proxy dynamic API requests to backend services. Example configuration:
<span>server {</span>
<span> listen 80;</span>
<span> server_name localhost;</span>
<span> client_max_body_size 1024M;</span>
<span> location / {</span>
<span> root e:/wwwroot;</span>
<span> index index.html;</span>
<span> }</span>
<span>}</span>1.4 Forward proxy (less common) – Clients explicitly specify the target server; the proxy hides client details. Example configuration:
<span>server {</span>
<span> listen 80;</span>
<span> server_name localhost nginx.tangll.cn;</span>
<span> resolver 8.8.8.8;</span>
<span> location / {</span>
<span> proxy_pass http://$http_host$request_uri;</span>
<span> }</span>
<span>}</span>Configuration Details
2.1 Overall structure
2.2 Global (main) block
<span># main – global configuration</span>
<span>user nobody nobody;</span>
<span>pid logs/nginx.pid;</span>
<span>worker_processes 2;</span>
<span>worker_rlimit_nofile 1024;</span>
<span>error_log logs/error.log info;</span>
<span>events { … }</span>
<span>http { … }</span>Key directives:
user – set the user/group for worker processes.
pid – file storing the master process ID.
worker_processes – number of worker processes (usually a multiple of CPU cores).
worker_rlimit_nofile – maximum open files per process.
error_log – path and level of error logging.
2.3 Events block
<span>events {</span>
<span> worker_connections 1024;</span>
<span> multi_accept on;</span>
<span> use epoll;</span>
<span>}</span>Directives control connection handling and I/O model.
2.4 HTTP block
<span>http {</span>
<span> sendfile on;</span>
<span> tcp_nopush on;</span>
<span> tcp_nodelay on;</span>
<span> keepalive_timeout 65;</span>
<span> send_timeout 50;</span>
<span> client_max_body_size 10m;</span>
<span> client_body_buffer_size 128k;</span>
<span> types_hash_max_size 2048;</span>
<span> include mime.types;</span>
<span> default_type application/octet-stream;</span>
<span> access_log logs/access.log main;</span>
<span> error_log logs/error.log;</span>
<span> gzip on;</span>
<span> underscores_in_headers on;</span>
<span> proxy_connect_timeout 500;</span>
<span> proxy_read_timeout 600;</span>
<span> proxy_send_timeout 500;</span>
<span> proxy_buffer_size 128k;</span>
<span> proxy_buffers 4 128k;</span>
<span> proxy_busy_buffers_size 256k;</span>
<span> proxy_temp_file_write_size 128k;</span>
<span> proxy_temp_path d:/nginx-1.15.2/temp;</span>
<span> proxy_cache_path d:/nginx-1.15.2/cache levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g;</span>
<span> upstream zuul_server { server 192.168.9.51:8000; }</span>
<span> server {</span>
<span> listen 8001;</span>
<span> server_name localhost;</span>
<span> location /report { alias D:/nginx-1.15.2/html/report-app/; }</span>
<span> location / { alias D:/nginx-1.15.2/html/master-app/; }</span>
<span> location /api/ { proxy_pass http://192.168.9.51:8057/; … }</span>
<span> }</span>
<span>}</span>2.4.4 Server block
listen – port to listen on (default 80).
server_name – hostnames for this virtual server.
root – document root for static files.
index – default index file.
access_log / error_log – log file locations.
2.4.5 Location module
Location directives match request URIs and can be precise (=), prefix (^~), or regex (~, ~*). Examples:
<span>location / { … }</span>
<span>location = / { … }</span>
<span>location ^~ /images/ { … }</span>
<span>location ~ \.(gif|jpg|jpeg)$ { … }</span>
<span>location ~* \.(css|js)$ { … }</span>Directives inside location include root, alias, proxy_pass, and proxy_set_header.
2.5 Upstream module
Defines backend servers for load balancing. Example:
<span>upstream backend {</span>
<span> server 192.168.1.10:8001;</span>
<span> server 192.168.1.11:8001;</span>
<span>}</span>Supports weight, max_fails, fail_timeout, backup, ip_hash, fair, url_hash, etc.
2.6 Stream block
<span>stream {</span>
<span> upstream backend {</span>
<span> hash $remote_addr consistent;</span>
<span> server 192.168.9.51:8888 weight=5;</span>
<span> server 192.168.8.230:8888 max_fails=3 fail_timeout=30s;</span>
<span> }</span>
<span> server {</span>
<span> listen *:9999 so_keepalive=on;</span>
<span> proxy_connect_timeout 1s;</span>
<span> proxy_timeout 20s;</span>
<span> proxy_pass backend;</span>
<span> proxy_protocol on;</span>
<span> }</span>
<span>}</span>Provides TCP proxy capabilities.
For a complete reference, consult the official Nginx documentation.
Architect's Alchemy Furnace
A comprehensive platform that combines Java development and architecture design, guaranteeing 100% original content. We explore the essence and philosophy of architecture and provide professional technical articles for aspiring architects.
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.
