Backend Development 15 min read

Comprehensive Nginx Configuration Guide: HTTP Server, Static Files, Reverse Proxy, Load Balancing, and Advanced Directives

This article provides a step‑by‑step tutorial on configuring Nginx as an HTTP server, static file server, reverse proxy, load balancer, and explains location matching, priority rules, and useful directives such as return, rewrite, error_page, logging, and built‑in variables.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
Comprehensive Nginx Configuration Guide: HTTP Server, Static Files, Reverse Proxy, Load Balancing, and Advanced Directives

Nginx can serve static resources directly; when a site consists only of static pages, placing the files under the document root (e.g., /usr/local/var/www/html ) and configuring a simple server block with listen 80 and root is sufficient.

Typical configuration steps:

Create the document root directory and add a test HTML file.

Edit nginx.conf to define a server block with listen 80 , server_name localhost , and a location / that points to the root directory.

Test the setup by accessing http://localhost/ or http://localhost/test.html .

Key location directives and their meanings:

server : defines a virtual server; multiple server blocks can coexist.

listen : specifies IP address and port to listen on.

server_name : sets the domain name for the virtual host.

location : maps URI patterns to specific handling rules; can use exact match ( = ), prefix match ( ^~ ), or regular expressions ( ~ , ~* ).

root and index : define the filesystem path and default index files.

Static server examples show how to create separate directories for images and use location blocks with root or custom variables (e.g., set $doc_root /usr/local/var/www; ) to serve files efficiently.

Location matching priority is independent of configuration order; the hierarchy is:

location = /   > location ^~ /path/   > location ~* /regex/   > location /   > location /

Reverse proxy is implemented with the proxy_pass directive, forwarding client requests to an upstream service (e.g., http://localhost:8081 ) while preserving headers such as Host and X-Forwarded-For .

Load balancing uses the upstream block. Common strategies include:

Round‑robin (default): distributes requests evenly.

Weighted round‑robin: assigns weights to servers (e.g., weight=3 ).

ip_hash : routes requests based on client IP to maintain session affinity.

Third‑party fair and url_hash modules for response‑time‑based or URL‑hash routing.

Static‑dynamic separation (动静分离) combines a reverse‑proxy for dynamic content with location blocks that serve static assets directly, improving caching and performance.

Additional useful directives:

return : sends a status code and optional URL (e.g., return 301 http://example.com ).

rewrite : modifies the request URI using regular expressions.

error_page : defines custom error pages.

Logging configuration with log_format and access_log .

deny : blocks access to specified resources.

Built‑in variables (e.g., $remote_addr , $host , $request_uri ) can be used throughout the configuration.

By following these examples, readers can set up a fully functional Nginx server that serves static files, proxies dynamic requests, balances load across multiple back‑ends, and handles common operational needs.

Load BalancingNginxReverse ProxyServer Configurationstatic files
Laravel Tech Community
Written by

Laravel Tech Community

Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.