POST to an existing static file
- Result
- 405
- Source behavior
- The static module returns not allowed for POST once the file is found
Fix an nginx 405 Not Allowed: why POST to a static file fails, which methods nginx's static handler accepts, and the proxy, WebDAV and error_page options.
nginx returns 405 Not Allowed when a request method is not accepted for the resource. The classic case is a POST to a static file: nginx's static handler serves GET, HEAD and POST requests but returns 405 for a POST to an existing file, and for any other method. Route the method to an app with proxy_pass or fastcgi_pass, or enable the right module.
Four quick questions, then a straight answer.
From your answers, this is quietly costing you traffic and nobody has the hours to chase it. Scaplet finds every affected page, drafts the fixes and tracks how AI answers mention you.
A data platform shipped 46 fixes without a ticket.
This looks handled. Re-check after a redesign, a migration or a big content push; that is when it breaks.
Your site is small and you have the time. Work through the steps on this page, starting with the pages that get the most traffic.
405 Not Allowed. In the nginx source, the static file handler returns 405 in three situations.POST to an existing static filePUT, DELETE, PATCH, anything but GET, HEAD, POSTPOST to a file that does not existTRACEclient sent TRACE methodCONNECT unless allowedclient sent CONNECT methodSo a form that posts to /contact.html fails even though the file is there. The fix is almost always to send that POST to an application, not to serve it as a file.
curl -i -X GET https://example.com/contact.html # 200
curl -i -X POST https://example.com/contact.html # 405 Not Allowed
curl -i -X OPTIONS https://example.com/contact.html
curl -i -X TRACE https://example.com/ # 405A body titled 405 Not Allowed with Server: nginx came from nginx itself. A 405 with another body came from your app, passed through.
The RFC requires an Allow header on a 405. Check the response; if one is present, it lists the methods the resource accepts.
Run nginx -T and find which location handles the URL. If it ends at a root with no proxy_pass, nginx is serving a file.
location /api/ {
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $host;
}
location / {
root /var/www/site;
try_files $uri $uri/ =404;
}location ~ \.php$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/run/php/php-fpm.sock;
}location = /thanks.html {
error_page 405 =200 $uri;
}The stopgap works because of how error_page behaves: the internal redirect changes the client request method to GET for methods other than GET and HEAD, and =200 changes the response code. Use it for a thank-you page, not for form handling, since the POST body is discarded.
| Cause | Fix |
|---|---|
| WebDAV or API client uses PUT, DELETE, MKCOL, COPY, MOVE | Build nginx with --with-http_dav_module and set dav_methods; the module is not built by default |
| Proxied app returns 405 | The method or route is wrong in the app; nginx passes it on. proxy_method can change the method sent upstream |
Method restricted with limit_except | Review the block; allowing GET also allows HEAD |
Googlebot fetches with GET, which the static handler allows, so a 405 on POST does not block crawling. But Google treats 4xx other than 429 as content that does not exist, so a GET-able page that returns 405 for GET will be dropped from the index over time. Test curl -I as well as POST. See 405 Method Not Allowed for the general status and 405 in Apache for the Apache side.
3 questions people ask next, answered from the sources on this page.
nginx's static handler allows GET, HEAD and POST requests, but returns 405 when a POST targets an existing regular file. Send the POST to an application with proxy_pass or fastcgi_pass.
Find the location serving the URL. If it serves files, route that path to your app. For a static thank-you page you can use error_page 405 =200 $uri;.
RFC 9110 says the origin server MUST generate one, but check your own response with curl -i. nginx's documentation does not describe it, so verify rather than assume.
Run the free audit, then approve the fixes. Every change can be undone.