Nginx

The most widely deployed web server and reverse proxy; learn its config model and the handful of directives that cover most real setups.

Intermediate · about 8 hours · difficulty 2/5

Learn first: How Web Servers Serve Traffic

Nginx is our recommended web server to learn deeply because you will meet it everywhere: on VMs, in Docker images, and as the most common Kubernetes ingress controller.

Config model

  • Config lives in /etc/nginx/nginx.conf plus included files (conf.d/*.conf or sites-enabled/).
  • Blocks nest: http > server (a virtual host, matched by listen and server_name) > location (matched by path; exact, prefix and regex rules have a precedence order worth learning).
  • proxy_pass http://upstream_name; with an upstream block for load balancing; set proxy_set_header Host $host; and the X-Forwarded-* headers.

Everyday features

  • root and try_files for static sites and single-page apps.
  • TLS with ssl_certificate and a modern protocol list; HTTP to HTTPS redirects.
  • gzip, client_max_body_size, proxy_read_timeout, limit_req for rate limiting, proxy_cache.
  • Custom log_format (JSON) for access logs.

Operations

Always run nginx -t before systemctl reload nginx; reload is graceful and keeps connections. Read error.log first when something fails.

Pitfalls

Trailing slash behaviour in proxy_pass, location precedence surprises, and if inside location (it rarely does what you expect).

Good enough to move on: you can host a static site and a proxied API on one server with TLS, redirects, gzip, rate limiting and JSON logs.

Key concepts

  • server and location blocks
  • proxy_pass and upstreams
  • TLS and redirects
  • try_files for SPAs
  • Rate limiting with limit_req
  • proxy_cache
  • nginx -t and graceful reload

Other options for this step

  • Caddy: A modern web server with automatic HTTPS and a very short config format, great for small teams and simple deployments.
  • HAProxy: A high-performance TCP and HTTP load balancer known for reliability, fine-grained health checks and detailed stats.
  • Apache HTTP Server: The long-standing web server still common in legacy stacks, shared hosting and PHP applications.

Mini project

Serve a React build and proxy /api to a backend on one Nginx server with Let's Encrypt TLS, HTTP to HTTPS redirect, gzip, a 10 requests per second rate limit on /api, and JSON access logs.

Check yourself

What is the difference between proxy_pass http://app; and proxy_pass http://app/; for a location /api/ block?

Free resources