Apache、IIS、Nginx 等绝大多数 web 服务器,都不允许静态文件响应 POST 请求,否则会返回 “HTTP/1.1 405 Method not allowed” 错误。
# 1、首先看下出现 405 的提示信息
405 Not Allowed
nginx/1.8.1
这是由于 nginx 安装的时候默认静态文件禁止使用 POST 方式请求,出现的禁止访问信息
# 2、有以下三种解决方法
1)将 405 错误指向成功
静态 server 下的 location 加入 error_page 405 =200 $uri;
location ~ ^/better/.*.(htm|html|gif|jpg|jpeg|png|ico|rar|css|js|zip|txt|flv|swf|doc|ppt|xls|pdf|json|ico|htc)$ { | |
<span style="white-space:pre"> </span>root D:/code/BetterjrWeb; | |
<span style="white-space:pre"> </span>error_page 405 =200 $uri; | |
} |
2)修改 nginx 下 src/http/modules/ngx_http_static_module.c 文件
if (r->method & NGX_HTTP_POST) { | |
return NGX_HTTP_NOT_ALLOWED; | |
} |
这一段注释掉,重新编译,不要 make install 编译生成的 nginx 文件复制到 sbin 下 重启 nginx
3)修改错误界面指向 (网上多流传这种方式,但是没有改变请求方法,所以行不通,所以采用以下方法)【我使用的这种方式解决的】
upstream static_backend { | |
server localhost:80; | |
} | |
server { | |
listen 80; | |
# ... | |
error_page 405 =200 @405; | |
location @405 { | |
root /srv/http; | |
proxy_method GET; | |
proxy_pass http://static_backend; | |
} | |
} |