绝大多数服务器,都不允许静态文件响应POST请求(GET请求静态文件是天经地义的),否则会返回HTTP/1.1 405 Method not allowed
错误。
然而在前端开发中,前端开发工程师经常模拟后端请求,返回静态数据来查看页面效果,怎么办?
目前有两种方案解决:
在nginx.conf中,请求的静态数据路径中,添加如下语句error_page 405=200 $request_uri
:
location ~ \.(action|jsp) {
root $testDataFold;
error_page 405 =200 $request_uri;
}
找到文件ngx_http_static_module.c
,然后找到下面代码并注释掉。
if (r->method & NGX_HTTP_POST) {
return NGX_HTTP_NOT_ALLOWED;
}
然后按照原来的编译参数,重新编译安装nginx即可。
推荐方案一,简单方便无副作用,^_^。
方案三:说服开发不要对静态文件使用 post 方法
对静态文件使用 POST 方法是无意义的,更改成 GET 方法请求静态文件。
转载了,谢谢作者。
https://www.qiansw.com/nginx-responds-to-the-post-request-in-the-static-file-and-prompts-405-for-the-error-problem.html