Skip to content

Instantly share code, notes, and snippets.

@zxhfighter
Last active May 27, 2021 14:55
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zxhfighter/7560812 to your computer and use it in GitHub Desktop.
Save zxhfighter/7560812 to your computer and use it in GitHub Desktop.
Nginx将静态文件响应POST请求,提示405错误问题

Nginx的405错误

绝大多数服务器,都不允许静态文件响应POST请求(GET请求静态文件是天经地义的),否则会返回HTTP/1.1 405 Method not allowed错误。

然而在前端开发中,前端开发工程师经常模拟后端请求,返回静态数据来查看页面效果,怎么办?

目前有两种方案解决:

方案一:修改配置文件nginx.conf(推荐


在nginx.conf中,请求的静态数据路径中,添加如下语句error_page 405=200 $request_uri

location ~ \.(action|jsp) {
    root $testDataFold;
    error_page 405 =200 $request_uri;
}

方案二:修改nginx源码编译


找到文件ngx_http_static_module.c,然后找到下面代码并注释掉。

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

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

推荐方案一,简单方便无副作用,^_^。

@internelp
Copy link

方案三:说服开发不要对静态文件使用 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment