我正在尝试创buildredirect来添加尾部斜杠,
location / { if($request_uri ~ ^([^.]*[^/])$){ return 301 https://$host$request_uri/; } }
我不知道如何做匹配,如果我简单地创buildredirect与/ – 它的工作,只是这个正则expression式似乎打破。
重写我试过了
location / { rewrite ^([^.]*[^/])$ $1/ permanent; ..... }
和
if ($http_x_forwarded_proto != "https") { return 301 https://$host$request_uri; }
工作正常,redirect到https,我想与它创build正则expression式
如果我正确地理解了这个问题,则您希望在不使用301redirect的情况下自动投放http://example.com/foo/index.html,当http://example.com/foo请求没有结尾斜杠?
如果是这样,我发现这个try_filesconfiguration工作:
try_files $uri $uri/index.html $uri/ =404;
$uri
uri完全匹配uri $uri/index.html
匹配一个包含index.html的目录,path的最后一个元素与目录名匹配,没有结尾的斜杠 $uri/
匹配目录 =404
返回404错误页面。 很久以前解决了,但我看到很多人检查这个问题,所以如果我没有记错的话,我没有这行代码,它redirect我:
port_in_redirect off;
我知道,我没有提到有问题的端口,没有办法回答,但如果有人遇到同样的问题,我就把它留在这里
这似乎按预期工作:
location / { if ($request_uri ~ ^([^.]*[^/])$) { return 301 https://$host$request_uri/; } try_files $uri $uri/ /index.html; .... }
不确定要看到与您尝试的区别,除了因if
语句中缺less空格(可能是拼写错误)而应该遇到的语法错误。 …也许是因为你有其他的指令,你没有告诉我们这个覆盖这个。
希望我正确理解你(没有足够的代表评论)
要redirect像/ admin这样的目录并添加一个尾部/你可以使用return和一个单独的位置块,例如
location = /admin { return 301 https://$server_name$request_uri/; }
这将然后将请求放入
location / { ......... rest of config }
看看这里的例子,当我遇到类似的问题时,帮了我很多。 http://nginx.org/en/docs/http/ngx_http_core_module.html#location,特别是关于特殊处理和/