Nginx 405 Not Allowed: why POST is rejected
Short answer
nginx answering 405 Not Allowed usually means a POST (or PUT, DELETE) request ended up on a static file, which only accepts GET and HEAD. Typically the API path was not handed to proxy_pass / fastcgi_pass and try_files fell back to index.html. Route the API location to the backend.
Most common cause: An SPA's try_files $uri /index.html also sends POST /api/... to index.html.
Try this first: Send the API to the backend
location /api/ {
proxy_pass http://127.0.0.1:3000;
proxy_set_header Host $host;
}
location / {
try_files $uri $uri/ /index.html;
}Last verified: · against the official docs listed below · Sources
Typical log lines
"POST /api/login HTTP/1.1" 405 157 405 Not Allowed
Common causes
- An SPA's try_files $uri /index.html also sends POST /api/... to index.html.
- The API location prefix is wrong (/api vs /api/), so requests miss proxy_pass.
- A form posts to a static .html page.
- A limit_except GET method restriction is in place.
Diagnosis commands
curl -si -X POST https://example.com/api/login | head -5nginx -T 2>/dev/null | grep -nE 'location|try_files|proxy_pass|limit_except'Fix configs
Send the API to the backend
location /api/ {
proxy_pass http://127.0.0.1:3000;
proxy_set_header Host $host;
}
location / {
try_files $uri $uri/ /index.html;
}Run nginx -t first, then systemctl reload nginx.
Key facts and sources
- limit_except restricts the HTTP methods allowed in a location; allowing GET also allows HEAD. [nginx.org]
- error_page with a URI does an internal redirect and changes methods other than GET and HEAD to GET; pointing it at a named location (error_page 405 = @name) keeps the URI and method. [nginx.org]
Checked against the linked pages on 2026-11-14; the official docs win if a newer nginx changes them.
FAQ
Can I work around it with error_page 405 =200 $uri?
It makes the page render, but the POST body is thrown away and nothing handles it. Send the request to the backend that should process it.
How do I tell nginx's 405 from the app's?
nginx's 405 page has an nginx footer and Server: nginx; an app's 405 usually carries an Allow header and its own error format.
Fix it with a tool
Related errors
VPS / CDN recommendations may appear here later, labelled as affiliate links. There are no referral links today.