Nginx Configuration Guide: HTTP Server, Static Files, Reverse Proxy, Load Balancing and Advanced Directives
This comprehensive guide explains how to configure Nginx as an HTTP server, static file server, reverse proxy, and load balancer, covering directory setup, location matching rules, priority order, upstream strategies, and useful directives such as return, rewrite, error_page, logging and access control.
The article, authored by a senior architect, provides a step‑by‑step tutorial on using Nginx for multiple roles, including serving static resources, acting as a reverse proxy, and performing load balancing.
1. HTTP Server – Nginx can serve static files directly. Create a document root (e.g., Docroot(/usr/local/var/www)) and place an index.html or other files under an html directory. A minimal nginx.conf server block looks like:
user nginx;
http {
server {
listen 80;
server_name localhost;
location / {
root /usr/local/var/www/html;
index index.html index.htm;
}
}
}Accessing http://localhost/ returns the default Nginx page, while http://localhost/test.html maps to /usr/local/var/www/html/test.html.
2. Static Server – Create separate directories (e.g., /usr/local/var/www/images and /usr/local/var/www/img) and place test images. Use the set directive to define a variable $doc_root and map locations:
http {
server {
listen 80;
server_name localhost;
set $doc_root /usr/local/var/www;
location / {
root $doc_root/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;
}
}
}This demonstrates path‑based and suffix‑based location matching.
3. Location Matching & Priority – Nginx supports several location modifiers ( =, ^~, ~, ~*, plain prefix). The priority order is: location = / (exact match) location ^~ /path/ (prefix, stop search) location ~ /regex/ and ~* (regex, longest wins)
plain prefix locations location / (catch‑all)
Example configuration illustrating these rules is provided in the source.
4. Reverse Proxy – The most common Nginx use‑case. A request is received by Nginx and forwarded to an internal service using proxy_pass. Example:
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;
}
}Visiting http://localhost/ actually reaches the backend at localhost:8081.
5. Load Balancing – Defined with the upstream block. Nginx supports several built‑in strategies:
Round‑robin (default)
Weighted round‑robin ( weight parameter) ip_hash – sticky sessions based on client IP
Third‑party fair – selects the server with the lowest response time
Third‑party url_hash – hashes the request URI
Example of weighted load balancing:
upstream test {
server localhost:8081 weight=1;
server localhost:8082 weight=3;
server localhost:8083 weight=4 backup;
}Requests are distributed according to the defined weights, with the backup server used only if the primary servers fail.
6. Additional Useful Directives return – sends a status code and optional redirect URL. rewrite – modifies the request URI using regular expressions. error_page – custom error pages.
Logging configuration with log_format and access_log, optionally enabling gzip on for compression. deny – blocks access to specific file types or paths.
Built‑in variables (e.g., $host, $remote_addr, $request_uri, $scheme) can be used throughout the configuration.
The article concludes with a reminder to like and share, and provides links to additional resources and community groups.
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.
Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.
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.
