使用try_文件和proxy_pass时不允许nginx返回405
我想使用nginx提供一些静态文件,并将restfulapi反向代理到后端。 我用docker把它们都包起来 下面是我的配置文件使用try_文件和proxy_pass时不允许nginx返回405,nginx,nginx-location,nginx-reverse-proxy,Nginx,Nginx Location,Nginx Reverse Proxy,我想使用nginx提供一些静态文件,并将restfulapi反向代理到后端。 我用docker把它们都包起来 下面是我的配置文件/etc/nginx/conf.d/default.conf server { listen 80; # gzip config gzip on; gzip_min_length 1k; gzip_comp_level 9; gzip_types text/plain text/css text/javascript ap
/etc/nginx/conf.d/default.conf
server {
listen 80;
# gzip config
gzip on;
gzip_min_length 1k;
gzip_comp_level 9;
gzip_types text/plain text/css text/javascript application/json application/javascript application/x-javascript application/xml;
gzip_vary on;
gzip_disable "MSIE [1-6]\.";
root /usr/share/nginx/html;
location / {
try_files $uri $uri/ /index.html @backend;
error_page 405 @backend;
}
location /terminal {
proxy_pass http://dns-management-backend:8080;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Real-IP $remote_addr;
}
location @backend {
proxy_pass http://dns-management-backend:8080;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Real-IP $remote_addr;
}
}
静态文件服务正常,但restful api返回我时不允许使用405
以下是nginx的日志
192.168.16.2 - - [19/Sep/2019:02:59:38 +0000] "POST /sys/login HTTP/1.1" 405 796 "http://dns-management.api.my.domain/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.75 Safari/537.36" "113.55.15.1"
最后,我找到了原因,这是错误地使用了
try_files
指令,如果请求url是POST/sys/login
,那么就提供了POST/index.html
。因此发生了405不允许的故障
修复很简单,将
try_files
指令更改为try_files$uri$uri/index.html@backend代码>如果要更改405中的错误代码,请尝试:error\u page 405=@backend代码>@RichardSmith,谢谢你的建议。但是,我找到了根本原因,请参见下面的答案。