【nginx】405 not allowed problem solution

1. Problem description

The first page I see is the page returned by nginx. I know that the error needs to be solved from nginx.

<html>
<head><title>405 Not Allowed</title></head>
<body bgcolor="white">
<center><h1>405 Not Allowed</h1></center>
<hr><center>nginx/1.0.11</center>
</body>
</html>

2. Cause of the problem

Because the static files requested here use the post method, nginx does not allow post to access static resources. Off topic, I tried to post and visited www.baidu.com and found that the page also reported an error. You can try to access it using the get method.

3. Solutions (three types)

(1) Point the 405 error to success (the problem I solved using this method)

Add error_page 405 =200 $uri to the location under the static server;

location / {
    
    
        root /usr/share/nginx/html/cashier;
        try_files $uri $uri/ /index.html;
        index index.html index.htm;
        error_page 405 =200  $request_uri; // $request_uri这个参数的含义下面有解释
}
(2) Modify the src/http/modules/ngx_http_static_module.c file under nginx
if (r->method & NGX_HTTP_POST) {
    
    
     return NGX_HTTP_NOT_ALLOWED;
}

Comment out the above paragraph, recompile, and copy the nginx file generated by make install to sbin and restart nginx.

(3) Modify the error interface pointing
upstream static_backend {
    
    
    server localhost:80;
}
 
server {
    
    
    listen 80;
    # ...
    error_page 405 =200 @405;  // 注意 405后面是要接空格的,而不是因为打错了字符
    location @405 {
    
    
        root /srv/http;
        proxy_method GET;
        proxy_pass http://static_backend;
    }
}
(4) Detailed description

405 Method Not Allowed is an HTTP response status code that indicates that the server received and recognized the specified request HTTP method, but the server rejected the specific method of requesting the resource. This code response confirms that the requested resource is valid and exists, but that the client used an unacceptable HTTP method during the request.

4. Nginx $request_uriand $uridetailed explanation

$uri

What is recorded in nginx $uriis the URL that is finally passed to the backend server after performing a series of internal redirect operations.

Contains the requested file name and path, and does not include “?”或“#”other parameters.

Full URL link: http://www.alipay.com/alipay/index.html
$uri:/alipay/index.html

$request_uri

$request_uriWhat is recorded is the original URL of the current request (including parameters). If no internal redirection is performed, request_urithe value after removing the parameters uriis the same as the value. The troubleshooting problem in the online environment is that if the request seen in the back-end server request_uridoes not match the request stored in Nginx, you can consider urisearching there.
Contains the requested file name and path and all parameters

Full URL link: http://www.alipay.com/alipay/index.html
$request_uri:/alipay/index.html#parameters

Guess you like

Origin blog.csdn.net/hzxOnlineOk/article/details/129378280