Comprehensive Guide to Nginx: Concepts, Configuration, and Usage

This article provides an in‑depth overview of Nginx, covering its core features, performance benefits, request processing flow, forward and reverse proxy concepts, startup commands, directory layout, detailed nginx.conf settings, location directive syntax, common SPA 404 handling, CORS setup, and gzip compression for building high‑performance web services.

Rare Earth Juejin Tech Community
Rare Earth Juejin Tech Community
Rare Earth Juejin Tech Community
Comprehensive Guide to Nginx: Concepts, Configuration, and Usage

Nginx is an open‑source high‑performance HTTP and reverse‑proxy server used for serving static resources, load balancing, caching, and more, prized for its low memory usage, high concurrency, and stability.

Key reasons to use Nginx include its event‑driven asynchronous architecture for high performance, strong reliability with fault tolerance and health checks, built‑in load‑balancing capabilities, efficient static file handling, and extensibility through modules such as gzip, SSL/TLS, and caching.

The request‑handling process involves receiving the request, parsing method, URL and headers, matching configuration rules, performing actions such as static file serving, reverse proxying, caching, URL rewriting, SSL/TLS encryption, and access control, and finally generating and sending the response.

Forward proxy forwards client requests to target servers, hiding the client’s IP, while reverse proxy receives client requests and forwards them to backend servers, providing load balancing, high availability, and security.

进入目录:/usr/local/nginx/sbin
启动命令:./nginx
重启命令:nginx -s reload
快速关闭命令:./nginx -s stop
有序地停止,需要进程完成当前工作后再停止:./nginx -s quit
直接杀死nginx进程:killall nginx

Typical Nginx directory structure includes folders for configuration ( conf), logs, HTML site files, temporary data, and the executable binary in sbin.

The main configuration file nginx.conf defines worker processes, error logs, PID file, event settings, HTTP settings (mime types, client buffers, keepalive, gzip, etc.), upstream blocks, and server blocks with listen ports, server names, root, index, static asset caching, log formats, and various proxy directives.

The location directive matches request URIs using modifiers (=, ~, ~*, ^~) and defines the configuration block to apply; examples illustrate exact match, prefix match, regex match, and case‑insensitive regex.

For single‑page applications, a common 404 fix is:

location / {
    try_files $uri $uri/ /index.html;
}

To enable cross‑origin requests, add headers such as Access-Control-Allow-Origin *, allowed methods, and handle OPTIONS pre‑flight with a 204 response.

Gzip compression can be enabled with directives like gzip on;, setting minimum length, buffers, compression level, MIME types, and the gzip_vary on; flag.

Overall, Nginx is a lightweight, high‑performance, reliable, and extensible server suitable for building high‑availability web applications.

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 balancingNginxreverse proxyWeb server
Rare Earth Juejin Tech Community
Written by

Rare Earth Juejin Tech Community

Juejin, a tech community that helps developers grow.

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.