繁体   English   中英

Nginx 在位置斜线后重定向

[英]Nginx redirect after slash in location

我有域名https://example.com/API/ ,我想重定向 /API/ 之后给出的任何内容,例如:

https://example.com/API/test to https://example.com/API/

下面是我的 Nginx conf

 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 未找到。

你可以有这样的东西:

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 {}指令,这样就可以了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM