使用 nginx 将 301 从子域重定向到特定域 url

Redirect 301 from subdomain to domain specific url with nginx

我现在有两个博客站点。

A - blog.domain.com - 在 Tumblr 上创建

B - 域。com/blog/ - 在 Wordpress(位于不同的服务器上)上创建并由 nginx 代理在 domain.com

上提供服务

我想将我所有的帖子从 A 重定向到 B,因此我会将我的子域移动到 B 服务器并为旧帖子 url 设置重定向。

我怎样才能在 nginx 中做到这一点?

我在新博客上有一些不同的网址,所以我不能动态地为所有人做这个(我在旧博客上只有 30-35 个网址,所以可以通过手动逐行重定向来完成)。

我觉得下面的设置就可以了

server {
  server_name  blog.domain.com;
  location / {
    return 301 http://domain.com/blog/;
  }
  location /posts/123456/my-first-post {
    return 301 http://domain.com/blog/my-first-post-on-new-blog/;
  }
}

这样做好吗?

非常感谢您在此案例中提供的帮助。

您可以将 URI 列表放入映射中:

map $uri $newuri {
    default                     /blog/;
    /posts/123456/my-first-post /blog/my-first-post-on-new-blog/;
    /posts/another/post         /blog/somewhere-on-new-blog/;
}
server {
    server_name  blog.domain.com;
    return 301 http://domain.com$newuri;
}

详情见this document