Lets stay I have a site at https://www.example.com
I want Nginx to redirect all requests to my site containing /rss/
to a server I have at the address 127.0.0.1:4003
, appending /api/v1
and the whole path that was passed in the original request.
For example, a request to https://www.example.com/en/rss/blog
would go to http://127.0.0.1/api/v1/en/rss/blog
.
I tried with location
, matching the url with regex:
location ~ ^.*\b(\/rss\/)\b.*$ {
rewrite ^.*\b(\/rss\/)\b.*$ /api/v1$1 break;
proxy_pass http://127.0.0.1:3007;
}
The redirection works, but is not appending the path, as I receive Cannot GET /api/v1/rss/
as response.
This is a problem with the regex I suppose, it should capture the whole path instead of /rss/
only; but how?
Any idea will be welcome!
CodePudding user response:
Word boundaries constructs are context dependant, in case of \b\/rss\/\b
, the pattern will only match when the /
s are preceded/followed with word chars, i.e. letters, digits or _
.
In your case, the regex should look like
location ~ .*(/rss/.*)
location ~ (/rss/.*)
I.e.
.*
- match any text but line break chars(/rss/.*)
- Group 1 ($1
):/rss
string and then any text but line break chars