如何解决Nginx 在位置斜线后重定向
我有域名 https://example.com/API/,我想重定向 /API/ 之后给出的任何内容,例如:
https://example.com/API/test to https://example.com/API/
下面是我的 Nginx 配置
location @error {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
root /var/www/html/test/;
index index.html index.htm;
internal;
}
location ~*/api {
rewrite ^/api(.*) $1 break;
proxy_pass http://127.0.0.1:3100;
client_max_body_size 60M;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
error_page 502 @error;
}
从上面的例子来看,如果 /API/ 得到 502,我将它重定向到。 PHP 文件工作正常,但如果在 /API/test 之后给出任何内容,则显示 404 not found。
解决方法
你可以拥有这样的东西:
server {
root /var/www/html; #your own values
server_name _; #website name
location @error {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
root /var/www/html/test/;
index index.html index.htm;
internal;
}
location /api {
proxy_pass http://127.0.0.1:3100;
client_max_body_size 60M;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
error_page 502 @error;
}
}
所以基本上你必须使用 location /api {}
指令,这会起作用。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。