:source-highlighter: pygments :source-language: nginx == Nginx === Redirecting image requests to webp versions [source] ---- http { map $http_accept $webp_suffix { "~*webp" ".webp"; } server { location ~ \.(png|jpe?g)$ { try_files $uri$webp_suffix $uri =404; expires 1y; } } } ---- === Redirecting to a canonical domain Instead of complex rules to redirect certain requests, just add a separate `server` block: [source] ---- server { server_name www.example.com; return 301 $scheme://example.com$request_uri; } ---- === Logging only partial IP addresses This assumes nginx is behind a proxy that sends a trusted `X-Forwarded-For` header, but the same can easily be done with the remote IP directly. [source] ---- server { map $http_x_forwarded_for $forwarded_anon { ~(?P\d+\.\d+\.\d+)\. $ip.0; ~(?P[^:]+:[^:]+): $ip::; default 0.0.0.0; } log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$forwarded_anon"'; } ----