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

This article provides a step‑by‑step tutorial on using Nginx as a static file server, configuring location blocks and regex priorities, setting up reverse proxy and various load‑balancing strategies, and applying advanced directives such as return, rewrite, error_page, logging and access control.

Architecture Digest
Architecture Digest
Architecture Digest
Comprehensive Nginx Configuration Guide: Static Server, Reverse Proxy, Load Balancing and Advanced Directives

Nginx can serve static resources directly; to deploy a static site, create a /usr/local/var/www/html directory, place an index.html or test.html file, and configure the server block in nginx.conf to point the root to that path.

Example server configuration:

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

After reloading Nginx, http://localhost/ serves the default page and http://localhost/test.html serves the custom file. If a 403 error occurs, adjust the user directive (e.g., user root; on Linux or the appropriate group on macOS).

Location blocks can be refined with regular expressions and priorities. Common directives include: server: defines a virtual server. listen: IP address and port. server_name: domain name. location: maps URI patterns to configuration; regex types ( ~, ~*) and prefix types ( ^~, =) have distinct priority rules. root and index: resolve file paths.

Regular‑expression symbols such as ., ?, +, *, \d, ^, $, {n}, [a‑z], and grouping (a|b|c) are explained, and captured groups can be referenced with $1, $2, etc.

Static‑server example with two image directories and a custom variable:

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

Reverse proxy is implemented with proxy_pass. A simple proxy 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 block. The default round‑robin method, weighted round‑robin, ip_hash, and third‑party modules fair and url_hash are demonstrated:

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

Weighted example:

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

IP‑hash ensures a client always reaches the same backend, useful for session‑based applications:

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

Other useful directives: return – sends a status code or redirects, e.g.,

return 301 http://www.example.com/moved/here;
rewrite

– modifies the URI with regex, e.g.,

rewrite ^/users/(.*)$ /show?user=$1 break;
error_page

– defines custom error pages, e.g.,

error_page 404 /404.html;
log_format

and access_log – configure request logging. deny – blocks access to matching resources.

The article also lists many built‑in Nginx variables such as $remote_addr, $host, $request_uri, $scheme, and others that can be used in configurations.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

load balancingreverse proxyServer ConfigurationStatic Server
Architecture Digest
Written by

Architecture Digest

Focusing on Java backend development, covering application architecture from top-tier internet companies (high availability, high performance, high stability), big data, machine learning, Java architecture, and other popular fields.

0 followers
Reader feedback

How this landed with the community

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.