位置从域重定向到 nginx 中的另一个域

def*_*750 1 nginx

我想将地点重定向 play.example.com/u/<user id>example.com/u/<user id>play.example.com/b/<game id>重定向至play.example2.net/b/<game id>

同时也有网站选项(我在同一域上托管的 webroot 和 api)。我怎样才能做到这一点?

Ale*_*dev 5

如果我正确理解你的问题,这应该有效:

server {
  server_name play.example.com;

...

  location ~ ^/u/(.*)$ {
    return 301 $scheme://example.com/u/$1;
  }

  location ~ ^/b/(.*)$ {
    return 301 $scheme://play.example2.net/b/$1;
  }
}
Run Code Online (Sandbox Code Playgroud)

  • 最好在正则表达式中至少使用起始锚点“^”:“location ~ ^/u/(.*) {”。当前位置块匹配`.*/u/`,这是太宽的匹配。 (3认同)