Throughout this guide we will validate configuration syntax with
nginx -t and reload NGINX after changes (for example systemctl reload nginx or nginx -s reload) so your edits can be tested safely without downtime.- Hosting multiple websites (virtual hosts)
- How NGINX maps requests to server blocks using the
server_namedirective. - Matching strategies and priority rules (exact names, wildcards, and regex).
- How NGINX maps requests to server blocks using the
- Redirects
- Using the
returndirective for simple, efficient redirects (for example, redirecting HTTP to HTTPS).
- Using the
- URL rewriting
- The
rewritedirective and basic regular expressions for routing and URL manipulation. - Examples illustrating capture groups and common rewrite patterns.
- The
- Upstreams
- Defining
upstreamblocks to group backend servers into a single logical pool for proxying. - How upstreams integrate with
proxy_pass.
- Defining
- Load balancing
- How NGINX distributes traffic across upstream backends.
- Built-in algorithms such as
round-robin(default),least_conn, and weighted servers.
- Reverse proxying
- Using NGINX as a reverse proxy to forward requests to backend services (often running on different ports like
8080,3000, etc.). - Handling headers, timeouts, and connection settings for robust proxying.
- Using NGINX as a reverse proxy to forward requests to backend services (often running on different ports like
- Caching
- Why caching improves performance and reduces backend load.
- Basic
proxy_cachebuilding blocks and configuration examples.
- Hosting multiple websites (virtual hosts)
server_name directive is the primary mapping between an incoming Host header and a server block. Priority rules are: exact names > longest wildcard starting with * > longest wildcard ending with * > regex matches.
Example: two sites on one server (port 80)
- Use
listen 80 default_serveronly for a fallback. - For HTTPS you’ll add
listen 443 ssland TLS certs per server block.
- Redirects
return. It’s fast and clear.
Example: redirect all HTTP to HTTPS
- URL rewriting
rewrite for regex-based URL transformations or legacy URL patterns. For many routing cases, consider try_files first (it’s less error-prone and faster).
Examples: rewrite with capture groups
laststops processing the current location and restarts the location lookup.breakstops processing rewrite directives in the current location and serves the rewritten URI.
- Upstreams
upstream blocks and reference them via proxy_pass. This centralizes backend definitions and enables load-balancing options.
Example: a basic upstream with three backends
- Load balancing
Health checks are available in commercial/NGINX Plus and via third-party modules for open source; you can also implement passive checks (detect failures from backend responses/timeouts).
- Reverse proxying — headers, timeouts, and robust proxying
proxy_pass to forward requests. Configure connection and response timeouts plus buffering for reliability.
Example: common proxy settings
proxy_http_version 1.1 and forward connection headers.
- Caching
proxy_cache_path, proxy_cache, proxy_cache_key, and proxy_cache_valid.
Example: basic file-based cache
X-Cache-StatusorX-Cachehelp you diagnose caching behavior (HIT,MISS,BYPASS, etc.).- Respect
Cache-ControlandExpiresfrom upstream if desired (useproxy_ignore_headersto override).
- Check syntax:
nginx -t - Reload gracefully:
systemctl reload nginxornginx -s reload - Inspect logs:
/var/log/nginx/access.logand/var/log/nginx/error.log - Use curl to validate behavior:
- NGINX official docs: https://nginx.org/en/docs/
- NGINX HTTP proxy module: https://nginx.org/en/docs/http/ngx_http_proxy_module.html
- NGINX rewrite module: https://nginx.org/en/docs/http/ngx_http_rewrite_module.html
- Guide to NGINX caching: https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_cache
- Prefer
returnandtry_filesfor simple, performant operations before reaching forrewrite. - Keep upstream definitions centralized so you can manage backends and weights without editing many server blocks.
- Add health checks, timeouts, and appropriate headers to make proxying resilient.
- Always validate with
nginx -tand reload gracefully to avoid downtime.