Nginx:从基本域重写到一个子目录

我有一个在http://site.com/blog上托pipe的博客。

我如何指示nginxsite.com请求重写site.com/blog

这不应该是永久性的。

 location = / { rewrite ^ http://site.com/blog/ redirect; } 

这只会请求专门为根。 如果你需要抓住一切(redirecthttp://site.com/somearticle/something.htmlhttp://site.com/blog/somearticle/something.html ),那么你需要更多的参与:

 location /blog/ { # Empty; this is just here to avoid redirecting for this location, # though you might already have some config in a block like this. } location / { rewrite ^/(.*)$ http://site.com/blog/$1 redirect; } 

这没有为我工作。 这是什么工作:

  1. 打开您的网站的NGINXconfiguration文件。 在服务器块内部,将path添加到根目录并设置文件的优先顺序:

     root /mnt/www/www.domainname.com; index index.php index.html index.htm; 
  2. 所有其他位置块之前创build一个空的位置块:

     location /latest { # Nothing in here; this is to avoid redirecting for this location } 
  3. 在您的位置/ {}块中注释掉根目录指令,并添加redirect,如下所示:

     location / { # root /mnt/www/www.domainname.com; index index.php index.html index.htm; rewrite ^/(.*)$ http://www.domainname.com/latest/$1 redirect; } 
  4. 确保你的位置〜.php $块指向它的根目录

     root /mnt/www/www.domainname.com; 

这为我修好了。

试试这个:

 location = / { return 301 /blog/; } 

这里的关键是'='符号。