The Nginx static file responds to the POST request to prompt the resolution of the 405 error

  • 2020-05-06 12:16:47
  • OfStack

Example 1: send an POST request to HTML static page

on the Apache server with the curl command under linux


[root@localhost ~]# curl -d 11=1 //www.jb51.net/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.jb51.net Port 80</ADDRESS>   
    </BODY>   
</HTML>  

Example 2: send an POST request to the HTML static page

on the nginx server using the curl command under linux


[root@localhost ~]# curl -d 11=1 //www.jb51.net/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 the static file responsive to the POST request.
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.jb51.net;    
    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
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 the following section to comment out


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

Then, according to the original compilation parameters, recompile and install nginx,


Related articles: