忽略 Nginx 中的尾部斜杠 proxy_pass
Ignore trailing slash in Nginx proxy_pass
我的 nginx 配置如下:
server {
listen 80 default_server;
listen [::]:80 default_server;
# SSL configuration
#
listen 443 ssl default_server;
listen [::]:443 ssl default_server;
server_name www.example.com;
location @publicsite {
resolver 8.8.8.8;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Connection close;
proxy_pass https://public-site.com$request_uri;
}
location / {
try_files $uri @publicsite;
}
//other locations...
}
使用此配置,当我尝试访问 www.example.com/pricing/
时,它按预期工作。但是当我尝试访问 www.example.com/pricing
时,它的工作方式类似于 https://public-site.com/pricing/
.
有没有办法摆脱这个实域重定向?
输出:
www.example.com/pricing/ => www.example.com/pricing/
www.example.com/pricing => https://public-site.com/pricing/
预计:
www.example.com/pricing/ => www.example.com/pricing/
www.example.com/pricing => www.example.com/pricing/
解决了我的问题
proxy_set_header Host public-site.com;
我的 nginx 配置如下:
server {
listen 80 default_server;
listen [::]:80 default_server;
# SSL configuration
#
listen 443 ssl default_server;
listen [::]:443 ssl default_server;
server_name www.example.com;
location @publicsite {
resolver 8.8.8.8;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Connection close;
proxy_pass https://public-site.com$request_uri;
}
location / {
try_files $uri @publicsite;
}
//other locations...
}
使用此配置,当我尝试访问 www.example.com/pricing/
时,它按预期工作。但是当我尝试访问 www.example.com/pricing
时,它的工作方式类似于 https://public-site.com/pricing/
.
有没有办法摆脱这个实域重定向?
输出:
www.example.com/pricing/ => www.example.com/pricing/
www.example.com/pricing => https://public-site.com/pricing/
预计:
www.example.com/pricing/ => www.example.com/pricing/
www.example.com/pricing => www.example.com/pricing/
解决了我的问题
proxy_set_header Host public-site.com;