Nginx
Overview
Nginx is an event-driven HTTP server and reverse proxy known for high concurrency and low memory use. It serves static content, terminates TLS, load-balances across upstream app servers, and proxies dynamic requests (e.g. to PHP-FPM, Node, or a Spring app). Read this when authoring nginx.conf, defining server/location blocks, configuring reverse proxy or load balancing, or installing Nginx from the official repository.
Official sources
Install / setup
# Ubuntu/Debian, from the official nginx.org repository (after adding the repo + key):
sudo apt update && sudo apt install nginx
Source: https://nginx.org/en/linux_packages.html (add the nginx signing key and deb ... nginx.org/packages/ubuntu repo first, then install).
Core concepts
- Master/worker processes — a master manages config; workers handle connections via an event loop.
- Contexts — directives nest in
main, events, http, server, and location blocks.
- server blocks — virtual hosts matched by
listen + server_name.
- location blocks — match request URIs (prefix,
=, ~ regex) to behavior.
- Reverse proxy —
proxy_pass forwards requests to a backend; set proxy_set_header for Host/X-Forwarded-*.
- upstream / load balancing — group backends; round-robin,
least_conn, ip_hash.
- Static serving —
root/alias + try_files resolve files on disk.
- TLS —
ssl_certificate/ssl_certificate_key on listen 443 ssl.
Best practices
Common pitfalls
- Overusing
if inside location causes surprising behavior → prefer try_files/map ("if is evil").
proxy_pass with a trailing slash vs none changes the rewritten URI → know the path-rewrite rule.
- Editing config without
nginx -t can take down the server on reload → always test first.
Examples
upstream app { server 127.0.0.1:3000; }
server {
listen 80;
server_name example.com;
location /static/ { root /var/www; }
location / {
proxy_pass http://app;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
Further reading
Related skills
- ../spring — proxy a Spring Boot app behind Nginx
- ../symfony — serve Symfony's front controller via Nginx + PHP-FPM
1---2name: nginx3description: Nginx — a high-performance HTTP server, reverse proxy, and load balancer for serving static files, terminating TLS, and proxying to app backends. Consult when writing nginx.conf, configuring server/location blocks, setting up reverse proxy (proxy_pass) or load balancing (upstream), serving static assets, enabling gzip/TLS, or installing from the official nginx.org repo.4domain: stack5category: backend6tags: [nginx, web-server, reverse-proxy, load-balancer, http, tls, backend]7official_sources:8 - https://nginx.org/en/docs/9 - https://github.com/nginx/nginx10 - https://nginx.org/en/linux_packages.html11verified: 2026-06-1712metadata:13 source: https://skillmd.com/skills/gabriel7silva/nginx14---1516# Nginx1718## Overview19Nginx is an event-driven HTTP server and reverse proxy known for high concurrency and low memory use. It serves static content, terminates TLS, load-balances across upstream app servers, and proxies dynamic requests (e.g. to PHP-FPM, Node, or a Spring app). Read this when authoring `nginx.conf`, defining server/location blocks, configuring reverse proxy or load balancing, or installing Nginx from the official repository.2021## Official sources22- Docs: https://nginx.org/en/docs/23- Repo: https://github.com/nginx/nginx24- Install: https://nginx.org/en/linux_packages.html2526## Install / setup27```bash28# Ubuntu/Debian, from the official nginx.org repository (after adding the repo + key):29sudo apt update && sudo apt install nginx30```31Source: https://nginx.org/en/linux_packages.html (add the nginx signing key and `deb ... nginx.org/packages/ubuntu` repo first, then install).3233## Core concepts34- **Master/worker processes** — a master manages config; workers handle connections via an event loop.35- **Contexts** — directives nest in `main`, `events`, `http`, `server`, and `location` blocks.36- **server blocks** — virtual hosts matched by `listen` + `server_name`.37- **location blocks** — match request URIs (prefix, `=`, `~` regex) to behavior.38- **Reverse proxy** — `proxy_pass` forwards requests to a backend; set `proxy_set_header` for Host/X-Forwarded-*.39- **upstream / load balancing** — group backends; round-robin, `least_conn`, `ip_hash`.40- **Static serving** — `root`/`alias` + `try_files` resolve files on disk.41- **TLS** — `ssl_certificate`/`ssl_certificate_key` on `listen 443 ssl`.4243## Best practices44- Use `try_files $uri $uri/ =404;` (or to a front controller) instead of `if` for routing (https://nginx.org/en/docs/http/ngx_http_core_module.html#try_files).45- Forward client info on proxies: set `Host`, `X-Forwarded-For`, `X-Forwarded-Proto` (https://nginx.org/en/docs/http/ngx_http_proxy_module.html).46- Test config before reload: `nginx -t` then `nginx -s reload` (https://nginx.org/en/docs/beginners_guide.html).47- Enable gzip and sensible `keepalive_timeout`/buffer tuning for throughput (https://nginx.org/en/docs/http/ngx_http_gzip_module.html).4849## Common pitfalls50- Overusing `if` inside `location` causes surprising behavior → prefer `try_files`/`map` ("if is evil").51- `proxy_pass` with a trailing slash vs none changes the rewritten URI → know the path-rewrite rule.52- Editing config without `nginx -t` can take down the server on reload → always test first.5354## Examples55```nginx56upstream app { server 127.0.0.1:3000; }5758server {59 listen 80;60 server_name example.com;6162 location /static/ { root /var/www; }6364 location / {65 proxy_pass http://app;66 proxy_set_header Host $host;67 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;68 proxy_set_header X-Forwarded-Proto $scheme;69 }70}71```7273## Further reading74- https://nginx.org/en/docs/beginners_guide.html — beginner's guide to running and configuring nginx75- https://nginx.org/en/docs/dirindex.html — alphabetical directive index7677## Related skills78- ../spring — proxy a Spring Boot app behind Nginx79- ../symfony — serve Symfony's front controller via Nginx + PHP-FPM