Knowledge

Compression, static caching and rate limiting with nginx limit_req

Updated 2026-11-27 · Present

  • gzip compresses text responses to save bandwidth; images and already-compressed files should not be compressed again.
  • Static files whose names carry a version can be cached by browsers for a long time with expires and Cache-Control.
  • limit_req limits requests per second per address, with burst to tolerate short spikes.

Goal

After this article you configure nginx 1.24.x on Ubuntu 24.04 LTS to compress text responses, let browsers cache static files, and limit the request rate so one client cannot overload the application. It assumes you already have a server acting as a reverse proxy for the application.

Preparation

Steps

Steps

Step 1: turn on gzip compression

Place this in the http block (or in server):

gzip on;
gzip_comp_level 5;
gzip_min_length 1024;
gzip_vary on;
gzip_proxied any;
gzip_types text/css application/javascript application/json image/svg+xml;

Step 2: cache static files in the browser

location /static/ {
    alias /var/www/app/static/;
    expires 30d;
    add_header Cache-Control "public, immutable";
}

expires 30d sets the Expires header and a matching Cache-Control: max-age of 30 days. The safe approach is to cache for a long time only files whose names change when their content changes (like app.3f9c1.css), because the browser will not ask again for 30 days. If a file keeps its name while its content changes, users will see the old version. In nginx, an add_header at location level replaces the add_header directives inherited from the outer level, so if you set security headers at server level, repeat them here.

Step 3: cache responses from the application (optional)

You can let nginx store the application's responses for a short time:

# in the http block
proxy_cache_path /var/cache/nginx/app levels=1:2 keys_zone=appcache:10m max_size=1g inactive=60m;

# in location /
proxy_cache appcache;
proxy_cache_valid 200 10m;
add_header X-Cache-Status $upstream_cache_status;

Cache only pages that are identical for every user. Do not cache personalised pages or pages that require login, because one person could see another person's data. The X-Cache-Status header shows the result, such as MISS, HIT or EXPIRED, and many other values, which helps you verify.

Step 4: rate limiting with limit_req

Declare the memory zone in the http block, then apply it in a location:

limit_req_zone $binary_remote_addr zone=perip:10m rate=10r/s;
limit_req_status 429;

location /api/ {
    limit_req zone=perip burst=20 nodelay;
    proxy_pass http://app_backend;
}

Note: if nginx sits behind another proxy or a CDN, $binary_remote_addr is that proxy's address, and all users share one counter. In that case you must configure nginx to obtain the real client address (the realip module) before using per-address limits.

Verification

curl -s -H "Accept-Encoding: gzip" -I -H "Host: app.example.com" http://127.0.0.1/static/app.3f9c1.css
for i in $(seq 1 40); do curl -s -o /dev/null -w "%{http_code}\n" -H "Host: app.example.com" http://127.0.0.1/api/; done | sort | uniq -c

The first command should show Content-Encoding: gzip and the Cache-Control you set. The second sends 40 requests in a row: you will see some 200 codes and the rest 429 once you exceed the rate plus the burst. Look at error.log to see the lines where nginx records the rejections.

Common errors

Summary

Compression reduces the amount of data to transfer, caching reduces the number of times work must be done, and limit_req protects the application from over-eager clients. All three are a few lines of configuration, but you should measure and verify with curl that they behave as intended before using them for real.

References