Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Servers, nginx and Docker

POST requests to a Node.js or Express API return 405 Not Allowed from nginx

Error405 Not Allowed

Short 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.

  • Applies to nginx 1.26, Node.js 22, Express 5
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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"
  2. 2Match what you see, then jump to its fix

    1. 1The Express logs show no request at all, and the response body is nginx's own error pageThe matching location has no proxy_passFix
    2. 2limit_except appears in the matching locationA limit_except block allows only some methodsFix
    3. 3The 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

What does the error look like?

  • 405 Not Allowed
  • The 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

Did this fix it?

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_except

2Fix

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

Did this fix it?

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_page

2Fix

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.log

Confirmed by nginx.org

Did this fix it?

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:

Related fixes

Servers

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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.