Skip to main content
Welcome back — I hope you had a good break. In this lesson we explore several practical NGINX features you’ll use regularly in production. We won’t cover every option, but we will focus on the most common and useful capabilities:
  • Host multiple websites on a single server using server_name.
  • Perform redirects with return (for simple canonicalization like HTTP → HTTPS).
  • Rewrite URLs with rewrite using regular expressions and capture groups.
  • Define upstream pools and apply load balancing (round-robin, weighted, least connections, IP hashing).
  • Use NGINX as a reverse proxy to forward requests to backend services on other ports.
  • Enable caching to improve performance and reduce backend load.
A slide titled "Objectives" with a teal gradient panel on the left. On the right are three numbered goals: hosting multiple sites on one web server, learning to redirect websites, and rewriting friendly URLs.

Quick overview

Below is a short table summarizing the topics and their typical use cases:

Hosting multiple sites on one server (server_name)

NGINX matches requests to a particular server block by listening port and server_name. Use separate server {} blocks for each domain or subdomain. Example: two sites on the same IP, one for example.com and one for api.example.com:
Notes:
  • The order of server_name matching: exact names, longest wildcard starting with *, longest wildcard ending with *, then regex.
  • Use listen 443 ssl; and certificate directives in the HTTPS server block.
When you add a new domain, create a dedicated server block and test the config using nginx -t before reloading with systemctl reload nginx (or nginx -s reload).

Redirects using return

For straightforward redirects (for example redirecting all HTTP traffic to HTTPS or canonicalizing www), prefer return because it’s simpler and faster than rewrite. HTTP → HTTPS redirect example:
Redirect www to non-www:
Use 301 for permanent redirects and 302 for temporary ones.
Avoid using rewrite when a simple return covers your use case—return is easier to read and slightly more efficient.

Rewriting URLs with rewrite and regex

rewrite allows transforming requested URIs using PCRE regular expressions and capture groups. Use it when you need complex mapping (e.g., legacy URL structures -> friendly URLs). Example: redirect old article paths to a new structure:
Explanation:
  • ^/old/([0-9]{4})/([0-9]{2})/(.*)$ captures year, month, and slug.
  • $1, $2, $3 reference the captured groups.
  • permanent issues a 301 redirect.
Tips:
  • Test your regex with tools like regex101 to avoid accidental matches.
  • Order matters: exact location blocks are evaluated before regex location blocks. Place regex location blocks carefully.

Upstream pools and load balancing

Use upstream blocks to define backend server groups. Reference the group from proxy_pass or other proxy directives. Basic upstream with round-robin (default):
Weighted servers:
Common load balancing methods:

Reverse proxy essentials

When proxying, ensure headers and client IPs are passed correctly. The typical minimal proxy configuration includes:
Notes:
  • proxy_http_version 1.1 and clearing Connection header help with keepalive behavior to upstreams.
  • If your backend runs on a different port, include it in the upstream server definition (e.g., server 127.0.0.1:3000;).

Caching responses with proxy_cache

Caching reduces backend load and improves response times. A simple caching setup:
Key points:
  • keys_zone reserves memory for cache keys (e.g., 10m).
  • proxy_cache_valid sets caching durations by status code.
  • X-Proxy-Cache header helps verify whether a response was served from cache (HIT) or passed to the backend (MISS).
Design cache keys carefully (including query strings or authentication headers when needed) and have a strategy for cache invalidation. For advanced cache purging, consider modules like ngx_cache_purge or manage via short TTLs and revalidation.

Final notes and best practices

  • Always test configuration changes with nginx -t and monitor error logs at /var/log/nginx/error.log.
  • Keep redirect and rewrite rules simple and clearly documented to avoid surprise behavior.
  • Use health checks and proper monitoring for upstream backends to detect failures quickly.
  • Use HTTPS and HSTS in production; consider automating certificate management with Let’s Encrypt (Certbot) or similar tools.
This lesson prepares you to host multiple sites, redirect and rewrite URLs, load-balance proxied backends, and cache responses effectively using NGINX.

Watch Video