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

时间:2017-01-28 10:46:08

标签: nginx

我的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

1 个答案:

答案 0 :(得分:7)

使用命名位置和内部重写。例如:

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

有关详情,请参阅this document