Master Nginx: From Static Server to Load Balancing and Reverse Proxy
This guide walks through configuring Nginx as a static HTTP server, setting up static file handling, implementing reverse proxy, applying various load‑balancing strategies, separating dynamic and static resources, and using essential directives such as return, rewrite, and error_page, providing practical code examples and tips.
1. HTTP Server
Nginx can serve static resources directly. When a site consists only of static pages, you can deploy it by placing files under the document root and configuring a simple server block.
1. Create an html directory under the document root ( /usr/local/var/www) and add test.html.
2. Configure nginx.conf with a server block:
user mengday staff;
http {
server {
listen 80;
server_name localhost;
client_max_body_size 1024M;
# default location
location / {
root /usr/local/var/www/html;
index index.html index.htm;
}
}
}3. Test access:
http://localhost/ → /usr/local/var/www/index.html (default Nginx page)
http://localhost/test.html → /usr/local/var/www/html/test.html
Note: If you encounter a 403 Forbidden when accessing images, adjust the user directive in nginx.conf (e.g., user root on Linux or user $(who am i) on macOS) and reload the configuration.
2. Static Server
In many companies a static server provides an upload function for other applications to fetch static resources.
1. Under /usr/local/var/www create images and img directories and place a test.jpg in each.
http {
server {
listen 80;
server_name localhost;
set $doc_root /usr/local/var/www;
# default location
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;
}
}
}Custom variables are defined with the set directive. Static location mapping can use path prefixes (e.g., /images/) or file‑extension patterns.
Accessing http://localhost/test.jpg maps to $doc_root/img. When multiple locations match, ^~ has higher priority than ~, so the /images/ block is chosen.
Common location matching types and their priority (high to low): = exact match ^~ prefix match (stop search) ~ case‑sensitive regex ~* case‑insensitive regex
regular string prefix / generic match
3. Reverse Proxy
Reverse proxy is a major Nginx feature. It receives external requests and forwards them to internal servers, returning the response to the client.
Example: a Java web application running on port 8081.
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://localhost:8081;
proxy_set_header Host $host:$server_port;
# set client IP
proxy_set_header X-Forwarded-For $remote_addr;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503;
}
}Visiting http://localhost/ proxies the request to localhost:8081.
4. Load Balancing
Load balancing distributes requests across multiple backend servers. Nginx uses the upstream directive. The default strategy is round‑robin.
upstream web_servers {
server localhost:8081;
server localhost:8082;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://web_servers;
proxy_set_header Host $host:$server_port;
}
}Other strategies include weight‑based, ip_hash, fair (third‑party), and url_hash (third‑party).
# weight example
upstream test {
server localhost:8081 weight=1;
server localhost:8082 weight=3;
server localhost:8083 weight=4 backup;
} # ip_hash example
upstream test {
ip_hash;
server localhost:8080;
server localhost:8081;
} # fair (third‑party)
upstream backend {
fair;
server localhost:8080;
server localhost:8081;
} # url_hash (third‑party)
upstream backend {
hash $request_uri;
hash_method crc32;
server localhost:8080;
server localhost:8081;
}5. Dynamic‑Static Separation
Separating dynamic and static resources allows caching of static assets. A typical configuration combines an upstream for dynamic requests and location blocks for static files.
upstream web_servers {
server localhost:8081;
server localhost:8082;
}
server {
listen 80;
server_name localhost;
set $doc_root /usr/local/var/www;
location ~* \.(gif|jpg|jpeg|png|bmp|ico|swf|css|js)$ {
root $doc_root/img;
}
location / {
proxy_pass http://web_servers;
proxy_set_header Host $host:$server_port;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root $doc_root;
}
}6. Other Useful Directives
return– returns an HTTP status code or redirects. rewrite – rewrites the request URI using regular expressions. error_page – defines custom error pages. log_format and access_log – configure request logging. deny – blocks access to matched resources.
Built‑in variables such as $args, $content_length, $host, $remote_addr, etc., 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.
Java Interview Crash Guide
Dedicated to sharing Java interview Q&A; follow and reply "java" to receive a free premium Java interview guide.
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.
