Score:0

How to Avoide Passing Regrex Part into proxy_pass in Nginx

yt flag

I want to foward all request to https://myfrontend/(anything)/front/ to my backend server http://mybackend:8081 and now I setup my nginx like below:

location ~ ^/(.*)/front/ {
  proxy_pass http://backend:8081;
}

However, this will pass the (.*) part to the end of the backend server like:

If I try to access: https://myfrontend/123/front/, it will be forward to http://backend:8081/123/front while what I want is to forward it to http://backend:8081 only

I've tried with rewrite and it works, but it would be a redirection while I want to keep the "123" in the URL in the browser address tab.

I've also tried the bellow configuration:

location ~ ^/(.*)/front/ {
  proxy_pass http://backend:8081/;  #with the trailing / at the end
}

But it's not allowed to do so if regrex is used in the lcoation. I've tried the following configuration with static location and it works well:

location  /123/front/ {
  proxy_pass http://backend:8081/;
}

Do you have any good idea?

Thanks

Romeo Ninov avatar
in flag
Do you mean **regex**? (not regrex )
Richard Smith avatar
jp flag
Try: `rewrite ^ / break;` inside the same `location`.
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.