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

This article provides a step‑by‑step tutorial on configuring Nginx as an HTTP server, serving static files, setting up reverse proxy, implementing various load‑balancing strategies, separating static and dynamic content, and using common directives such as return, rewrite, error_page, logging and access control.

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

In the first part, the article explains how to use Nginx as a simple HTTP server for static resources. It shows creating a Docroot(/usr/local/var/www) directory, placing test.html, and configuring nginx.conf with a basic server block that listens on port 80 and serves files from the root path.

Next, it demonstrates static‑file handling by creating images and img directories under the document root and configuring location blocks. The example uses a custom variable $doc_root and shows how to map requests like http://localhost/test.jpg to the appropriate directory using location /images/ and regular‑expression locations.

The reverse‑proxy section introduces the proxy_pass directive. A sample configuration forwards all requests received on port 80 to a backend Java application running on localhost:8081, while preserving the original Host header and forwarding the client IP via X-Forwarded-For.

Load‑balancing is covered in detail, presenting five strategies supported by Nginx: round‑robin (default), weighted round‑robin, ip_hash, fair (third‑party), and url_hash (third‑party). Example upstream blocks illustrate how to define multiple backend servers and select a strategy, including backup servers and weight assignments.

The article then explains static‑dynamic separation (动静分离) by combining proxying for dynamic requests with direct file serving for static assets. It shows a combined configuration that uses proxy_pass for the root location and a regular‑expression location to serve cached images, CSS, JS, etc.

Finally, several useful Nginx directives are introduced: return for status‑code redirects, rewrite for URI rewriting, error_page for custom error handling, logging configuration with log_format and access_log, deny for access restriction, and a list of built‑in variables (e.g., $args, $remote_addr, $request_uri) that can be used throughout the configuration.

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.

Nginxreverse proxy
Top Architect
Written by

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.

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.