brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: nginx limit_except allows only the HTTP methods a location needs

Nginx
~/tips/tip-nginx-limit-except

location /api/public/ {

Blocks POST, PUT and DELETE on paths that should only ever be read.

A static site or a read-only API has no reason to accept POST or DELETE. Requests like that are usually scanners. limit_except refuses them before they reach anything.

location /api/public/ {
    limit_except GET {
        deny all;
    }
    proxy_pass http://127.0.0.1:8081;
}

The methods after limit_except are the ones that stay allowed, and the block applies to everything else. GET automatically includes HEAD. A POST then gets a 403 and a GET goes through as normal.

Inside the block you can use more than deny all. allow 203.0.113.10; deny all; lets one address still write, and auth_basic asks for a login only for write methods while reading stays open.

The syntax is easy to read backwards: you list what you allow, not what you block. It also returns 403, not the 405 Method Not Allowed that the HTTP spec suggests, so a client sees “forbidden” rather than a hint about the method. If that matters to you, an if ($request_method !~ ^(GET|HEAD)$) { return 405; } in the location does it. Remember CORS preflight too: a browser on another domain sends OPTIONS first, so I’d add it to the list if such pages call your API.

Further reading: limit_except

Going deeper: ๐Ÿ›œ Guide: AdGuard Home + Tailscale (Secure Split-Horizon DNS)

More Nginx tips sudo certbot --nginx -d example.com -d www.example.com /data/nginx/custom/server_redirect.conf map $http_upgrade $connection_upgrade { proxy_intercept_errors on; proxy_redirect http://localhost:3000/ /app/; All Nginx tips (50) All topics

Link