Nginx如何从$uri中删除前导斜杠

我的Nginx配置文件:

  location / {
    try_files $uri $uri/ /index.php?url=$uri;
  }

  ## PHP conf in case it's relevant 
  location ~ \.php${
  fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
  fastcgi_split_path_info ^(.+\.php)(/.*)$;
  include /etc/nginx/fastcgi.conf;
  fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
  }

请尝试以下网址:http://example.org/login:

预期行为:

http://example.org/index.php?url=login

实际行为:

http://example.org/index.php?url=/login

最佳答案
使用命名位置和内部重写.例如:

location / {
    try_files $uri $uri/ @rewrite;
}
location @rewrite {
    rewrite ^/(.*)$/index.php?url=$1 last;
}

有关更多信息,请参见this document

转载注明原文:Nginx如何从$uri中删除前导斜杠 - 代码日志