Search code examples
nginxreverse-proxy

nginx proxy pass rewrite rule when the first path parameter is not fixed


Need help with below nginx proxy/rewrite rule -

Request: https://domain1.com/{cc}-{lc}/news/article
--needs to proxy forwarded like below --
Target: https://domain2.com/{cc}-{lc}/info/article

Examples-

Request: https://domain1.com/us-en/news/welcome_2020
--needs to proxy forwarded like below --
Target: https://domain2.com/us-en/info/welcome_2020

Request: https://domain1.com/fr-fr/news/welcome_2020
--needs to proxy forwarded like below --
Target: https://domain2.com/fr-fr/info/welcome_2020


Solution

  • Use

    location ... { # your location
        rewrite ^(/[^/]+)/news(/.*) $1/info$2 break;
        ... # all the other proxy setup
        proxy_pass ...
    }