Nginx从variables中删除字符

基本上我想要做的是把请求的URL放入一个variables,然后删除一个字符(如/)并获得一个新的variables。

我试图在URL的末尾实现尾部的斜杠; 这工作,但我想要做的所有文件:

location /examplehtmlfile/ { try_files = $uri $uri/ examplehtmlfile.html } 

否则,如果我在最后添加一个尾部的斜杠它会产生一个404错误。

所以我想要做的是添加到try_files指令(对于主/位置)是这样的:

 try_files = $uri $uri/ /$uriwithoutslash.html /$uriwithoutslash.php 

谢谢

如果包含尾部斜线,则需要重写$urivariables。 这是一个内部重写,所以它不会影响您的客户看到的URL。

 location ~ ./$ { rewrite ^(.+)/$ $1 last; } 

你的主要位置只能testing静态内容的存在。 PHP文件需要在不同的位置块中处理。

 location / { try_files $uri $uri.html $uri/index.html @php; } 

PHP文件的存在可以在命名的位置进行testing:

 location @php { try_files $uri.php $uri/index.php =404; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_pass ...; } 

=404可以replace为默认的URI,例如: /index.php

root指令也是必需的,但是没有查询index指令。

编辑:

如果您需要支持扩展名为.php URI,您可以重写它们并添加斜杠或复制PHP位置块。 或者:

 location ~ \.php$ { rewrite ^(.*)\.php$ $1/ last; } 

要么:

 location ~ \.php$ { try_files $uri =404; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_pass ...; } 

感谢@RichardSmith,它已经解决了!

这是我最后的configuration: http : //pastebin.com/TdAz9ad9