关于nginx报错405

06-18 1480阅读

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

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

关于nginx报错405
(图片来源网络,侵删)

二、解决方式(四种)

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 ;
        }
   }
VPS购买请点击我

文章版权声明:除非注明,否则均为主机测评原创文章,转载或复制请以超链接形式并注明出处。

目录[+]