Nginx:从基域重写到子目录

Qui*_*Par 7 rewrite nginx

我在http://site.com/blog上托管了一个博客。

如何指示 nginx 重写来自site.comto 的请求site.com/blog

这不应该是永久性的。

Sha*_*den 7

location = / {
    rewrite ^ http://site.com/blog/ redirect;
}
Run Code Online (Sandbox Code Playgroud)

这只会专门为根执行请求。如果您需要捕获所有内容(重定向http://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;
}
Run Code Online (Sandbox Code Playgroud)


小智 5

试试这个:

location = / {
      return 301 /blog/;
 }
Run Code Online (Sandbox Code Playgroud)

这里的关键是“=”符号。