nginx

关注公众号 jb51net

关闭
首页 > 网站技巧 > 服务器 > nginx > nginx报错405 not allowed

关于nginx报错405 not allowed解决方法总结

作者:好怪~

这篇文章主要给大家介绍了关于nginx报错405 not allowed解决方法的相关资料,nginx遇到post请求静态文件会得到405错误,文中通过代码介绍的非常详细,也给出了推荐方法,需要的朋友可以参考下

一、报错原因提示:nginx 解决 405 not allowed错误

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

二、解决方式(四种)

1、将405错误指向成功

静态server下的location加入 error_page 405 =200 $uri;(说白了就是强制将405错误用200代替了)

location / {
        root /usr/locai/nginx/html/kt;
        try_files $uri $uri/ /index.html;
        index index.html index.htm;
        error_page 405 =200  $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、允许nginx的post请求访问静态资源,个人感觉是强制把post请求变get了

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;
    }
}

**4、跨服务调用报错解决(亲测有效)

    server {
        listen       8010;
        server_name  localhost;
        location / {
            root   /usr/local/system/efe/dist;
            index  index.html index.htm;
            try_files $uri $uri/ /index.html;
            error_page 405 =200 @405;
        location @405 {
             proxy_set_header Host $host;
             proxy_set_header X-Real-IP $remote_addr;
             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
             #ip为后端服务地址
             proxy_pass http://ip+端口$request_uri ;
        }
   }

总结 

到此这篇关于关于nginx报错405 not allowed解决的文章就介绍到这了,更多相关nginx报错405 not allowed内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文