關于nginx報錯405?not?allowed解決方法總結(jié)
一、報錯原因提示:nginx 解決 405 not allowed錯誤
問題產(chǎn)生原因:因為這里請求的靜態(tài)文件采用的是post方法,nginx是不允許post訪問靜態(tài)資源。題話外,試著post訪問了下www.baidu.com發(fā)現(xiàn)頁面也是報錯,可以試著用get方式訪問
二、解決方式(四種)
1、將405錯誤指向成功
靜態(tài)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請求訪問靜態(tài)資源,個人感覺是強制把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、跨服務調(diào)用報錯解決(親測有效)
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 ;
}
}總結(jié)
到此這篇關于關于nginx報錯405 not allowed解決的文章就介紹到這了,更多相關nginx報錯405 not allowed內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Nginx代理Vue項目出現(xiàn)Invalid Host header問題及解決
在使用Nginx的upstream對Vue項目進行負載均衡時,如果代理地址無法訪問目標地址且頁面報錯InvalidHostheader(無效主機頭),可能是由于Vue項目的主機檢查配置導致的,解決方法是在Vue項目的webpack.dev.js文件中的devServer下添加disableHostCheck:true,跳過主機檢查2024-12-12
解決Nginx配置靜態(tài)資源文件404 Not Found問題
在使用Nginx作為靜態(tài)資源服務器時,如果配置了根目錄root導致404錯誤,而使用前綴URL配置alias則需要正確處理目錄路徑,使用alias時要確保目錄名后加‘/’,并且在需要時使用root和alias配置,本文介紹Nginx配置靜態(tài)資源文件404 Not Found問題解決方法,感興趣的朋友一起看看吧2025-03-03
nginx 不同的訪問路徑對應項目不同的目錄的實現(xiàn)方法
要在 Nginx 中配置不同的訪問路徑對應不同的項目目錄,可以使用 Nginx 的?location?指令來實現(xiàn),本文主要介紹了nginx不同的訪問路徑對應項目不同的目錄的實現(xiàn)方法,具有一定的參考價值,感興趣的可以了解一下2023-09-09

