Knowledgebase

Find frequently asked questions and setup guides by searching or by browsing the categories.

What 405 Method Not Allowed Means

Every HTTP request has a method: GET to read a page, POST to send a form, and others such as PUT, DELETE, and OPTIONS for APIs. A 405 status code means the server found the address but does not accept that method for it. The HTTP standard asks the server to list the methods it does accept in an Allow header, so check for that header first. Neighboring codes are in HTTP status codes.

What a Visitor Can Try

  • Reload the form page: a form sent from an old or cached page can post to an address that no longer accepts it.
  • Tell the site owner which page and which action caused it if it repeats.

The method is chosen by the page, not the browser, so the real fix is on the server.

Check the Response with curl

curl -i -X POST https://example.com/contact
curl -i -X OPTIONS https://example.com/api/items

Read the status line and look for an Allow header. A value such as "GET, HEAD" means the address is served by something that only reads. An HTML page titled "405 Not Allowed" with an Nginx footer means Nginx itself refused; a JSON body or the application's own error page means the application did.

POST to a Static File in Nginx

Nginx serves static files for GET and HEAD only. A POST that ends at a static file, for example a form whose action is a .html page, gets 405. Send the request to the application instead. For a PHP application, the usual pattern passes everything that is not an existing file to the front controller:

location / {
    try_files $uri $uri/ /index.php?$args;
}

Test with sudo nginx -t before you reload. The PHP side of this setup is covered in PHP-FPM.

Methods Limited in the Nginx Configuration

A limit_except block restricts the methods allowed in a location. Its deny answers with 403, not 405:

location /api/ {
    limit_except GET POST {
        deny all;
    }
    proxy_pass http://127.0.0.1:3000;
}

Allowing GET also allows HEAD. If clients should get a proper 405 for other methods, return it explicitly in the location:

if ($request_method !~ ^(GET|HEAD|POST)$) {
    return 405;
}

When a 405 comes from a configuration someone else wrote, search for both patterns:

sudo grep -rn -e limit_except -e 'return 405' /etc/nginx/

Apache

In Apache, a Limit or LimitExcept section with Require all denied inside answers 403. A 405 from Apache usually has one of these causes:

  • No handler for the method: a PUT or DELETE sent to a static file, or to a folder where WebDAV is not enabled.
  • The allowmethods module: its AllowMethods directive returns 405 for every method not listed in it.
  • An .htaccess file: rules there apply on top of the virtual host; see the .htaccess file.
sudo grep -rn -i -e LimitExcept -e AllowMethods /etc/apache2/ /etc/httpd/ 2>/dev/null

Application Routes

Web frameworks pair each address with the methods it accepts. When an address exists but not for the method sent, the framework returns 405. Add the method to the route, or correct the client so it uses the method the route expects. The details are in the application's own log.

A Redirect That Turns POST into GET

A common hidden cause: a form posts to an http:// address, or to an address without its trailing slash, and the server answers with a 301 or 302 redirect. Browsers repeat the request after such a redirect as a GET, and a target that accepts only POST answers 405. Check with curl -i -X POST whether the first answer is a redirect. Post to the final address directly, or redirect with 307 or 308, which keep the method; see 302 Found and 301 vs 302.

Confirm the Fix

curl -i -X POST -d "test=1" https://example.com/contact

The status should now be 200, 201, or a 303 redirect to a result page, depending on the application. Then send the form in a browser and check that the access log shows the POST line with the new status.

Was this answer helpful?
Loading...