Backend Development 16 min read

Comprehensive Nginx Configuration Guide: HTTP Server, Static Server, 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, variable usage, and common directives such as return, rewrite, error_page, and access logging.

Java Architect Essentials
Java Architect Essentials
Java Architect Essentials
Comprehensive Nginx Configuration Guide: HTTP Server, Static Server, 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., Docroot(/usr/local/var/www) ) and creating an html directory with test.html enables simple deployment.

Configuration of the server block is done in nginx.conf . A minimal example:

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;
        }
    }
}

Testing the setup involves accessing http://localhost/ (which maps to /usr/local/var/www/index.html ) and http://localhost/test.html (which maps to /usr/local/var/www/html/test.html ).

Common location directives include:

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

listen : specifies IP address and port.

server_name : sets the domain name.

location : maps URI patterns to configuration blocks; can use exact match (=), prefix (^~), regular expression (~, ~*), or generic (/).

root : defines the filesystem path for static files.

index : specifies default index files.

Regular‑expression symbols for location patterns are explained (e.g., . , ? , + , * , \d , ^ , $ , {n} , [a‑z] , (a|b|c) , \ ).

Static server configuration often involves creating separate directories for images and other assets, then using location blocks such as:

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;
}

Reverse proxy is implemented with proxy_pass . Example forwarding all traffic to a Java web app on port 8081:

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 uses the upstream directive. Several strategies are covered:

Round‑robin (default): upstream web_servers { server localhost:8081; server localhost:8082; }

Weight: upstream test { server localhost:8081 weight=1; server localhost:8082 weight=3; server localhost:8083 weight=4 backup; }

IP hash: upstream test { ip_hash; server localhost:8080; server localhost:8081; }

Third‑party fair and url_hash modules for response‑time‑based or request‑URI‑based distribution.

Dynamic‑static separation combines reverse proxy and static file handling in one server block, routing static assets to a dedicated root and proxying other requests to the backend.

Additional useful directives:

return – sends a status code and optional redirect URL.

rewrite – modifies the request URI using regular expressions.

error_page – defines custom error pages.

log_format and access_log – configure request logging.

deny – blocks access to specified locations.

Various built‑in variables (e.g., $args , $remote_addr , $request_uri ) are listed for reference.

The article concludes with a reminder to share the content and join the community for further learning.

Backend DevelopmentLoad BalancingNginxReverse ProxyServer Configurationstatic files
Java Architect Essentials
Written by

Java Architect Essentials

Committed to sharing quality articles and tutorials to help Java programmers progress from junior to mid-level to senior architect. We curate high-quality learning resources, interview questions, videos, and projects from across the internet to help you systematically improve your Java architecture skills. Follow and reply '1024' to get Java programming resources. Learn together, grow together.

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.