nginx文件夹redirect

我正在尝试从nginx.conf中的确切文件夹redirect

给定URL:domain.com/path1/path2/path3

redirect到:sub.domain.com/path1/path2/path3

以下是我到目前为止:

location ~* ^/path1[\/?]$ { rewrite ^/(.*) http:sub.domain.com/$1 break; } 

我曾与之合作过

 location /path1 { rewrite ^/(.*) http:sub.domain.com/$1 break; } 

这个问题是它也redirect页面像domain.com/path1moretext/sotherotherpath到sub.domain.com/path1moretext/sotherotherpath

这不是我想要的。 (必须在上面的href代码中取出“//”,因为这是我的第一篇文章,对不起)。

 location / { rewrite ^\/path1\/(.*)$ http://sub.domain.com/$1 last; // rest of config for root } 

我来自Apache背景,我刚刚开始认真地使用Nginx,我已经为重写自己而苦苦挣扎,但是,我最近使用了上述内容,没有明显的问题。

 location = /path1 { rewrite ^ http://sub.domain.com$uri permanent; } location /path1/ { rewrite ^ http://sub.domain.com$uri?$args permanent; } 

编辑:也阅读这个最后/rest/永久/redirect的信息。