'redirect old domain example.com/subpages to respective newdomain.com/subpages using Nginx

Let's say I have a old domain "example.com" and a new one "newdomain.com"

the old and new domains have the same sub location pages for example example.com/test1/ = newdomain.com/test1/ example.com/test2/ = newdomain.com/test2/ and so on

I need an Nginx rule to do redirects all subpages from the old domain to the new domain respective subpages



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source