405 not allowed Nginx

405 not allowed Nginx fix for POST requests

Today I wanted to write a little useful tip for people trying to do POST requests to static files. Apparently Nginx returns 405 not allowed response in those cases.

Here are the couple workarounds to get you going again:

server {
    listen       80;
    server_name  test.com;

    root   /my/root;

    error_page  405     =200 $uri;

    location / {
        index  index.html index.htm;

    # ...

So this first example does a little trick – every POST ( with 405 ) to static file gets routed back to same uri, but =200 is changing response code to 200. To capture specific urls we may need to create more locations, since Nginx will do additional lookup for errors.

Here slightly more interesting sample (dynamic config):


  error_page 405 = @app;

  location @app {
    proxy_pass http://backend;


In this case we send all POST 405 errors to the named location @app which then proxies the request down to the upstream servers. Also note that in this case we don’t set specific status code, so whatever upstream server return will be set as status code.

The last example I found online, don’t remember where exactly. The idea looks like this:

server {

  listen 80;

  root /my/root;

  error_page 405 = $uri;

  # magic happens here
  rewrite ^.*$ /api.json last;

  location / {
     index index.html;

Lets look what’s going on here – first we send POST request to api.json (which is our static file), then we proxy 405 error to the original url (essentially triggering new Nginx lookup) and our rewrite condition matches the request and return api.json with 200 status code. You can take it even further and wrap rewrite into specific location or update the regex rule to match only some urls.

Wake up, Neo... The matrix has you...

Join our growing UNDERGROUND MOVEMENT of Rain Makers. Just drop your email below and your life will never be the same again.

Feel free to reach out on Twitter, Facebook or Instagram.

I won't send you spam. Unsubscribe at any time. Powered by ConvertKit

Leave a Reply

Your email address will not be published. Required fields are marked *