Score:2

Rewrite cname host mapping url paths with nginx

bv flag

There is currently an app that is using cname host mapping with a third party app

the hostname for the third party app is mycompany.partner.com and the current cname host map under my domain help.mycompany.com

so current routing is below

users => cloudflare DNS(help.mycompany.com) => cname host mapping(mycompany.partner.com) => partner app

now i want to do this

                                                             => cname host mapping(mycompany.partner.com) => partner app
                                                            |
users => cloudflare DNS(help.mycompany.com) => my nginx =>  |
                                                            |
                                                             => my frontend app

is this possible?

So basically i want all traffic to come to my own app via nginx now and then i route some traffic based on url path to the third party app and others to my frontend app

how can i achieve this with nginx? below are the url paths i want to route

this routes to my frontend app

help.mycompany.com/app/test1                  => http://localhost:500/app/test1  
help.mycompany.com/app/test2/test3            => http://localhost:500/app/test2/test3
help.mycompany.com/app/parameter?key=check    => http://localhost:500/app/parameter?key=check

this rewrites/routes to partner app

help.mycompany.com/app/partner1               => https://mycompany.partner.com/app/partner1 
help.mycompany.com/app/discuss/check          => https://mycompany.partner.com/app/discuss/check

and all other paths 

location block to route all url paths to the frontend app is below

location ^~ / {

    rewrite ^/(.*)$ /$1 break;
    proxy_pass http://localhost:500;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $host;
    proxy_redirect off;
}

but now i need to split some url paths to the partner app as stated above

so basically all traffic will pass through nginx as i will point DNS for help.mycompany.com to now point to my nginx reverse proxy and then routes and rewrites the url

Thanks

drookie avatar
za flag
This is a basic question about nginx proxying/rewriting discussed a gazillion times, did you at least try to search for an answer ?
uberrebu avatar
bv flag
this is not a basic question
us flag
Sorry to disagree, this is standard reverse proxy setup for nginx, and there are many resources online available to guide you through it. http://nginx.org/en/docs/http/ngx_http_core_module.html#location gives you information how `nginx` processes locations, that should help you implementing what you want.
uberrebu avatar
bv flag
can you please help with an answer? will really appreciate it...if it is basic then would be happy to get a reply...like i mentioned here the nginx will route all traffic now including the thirdparty app, which is the part am not sure about..so please help with an answer if you can..thanks
uberrebu avatar
bv flag
@TeroKilkanen did you get that i want to rewrite the thirdparty app urls so i am not just redirecting to them, i want to rewrite the urls. That is the part am not sure about, how does that even work? for example before the app controls all urls within itself with the cname host mapping but now i want to control all links from my nginx because i need to re-route some paths to another internal frontend app...is this not enough to help with an answer if it is simple?
Marcel avatar
gb flag
This is not just a reverse proxy, this is akin to Apache's ProxyPassReverse please take a look at their solution https://www.nginx.com/resources/wiki/start/topics/examples/likeapache/
Marcel avatar
gb flag
You will have problems with the third party website cookies if they use CORS and XSS headers you won't be able to server their content through your domain. For that you have to do SSL termination on your domain and proxy the traffic to their domain, much like a transparent forwarding proxy. With Nginx you achieve that with the `stream` directive.
uberrebu avatar
bv flag
@Marcel that is what i have been trying to explain but people keep saying this is basic stuffs, you mind helping with an answer to point in right direction?
Marcel avatar
gb flag
I did it already, your answer is in the nginx link with the documentation pointing to the proxypassreverse. I also said you will have several third party cookies issues. In order to achieve what you want you will have to disable secure HTTP headers like XSS and CORS, which is not really recommended.
uberrebu avatar
bv flag
i think i just need proxy_pass, so it wont be rewriting ursl but just proxy_pass to the third party, i posted here to get an answer so not sure how an answer is on an external url, no single answer to this question below
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.