当前位置: 移动技术网 > 网络运营>服务器>nginx > Nginx静态文件响应POST请求 提示405错误的解决方法

Nginx静态文件响应POST请求 提示405错误的解决方法

2019年04月22日  | 移动技术网网络运营  | 我要评论

例1:用linux下的curl命令发送post请求给apache服务器上的html静态页

复制代码 代码如下:

[root@localhost ~]# curl -d 11=1 //www.jb51.net/   
<!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 /.<p>  
        <hr>  
        <address>apache/1.3.37 server at www.jb51.net port 80</address>  
    </body>  
</html> 

例2:用linux下的curl命令发送post请求给nginx服务器上的html静态页

复制代码 代码如下:

[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>

但在有些应用中,需要使静态文件能够响应post请求。
对于nginx,可以修改nginc.conf配置文件,改变“405错误”为“200 ok”,并配置location来解决,方法如下:

复制代码 代码如下:

server   
{   
    listen  80;   
    server_name www.jb51.net;   
    index 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;   
    }   
}  

当然也可以修改nginx源代码来解决
修改源代码,重新编译安装nginx
编辑nginx源代码

复制代码 代码如下:

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

修改: 找到下面一段注释掉

复制代码 代码如下:

/*  
if (r->method & ngx_http_post)  
{  
    return ngx_http_not_allowed;  
}  
*/  

然后按照原来的编译参数,重新编译安装nginx,即可

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网