重写一个子目录到nginx的根目录

假设我有一个站点http://domain/并且将一些文件放在子目录/html_root/app/并使用以下重写规则将此文件夹重写为我的根目录:

 location / { root /html_root; index index.php index.html index.htm; # Map http://domain/x to /app/x unless there is ax in the web root. if (!-f $request_filename){ set $to_root 1$to_root; } if (!-d $request_filename){ set $to_root 2$to_root; } if ($uri !~ "app/"){ set $to_root 3$to_root; } if ($to_root = "321"){ rewrite ^/(.+)$ /app/$1; } # Map http://domain/ to /app/. rewrite ^/$ /app/ last; } 

我知道这不是一个聪明的方法,因为我有另一个子目录/html_root/blog/ ,我希望它可以通过http://domain/blog/

我的问题是,上面的重写规则工作正常,但仍然有一些问题:如果我访问

http://domain/a-simple-page/ (从http://domain/app/a-simple-page/重新编译)

它工作正常,但如果我访问

http://domain/a-simple-page (没有结尾的斜杠),它redirect到原始地址:

http://domain/app/a-simple-page/

任何方式来redirect的URL没有后斜线按照我的规则?

经典的错误教程,而不是阅读wiki我强烈build议阅读关于你应该使用的function(如location和try_files)以及我的Nginx入门,因为你完全错过了Nginx的基础知识。

我试图用正确的格式写出你想要的东西,但是我不能保证它能正常工作,因为我不确定我实际上是否明白你要做什么,不过,它应该给你一个基础, 。

 server { listen 80; server_name foobar; root /html_root; index index.php index.html index.htm; location / { try_files $uri $uri/ @missing; } location /app { # Do whatever here or leave empty } location @missing { rewrite ^ /app$request_uri?; } }