Sub topic 15
Hardening and Rate Limiting
Assume you can use sudo, edit Nginx files, test HTTP requests, and read basic HTTP status codes. The goal of hardening is to reduce information leaks and limit the damage caused by abusive requests. Hardening does not replace application security, operating-system updates, or correct file permissions.
Understand the request path
Nginx receives a connection, selects a server block, matches a location, and then applies directives such as access rules, headers, and rate limits. A rate limit usually uses the client IP address as its key. Nginx stores counters in a shared memory zone so its worker processes can enforce the same limit.
Hide details and restrict files
Place server_tokens off; in the http context to remove the Nginx version from common error pages and response headers. This does not make a vulnerability disappear, but it gives scanners less information.
A common mistake is exposing hidden files, backup files, or configuration fragments through a broad root. Deny files whose names begin with a dot, while allowing the ACME challenge directory if you use an automated certificate client.
http {
server_tokens off;
server {
listen 80;
server_name example.com;
root /var/www/example.com/html;
location ^~ /.well-known/acme-challenge/ {
try_files $uri =404;
}
location ~ /\.(?!well-known) {
deny all;
}
location / {
try_files $uri $uri/ =404;
}
}
}Keep private keys outside web roots and give them restrictive permissions. Do not rely on deny all as a substitute for correct ownership and file modes.
Add useful response headers
Headers can reduce browser-side risks. Add them at the server level or in a shared configuration file.
server {
add_header X-Content-Type-Options "nosniff" always;
add_header X-Frame-Options "SAMEORIGIN" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
}Test application compatibility before adding a strict Content-Security-Policy header. A policy that blocks required scripts can break a site.
Limit abusive request rates
limit_req_zone defines a zone and a request rate. It must be in the http context. The limit_req directive applies that zone to a server or location context.
The following configuration allows about ten requests per second per client IP on /login. A burst of 20 requests can wait briefly or pass immediately when nodelay is used. Requests beyond the burst receive status 503 by default, so this example changes the response to 429, meaning “Too Many Requests.”
http {
limit_req_zone $binary_remote_addr zone=login_per_ip:10m rate=10r/s;
server {
listen 80;
server_name example.com;
location = /login {
limit_req zone=login_per_ip burst=20 nodelay;
limit_req_status 429;
proxy_pass http://127.0.0.1:3000;
}
}
}The binary IP variable uses less memory than $remote_addr. If Nginx sits behind a trusted load balancer, configure real client IP handling first. Otherwise, every request may appear to come from the proxy, causing one client to throttle everyone.
Test safely
Check syntax before every reload. A reload keeps existing connections while workers adopt the new configuration.
sudo nginx -t && sudo systemctl reload nginx
curl -i http://example.com/login
sudo tail -f /var/log/nginx/access.log /var/log/nginx/error.logDo not rate-limit every endpoint with the same value. Login, search, and expensive API operations usually need stricter limits than static files. Account for shared networks, IPv6 addresses, monitoring systems, and legitimate bursts.
Exercise
Why must limitreqzone be declared in the http context, and what status code should a client receive after exceeding the configured limit?
Show answer
The zone is shared by the HTTP configuration and worker processes, so it is declared in http. The example returns HTTP 429, Too Many Requests.