The Nginx static file responds to the POST request and prompts for a 405 error resolution

  • 2020-05-09 19:39:50
  • OfStack

Example 1: send an POST request to the HTML static page on the Apache server with the curl command under linux


[root@localhost ~]# curl -d 11=1 //www.ofstack.com/index.html    
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">   
<HTML>   
    <HEAD>   
        <TITLE>405 Method Not Allowed</TITLE>   
    </HEAD>   
    <BODY>   
        <H1>Method Not Allowed</H1>   
        The requested method POST is not allowed for the URL /index.html.<P>   
        <HR>   
        <ADDRESS>Apache/1.3.37 Server at www.ofstack.com Port 80</ADDRESS>   
    </BODY>   
</HTML>  

Example 2: send an POST request to the HTML static page on the nginx server with the curl command under linux


[root@localhost ~]# curl -d 11=1 //www.ofstack.com/index.htm    
<html>   
    <head><title>405 Not Allowed</title></head>   
    <body bgcolor="white">   
        <center><h1>405 Not Allowed</h1></center>   
        <hr><center>nginx/1.2.0</center>   
    </body>   
</html> 

But in some applications, you need to make static files responsive to POST requests.
For Nginx, you can modify the nginc.conf configuration file, change the "405 error" to "200 ok", and configure location to solve the problem as follows:


server    
{    
    listen  80;    
    server_name www.ofstack.com;    
    index index.html index.htm index.php;    
    root  /opt/htdocs;    
    if (-d $request_filename)    
    {    
        rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;    
    }    
    error_page  405 =200 @405;    
    location @405    
    {    
        root  /opt/htdocs;    
    }    
    location ~ .*\.php?$    
    {    
        include conf/fcgi.conf;         
        fastcgi_pass  127.0.0.1:10080;    
        fastcgi_index index.php;    
    }    
}   

Of course, you can also modify the nginx source code to solve the problem
Modify the source code to recompile and install nginx
Edit nginx source code


[root@localhost ~]# vim src/http/modules/ngx_http_static_module.c   

Modified: find 1 paragraph below to comment out


/*   
if (r->method & NGX_HTTP_POST)   
{   
    return NGX_HTTP_NOT_ALLOWED;   
}   
*/   

Then recompile and install nginx as per the original build parameters


Related articles: