Fix 405 Method Not Allowed Error on Nginx
This guide walks through the most common causes of the 405 Method Not Allowed error in Nginx and shows you exactly how to fix each one. You’ll learn how to adjust server blocks, handle static file serving, and deal with proxy configurations so your web app stops rejecting valid requests.
What You’ll Need
- Access to your Nginx configuration files (usually in
/etc/nginx/or/usr/local/nginx/conf/) - Root or sudo privileges on the server
- Ability to reload Nginx after changes (
sudo systemctl reload nginxorsudo nginx -s reload) - Basic familiarity with the command line and Nginx config syntax
Step-by-Step
- Identify the exact URL and method that triggers the error. Use your browser’s developer tools (Network tab) or a command-line tool like
curl -X POST https://yourdomain.com/api/endpointto see the response. Note the request method (GET, POST, PUT, DELETE, etc.) and the URI. - Check the Nginx error log. Look at
/var/log/nginx/error.logor the access log for the request. The log often shows which server block or location handled the request, which helps narrow down the issue. - Review the relevant server block and location directives. Open your site’s config file (e.g.,
/etc/nginx/sites-available/example.com). Look for anylocationblocks that might be intercepting the request. Pay special attention tolocation /,location ~ \.php$, or anytry_filesdirectives. - If you’re using a static file server, check for missing index or try_files misconfiguration. A common cause of 405 is when Nginx tries to serve a directory listing but the request method isn’t allowed for that directory. Ensure
autoindexis set appropriately and thattry_filesdoesn’t fall back to a static file that doesn’t support the method. - If you’re proxying to a backend (e.g., Node.js, Python, or another server), verify the proxy_pass setting. The 405 often comes from the backend, not Nginx itself. Check that the backend actually accepts the method. In Nginx, ensure your
locationblock hasproxy_pass http://backend;and that you haven’t accidentally restricted methods withlimit_exceptorifconditions. - Look for any explicit method restrictions in your config. Search for
limit_exceptin your Nginx config. This directive allows you to allow only certain methods. If you see something likelimit_except GET { deny all; }, that will block POST, PUT, DELETE, etc. Remove or adjust that block to allow the needed methods. - If you’re using a reverse proxy with WebSocket or API routes, ensure the correct headers are passed. Some backends require specific headers like
Host,X-Real-IP, orX-Forwarded-For. Missing headers can cause the backend to reject the method. Addproxy_set_header Host $host;and other relevant headers. - Check if the request is being handled by a static file location that doesn’t allow the method. For example, if you have a
location ~ \.(js|css)$block, it might only serve GET/HEAD. If your app needs to POST to a .js URL (unlikely but possible), you’d need to adjust that block. - Test your configuration after any changes. Run
sudo nginx -tto check for syntax errors, then reload Nginx withsudo systemctl reload nginxorsudo nginx -s reload. - If the error persists, temporarily disable any security modules or plugins. Some web application firewalls (like ModSecurity) or Nginx modules can block methods. Try disabling them temporarily to see if the error goes away.
Common Mistakes
- Forgetting that the backend returns the 405. Nginx is just a proxy; if your backend (e.g., Express, Django) doesn’t have a route for the method, it will return 405. Always check the backend logs.
- Using
try_filesincorrectly. A common pattern liketry_files $uri $uri/ /index.php?$query_string;can cause issues if the last fallback doesn’t handle the method. Ensure the fallback is a script that accepts all methods. - Overusing
ifblocks. Nginx’sifis tricky and can lead to unexpected behavior. Avoid usingif ($request_method = POST) { ... }unless you really know what you’re doing. - Not checking the access log. The access log shows the exact request and response code. It can tell you if the request even reached Nginx or if it was blocked earlier.
- Assuming the error is always from Nginx. As mentioned, it’s often the backend. Always verify by looking at the backend’s logs.
Tips
- Use
curlto test methods. For example,curl -X POST -d 'test' http://yourdomain.com/apiwill show you the exact response. Use-vto see headers. - Keep your Nginx config versioned. Use Git or a simple backup before making changes. This makes it easy to revert if something breaks.
- Understand the difference between
405and403. 405 means the method is not allowed, while 403 means forbidden. If you see 403, it’s usually an access control issue, not a method issue. - Check for any
add_headerdirectives that might interfere. Some headers likeAllowcan be set manually, but they don’t cause 405 by themselves. - If you’re using a framework like Laravel or Rails, make sure the routes are defined correctly. The 405 might be from the framework, not Nginx. Test with a simple
curlto the backend directly (bypassing Nginx) to isolate the issue.
Troubleshooting
If you’ve followed the steps and still see the 405, try these additional checks:
- Check if the request is being redirected. Sometimes a 301 redirect can change a POST to a GET, and the new URL doesn’t accept GET. Use
curl -vto see if there’s a redirect. - Look at the full response body. The 405 error page often includes details. If it’s from Nginx, it will say “405 Not Allowed”. If it’s from the backend, it might have a stack trace.
- Try a different browser or tool. Some browsers may cache the response or send a different method. Use a fresh incognito window or a tool like Postman.
- Check for any
locationblocks that use regular expressions. Nginx evaluates regex locations in order. A regex location might be catching your request and not allowing the method. Uselocation ^~to prioritize prefix locations if needed. - If you’re using a CDN or load balancer in front of Nginx, check its settings. The CDN might be stripping the method or returning its own 405. Test by hitting Nginx directly via its IP or hostname.
- Review the Nginx configuration for any
error_pagedirectives. Sometimes a custom error page might be misconfigured, causing a loop or incorrect response.
Frequently Asked Questions
Why does Nginx return 405 for a POST request to a static file?
Nginx’s static file module only supports GET and HEAD by default. If you try to POST to a static file (like a .html or .jpg), Nginx will return 405. This is expected behavior; you should not POST to static files—use a backend script instead.
Can I make Nginx allow POST to static files?
Technically, you could use limit_except to allow POST, but it doesn’t make sense because Nginx won’t do anything with the POST body. It’s better to route POST requests to a backend that can process them.
How do I fix 405 when using a reverse proxy?
Usually, the backend is rejecting the method. Check your backend’s routes and ensure they accept the method. Also, verify that Nginx is passing the request correctly with proxy_pass and not altering the method. Use curl -v to see the exact request sent to the backend.
What is the difference between 405 and 501?
405 means the server knows the method but it’s not allowed for the resource. 501 means the server doesn’t support the method at all. For example, Nginx returns 405 for POST to a static file, but it would return 501 for a method like TRACE if it’s not implemented.
Why does my API return 405 on OPTIONS requests?
OPTIONS requests are often used for CORS preflight. If your backend doesn’t handle OPTIONS, it might return 405. In Nginx, you can handle OPTIONS by adding a location block that returns 204 with appropriate CORS headers, or you can configure your backend to respond to OPTIONS.
Can a firewall or security module cause a 405?
Yes, some Nginx modules like ModSecurity or certain security rules can block methods. If you have such modules enabled, try disabling them temporarily to see if the error goes away. Check the module logs for any blocked requests.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.