1

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!

2
  • Do not use word boundaries with special chars, try just location ~ .*(/rss/.*) Commented Feb 16, 2022 at 22:22
  • Ops, you are right, didn't notice it… thanks a lot! Commented Feb 16, 2022 at 22:26

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.