AlgoViz

NGINX

medium 28 min

NGINX (say "engine x") is a web server, reverse proxy, load balancer and response cache.

Three of its ideas in Python

Read it, run it, change a line and run it again.

Loading editor…

Smooth weighted round robin, the leaky bucket of `limit_req`, and a one-second cache with a lock. A model for learning, not a proxy. Config, commands and client notes are in the variants.

The same in nginx
# WebSocket and event streams through NGINXmap $http_upgrade $connection_upgrade {    default upgrade;    ''      close;}upstream chat {    zone chat 64k;    hash $cookie_session consistent;    server 10.0.0.1:8080;    server 10.0.0.2:8080;}server {    listen 80;    location /ws/ {        proxy_pass http://chat;        proxy_http_version 1.1;        proxy_set_header Upgrade $http_upgrade;        proxy_set_header Connection $connection_upgrade;        proxy_read_timeout 1h;    }    location /events/ {        proxy_pass http://chat;        proxy_buffering off;    }}
The same in Shell
# Is the configuration valid, and what is really loaded?nginx -tnginx -T | grep -n "proxy_cache"# Apply it without dropping requestsnginx -s reload# Where does the cache answer?curl -s -o /dev/null -D - http://localhost/api/products | grep -i x-cache-status# Which modules were built in?nginx -V