Location redirect from domain to another one in nginx

I want to redirect locations play.example.com/u/<user id> to example.com/u/<user id> and play.example.com/b/<game id> to play.example2.net/b/<game id>

While having options for website too (webroot and api which i host on same domain). How can I achieve that?


If i understood your question properly, this should work:

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;
  }
}