Nginx Configuration Guide: Static Server, Reverse Proxy, Load Balancing, and Dynamic‑Static Separation
This article provides a comprehensive tutorial on configuring Nginx as a static file server, setting up location mappings, implementing reverse proxy, various load‑balancing strategies, dynamic‑static separation, and essential directives such as return, rewrite, error_page, logging, and access control.
The guide begins with using Nginx as a static server, showing how to create a document root, place test HTML files, and configure a basic
http { server { listen 80; server_name localhost; client_max_body_size 1024M; location / { root /usr/local/var/www/html; index index.html index.htm; } } }block.
It then explains location directives, including regular‑expression matching, and demonstrates a static server with custom variables using
http { server { listen 80; server_name localhost; set $doc_root /usr/local/var/www; location / { root /usr/local/var/www/html; index index.html index.htm; } location ^~ /images/ { root $doc_root; } location ~* \.(gif|jpg|jpeg|png|bmp|ico|swf|css|js)$ { root $doc_root/img; } } }.
The article covers reverse proxy configuration, routing external requests to an internal Java web service via
server { listen 80; server_name localhost; location / { proxy_pass http://localhost:8081; proxy_set_header Host $host:$server_port; proxy_set_header X-Forwarded-For $remote_addr; proxy_next_upstream error timeout invalid_header http_500 http_502 http_503; } }.
Load‑balancing is introduced with the upstream directive, describing round‑robin, weighted, ip_hash, fair, and url_hash strategies, each illustrated with example configurations such as
upstream web_servers { server localhost:8081; server localhost:8082; }and
upstream test { server localhost:8081 weight=1; server localhost:8082 weight=3; server localhost:8083 weight=4 backup; }.
Dynamic‑static separation is explained, showing how to split static resources from dynamic requests and combine proxying with static file serving in a single server block.
Additional useful directives are presented: return 301 http://www.example.com/moved/here; for redirects, rewrite ^/users/(.*)$ /show?user=$1 break; for URI rewriting, error_page 404 /404.html; for custom error pages, logging configuration with log_format and access_log, and access control using deny all; within a location.
The article concludes with a list of common built‑in Nginx variables (e.g., $host, $remote_addr, $request_uri) that can be used in configurations.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Architect's Guide
Dedicated to sharing programmer-architect skills—Java backend, system, microservice, and distributed architectures—to help you become a senior architect.
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.
