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.confplus included files (conf.d/*.conforsites-enabled/). - Blocks nest:
http>server(a virtual host, matched bylistenandserver_name) >location(matched by path; exact, prefix and regex rules have a precedence order worth learning). proxy_pass http://upstream_name;with anupstreamblock for load balancing; setproxy_set_header Host $host;and theX-Forwarded-*headers.
Everyday features
rootandtry_filesfor static sites and single-page apps.- TLS with
ssl_certificateand a modern protocol list; HTTP to HTTPS redirects. gzip,client_max_body_size,proxy_read_timeout,limit_reqfor 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
- nginx documentation (nginx.org)
- nginx beginner's guide (nginx.org)