翼度科技»论坛 云主机 服务器技术 查看内容

Nginx的405 not allowed错误解决方法

10

主题

10

帖子

30

积分

新手上路

Rank: 1

积分
30
1、问题情况

首先看到的页面是nginx返回的页面,得知错误要从nginx上来解决
  1. <html>
  2. <head><title>405 Not Allowed</title></head>
  3. <body bgcolor="white">
  4. <center><h1>405 Not Allowed</h1></center>
  5. <hr><center>nginx/1.0.11</center>
  6. </body>
  7. </html>
复制代码
2、问题原因

因为这里请求的静态文件采用的是post方法,nginx是不允许post访问静态资源。题话外,试着post访问了下www.baidu.com发现页面也是报错,可以试着用get方式访问

3、问题解决

现贴出三种解决方式,供大家选择:
1、将405错误指向成功(我采用的这种方法解决的问题)
静态server下的location加入 error_page 405 =200 $uri;
  1. location / {
  2.         root /usr/share/nginx/html/cashier;
  3.         try_files $uri $uri/ /index.html;
  4.         index index.html index.htm;
  5.         error_page 405 =200  $request_uri;
  6.     }
复制代码
2、修改nginx下src/http/modules/ngx_http_static_module.c文件
  1. if (r->method & NGX_HTTP_POST) {
  2.      return NGX_HTTP_NOT_ALLOWED;
  3. }
复制代码
以上这一段注释掉,重新编译,将make install编译生成的nginx文件复制到sbin下 重启nginx
3、修改错误界面指向(网上多流传这种方式,但是没有改变请求方法,所以行不通,所以采用以下方法)
  1. upstream static_backend {
  2.     server localhost:80;
  3. }
  4. server {
  5.     listen 80;
  6.     # ...
  7.     error_page 405 =200 @405;
  8.     location @405 {
  9.         root /srv/http;
  10.         proxy_method GET;
  11.         proxy_pass http://static_backend;
  12.     }
  13. }
复制代码
4、拓展

405 Method Not Allowed是一个HTTP 响应状态代码,表示服务器接收并识别了指定的请求HTTP 方法,但服务器拒绝了请求资源的特定方法。此代码响应确认请求的资源有效且存在,但客户端在请求期间使用了不可接受的 HTTP 方法。
到此这篇关于Nginx的405 not allowed错误解决方法的文章就介绍到这了,更多相关Nginx 405 not allowed内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

来源:https://www.jb51.net/article/283875.htm
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

举报 回复 使用道具