目录
  • 一、问题描述
  • 二、问题原因
  • 三、解决办法(三种)
    • (1)将405错误指向成功(我采用的这种方法解决的问题)
    • (2)修改nginx下src/http/modules/ngx_http_static_module.c文件
    • (3)修改错误界面指向
    • (4)详细描述
  • 四、Nginx $request_uri和$uri详解
    • $uri
    • $request_uri
  • 总结

    一、问题描述

    首先看到的页面是nginx返回的页面,得知错误要从nginx上来解决

    <html>
    <head><title>405 Not Allowed</title></head>
    <body bgcolor="white">
    <center><h1>405 Not Allowed</h1></center>
    <hr><center>nginx/1.0.11</center>
    </body>
    </html>

    二、问题原因

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

    三、解决办法(三种)

    (1)将405错误指向成功(我采用的这种方法解决的问题)

    静态server下的location加入 error_page 405 =200 $uri;

    location / {
            root /usr/share/nginx/html/cashier;
            try_files $uri $uri/ /index.html;
            index index.html index.htm;
            error_page 405 =200  $request_uri; // $request_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;  // 注意 405后面是要接空格的,而不是因为打错了字符
        location @405 {
            root /srv/http;
            proxy_method GET;
            proxy_pass http://static_backend;
        }
    }

    (4)详细描述

    405 Method Not Allowed是一个HTTP 响应状态代码,表示服务器接收并识别了指定的请求HTTP 方法,但服务器拒绝了请求资源的特定方法。此代码响应确认请求的资源有效且存在,但客户端在请求期间使用了不可接受的 HTTP 方法。

    四、Nginx $request_uri和$uri详解

    $uri

    nginx中的$uri记录的是执行一系列内部重定向操作后最终传递到后端服务器的URL

    包含请求的文件名和路径,不包含“?”或“#”等参数。

    完整URL链接:http://www.alipay.com/alipay/index.html
    $uri:/alipay/index.html

    $request_uri

    $request_uri记录的是当前请求的原始URL(包含参数),如果没有执行内部重定向操作,request_uri去掉参数后的值和uri的值是一样的。在线上环境中排查问题是,如果在后端服务器中看到的请求和Nginx中存放的request_uri无法匹配,可以考虑去uri里边进行查找。

    包含请求的文件名和路径及所有参数

    完整URL链接:http://www.alipay.com/alipay/index.html
    $request_uri:/alipay/index.html#参数

    总结

    到此这篇关于nginx 405 not allowed问题三种解决方法的文章就介绍到这了,更多相关nginx 405 not allowed问题内容请搜索本网站以前的文章或继续浏览下面的相关文章希望大家以后多多支持本网站!

    您可能感兴趣的文章:

    • 关于nginx报错405 not allowed解决方法总结
    • nginx版本号隐藏(附405 not allowed解决办法)
    • nginx部署前端post请求405 not allowed问题解决
    • nginx代理转发报错405 Method Not Allowed解决
    • Nginx的405 not allowed错误解决方法
    • Nginx静态文件响应POST请求 提示405错误的解决方法
    • nginx部署到服务器后文件上传提示405