Nginx Cookbook
Nginx Coookbook#
Basics#
Check version:
nginx -v
nginx version: nginx/1.20.1
Check it is running:
ps -ef | grep nginx
root 270819 1 0 Nov06 ? 00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
www-data 353025 270819 0 Nov11 ? 00:19:26 nginx: worker process
www-data 353026 270819 0 Nov11 ? 00:16:39 nginx: worker process
Nginx will always have 1 master and 1 or more worker processes. The master is running as
rootas nginx needs elevated privileges to function properly.
Directories#
/etc/nginx/- configuration/etc/nginx/nginx.conf- global settings: worker process, tuning, logging and dynamic modules. A top levelhttpblock./etc/nginx/conf.d/- HTTP server configuration files. Files ending in.confare included in the top levelhttpblock. In some distros thesites-enabledandsites-availablefolders are used - but this convention is deprecated./var/log/nginx/- default log location
Commands#
nginx -h- helpnginx -v- nginx versionnginx -V- build and conf infonginx -t- test the nginx configurationnginx -T- tests and prints validated infonginx -s signal- sends a signal to the nginx master process. Eg.stop, quit, reload, reopennginx -s reload- graceful reload
Serving Static Content#
Overwrite /etc/nginx/conf.d/default.conf with:
server {
listen 80 default_server;
server_name www.example.com;
location / {
root /usr/share/nginx/html;
index index.html index/htm;
}
}
- This shares static content over port 80 from
/usr/share/nginx/html - The
serverblock defines a new context for nginx to listen for. listendirects nginx to listen on port 80.server_namedefines the hostname to server requests from - if notdefault_servernginx would only direct requests here if the HTTP host heads matched theserver_name.locationblock defines a configuration based on the path in the url.roottells nginx where to look for static files
More info on location matching in the nginx docs
location = /is an exact match - speeds up processing if it happens frequently~*and~precede regular expressions (case-insensitive and sensitive)^~if the longest matching prefix location modifier the reg ex is not checked
2. High-Performance Load Balancing#
- Share the load among multi upstreams from horizontal scaling
- Intelligence is required to keep a tracking cookie or routing for clients for a session
- Load balancer should be smart enough to detect upstream failure
- active vs passive health checks
HTTP Load Balancing#
Distribute load between 2 or more HTTP servers
upstream backend {
server 10.10.12.45:80 weight=1;
server app.example.com:80 weight=2;
server spare.example.com:80 backup;
}
server {
location / {
proxy_pass http://backend;
}
}
- This config balances load across 2 HTTP servers on port 80 and defines 1
backup- for when the primaries are not available. upstream- controls the load balancing for HTTP. A pool of destinations.weightdefines the weight in the balancing algorithm.
TCP Load Balancing#
Distribute load between 2 or more TCP servers
stream {
upstream mysql_read {
server read1.example.com:3306 weight=5;
server read2.exmaple.com:3306;
server 10.10.12.34:3306 backup;
}
server {
listen 3306;
proxy_pass mysql_read;
}
}
serverblock directs nginx to listen on port3306and balance traffic between 2 MySQL read replicas.-
This config must not be added to
conf.das that is added to thehttpblock. You would create a folder calledstream.conf.dand useincluseinnginx.confstream { include /etc/nginx/stream.conf.d/*.conf; }
then in /etc/nginx/stream.conf.d/mysql_reads.conf:
upstream mysql_read {
server read1.example.com:3306 weight=5;
server read2.example.com:3306;
server 10.10.12.34:3306 backup;
}
server {
listen 3306;
proxy_pass mysql_read;
}
httpandstreamoperate at different layers of the OSI model.- The
httpcontext operates atlayer 7andstreamoperates at transportlayer 4.
UDP Load Balancing#
Distribute load between 2 or more tcp servers
stream {
upstream ntp {
server ntp1.example.com:123 weight=2;
server ntp2.example.com:123;
}
server {
listen 123 udp;
proxy_pass ntp;
}
}
- set udp load balancing using
udpon thelistendirective - If the service requires multiple packets to be sent back and forth use
reuseporton thelistendirective. Eg. OpenVPN, VOIP, Virtual desktop and DTLS (Datagram Transport Later Security)
Load Balancing Discussion#
Why does one need a load balancer when multiple hosts in a DNS A or SERV (service) record can be used?
- Alternate balancing algorithms
- Allowing load balancing over DNS servers themselves
UDP is relied upon with: DNS, NTP, QUIC, HTTP/3 and VOIP
Load Balancing Methods#
Round Robin load balancing does not fit your requirement as heterogenous workloads and server pools are used
Use one of: least connections, least time, generic hash, random or IP hash
upstream backend {
least_conn;
server backend1.example.com;
server backend2.example.com;
}
- round robin: default load-balancing method
least_conn- least connections new requests for to the upstream server with the least connectionslast_time- (only nginx plus) favours servers with lower response timehash- generic hash a hash is generated to direct trafficrandomip_hash- uses client ip as a hash
Lots of Nginx Plus stuff - seems the book’s main purpose is to upsell…
Passive Health Checks#
Active health checks are only available with Nginx Plus
upstream backend {
server backend1.example.com:1234 max_fails=3 fail_timeout=3s;
server backend2.example.com:1234 max_fails=3 fail_timeout=3s;
}
- watches for failed and timed out connections
3. Traffic Management#
…