POST requests to a Node.js or Express API return 405 Not Allowed from nginx
405 Not AllowedShort answer
When the 405 page comes from nginx and not from Express, nginx never handed the POST to your app. Find which location matched the URL and make sure it has proxy_pass. Then look for limit_except blocks that restrict methods, and for an error_page rule that re-sends the request as a GET.
Fix it fast
1Find out which cause you have
Dump the full running config and find which location block your URL falls into. The one that matches decides what happens to the POST.
sudo nginx -T | grep -n -E "location|proxy_pass|limit_except|error_page"2Match what you see, then jump to its fix
- 1
The Express logs show no request at all, and the response body is nginx's own error pageThe matching location has no proxy_passFix - 2
limit_except appears in the matching locationA limit_except block allows only some methodsFix - 3
The config has error_page 405 (or another code) pointing to a URI, and the form or API call arrives as GETAn error_page rule turns the POST into a GETFix
- 1
What does the error look like?
405 Not AllowedThe error page is served by nginx, and the Express logs show no request
Cause 1 of 3
The matching location has no proxy_pass
1Check
proxy_pass is the directive that maps a location to the proxied server. A POST to a path covered by a location without it is not sent to your app. Compare the location that matches your API path with the one that has proxy_pass.
sudo nginx -T | grep -n -B2 -A6 "location"2Fix
Put proxy_pass in the location that covers the API path. Without a URI after the address, the path is passed on unchanged.
location /api/ {
proxy_pass http://127.0.0.1:3000;
}3Verify
Reload nginx and send the same POST.
sudo nginx -t && sudo systemctl reload nginx
curl -i -X POST https://example.com/api/contact -H "Content-Type: application/json" -d '{}'Confirmed by nginx.org
Cause 2 of 3
A limit_except block allows only some methods
1Check
limit_except limits the allowed HTTP methods in a location, and listing GET also allows HEAD. If POST is not listed, and the block denies the rest, POST is refused.
sudo nginx -T | grep -n -A5 limit_except2Fix
List every method the API uses, or remove the block if method filtering belongs in the app.
location /api/ {
proxy_pass http://127.0.0.1:3000;
limit_except GET POST {
deny all;
}
}3Verify
Reload nginx and repeat the POST.
sudo nginx -t && sudo systemctl reload nginx
curl -i -X POST https://example.com/api/contact -d 'a=1'Confirmed by nginx.org
Cause 3 of 3
An error_page rule turns the POST into a GET
1Check
When error_page sends the request to another URI, nginx changes the method to GET for every method except GET and HEAD, so the body is lost. Search for error_page rules that handle 405 or that were added to hide it.
sudo nginx -T | grep -n error_page2Fix
Remove the error_page 405 workaround and fix the real cause (the two above). Keep error_page only for static error pages.
# delete a line such as:
# error_page 405 =200 $uri;3Verify
Reload nginx and check that the app now receives a POST.
sudo nginx -t && sudo systemctl reload nginx
sudo tail -f /var/log/nginx/access.logConfirmed by nginx.org
How do I stop it coming back?
- Keep API routes under one prefix with one proxy_pass location, so no other location can catch them.
- Run sudo nginx -T after every config change and read the matching location.
- Log a POST test to the access log after each deploy.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- WorkforceA field app, an admin console and a self-hosted Node.js server for push and reconciliation.
- stegnor.comMy company site: 804 pages from a Python and Jinja generator, with 15 free browser tools.
Related fixes
Servers
Node.js app says Cannot find module only when it runs under Docker Compose
The same image starts fine with docker run but fails with docker compose up
The image installed node_modules, but a Compose volume mounted over the app folder hides them.
3 causes, each with a fixServers
A Lambda function fails to send its response back, with HTTP 413
LAMBDA_RUNTIME Failed to post handler success response. Http response code: 413
HTTP 413 means the response the function tried to return is too big.
2 causes, each with a fixServers
nginx 502 Bad Gateway in front of a Node.js app
connect() failed (111: Connection refused) while connecting to upstream
A 502 means nginx reached for your Node.js app and got nothing usable back.
5 causes, each with a fixServers
nginx gives up waiting for the app and returns 504
upstream timed out (110: Connection timed out) while reading response header from upstream
nginx returns 504 when the app behind it takes the request but sends nothing back within proxy_read_timeout (60 seconds by default), or when the connection cannot be opened in time.
5 causes, each with a fixServers
The nginx container serves the default page or 404 because /usr/share/nginx/html has no app files
requests for your pages return 404 Not Found
The official nginx image serves whatever sits in /usr/share/nginx/html.
3 causes, each with a fixServers
Pages load unstyled because /_next/static files return 404 behind nginx
GET https://example.com/_next/static/chunks/main.js 404 (Not Found)
The browser asks for /_next/static/..
3 causes, each with a fix