Skip to main content
Welcome back — I hope you had a good break. This guide explores several of NGINX’s most useful features. We won’t document every capability; instead we’ll focus on the patterns you’ll use most frequently in production. The topics are presented in a logical order so you can follow along and build on each concept.
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.
Overview of topics
  • Hosting multiple websites (virtual hosts)
    • How NGINX maps requests to server blocks using the server_name directive.
    • Matching strategies and priority rules (exact names, wildcards, and regex).
  • Redirects
    • Using the return directive for simple, efficient redirects (for example, redirecting HTTP to HTTPS).
  • URL rewriting
    • The rewrite directive and basic regular expressions for routing and URL manipulation.
    • Examples illustrating capture groups and common rewrite patterns.
  • Upstreams
    • Defining upstream blocks to group backend servers into a single logical pool for proxying.
    • How upstreams integrate with proxy_pass.
  • 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.
  • Caching
    • Why caching improves performance and reduces backend load.
    • Basic proxy_cache building blocks and configuration examples.
Below are concise examples, best-practice suggestions, and quick tests to help you apply these patterns reliably. Quick reference table — common directives and use cases
  1. Hosting multiple websites (virtual hosts)
NGINX uses server blocks to host multiple websites on a single machine. The 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)
Tips:
  • Use listen 80 default_server only for a fallback.
  • For HTTPS you’ll add listen 443 ssl and TLS certs per server block.
  1. Redirects
When you need a simple redirect (HTTP→HTTPS, domain canonicalization, etc.), use return. It’s fast and clear. Example: redirect all HTTP to HTTPS
Example: canonical domain redirect
  1. URL rewriting
Use 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
Example: simple internal rewrite (no redirect)
Note:
  • last stops processing the current location and restarts the location lookup.
  • break stops processing rewrite directives in the current location and serves the rewritten URI.
  1. Upstreams
Group backends into upstream blocks and reference them via proxy_pass. This centralizes backend definitions and enables load-balancing options. Example: a basic upstream with three backends
Using the upstream in a server block:
  1. Load balancing
NGINX supports several built-in algorithms. Default is round-robin. 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).
  1. Reverse proxying — headers, timeouts, and robust proxying
Use proxy_pass to forward requests. Configure connection and response timeouts plus buffering for reliability. Example: common proxy settings
When proxying WebSockets, include proxy_http_version 1.1 and forward connection headers.
  1. Caching
Caching reduces backend load and improves latency. Basic building blocks: proxy_cache_path, proxy_cache, proxy_cache_key, and proxy_cache_valid. Example: basic file-based cache
Common headers:
  • X-Cache-Status or X-Cache help you diagnose caching behavior (HIT, MISS, BYPASS, etc.).
  • Respect Cache-Control and Expires from upstream if desired (use proxy_ignore_headers to override).
Testing and validating changes
  • Check syntax: nginx -t
  • Reload gracefully: systemctl reload nginx or nginx -s reload
  • Inspect logs: /var/log/nginx/access.log and /var/log/nginx/error.log
  • Use curl to validate behavior:
Links and references Final notes
  • Prefer return and try_files for simple, performant operations before reaching for rewrite.
  • 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 -t and reload gracefully to avoid downtime.

Watch Video