I am trying to join a variable and a string within the map directive in Nginx, but no luck so far. The idea is that any requests to /press/some-variable-text should get redirected to /magazine/some-variable-text

map $uri $new_uri {
    ~^/press/(?<press_id>.+)$ /magazine/$press_id;

The output of this is be the following string "/magazine/$press_id" (it doesn't interpolate the variable). When I remove the prefix /magazine/ then the variable content is rendered normally.

Is there a way to achieve this?


I assume you are using nginx 1.10 or earlier. The variable interpolation you're trying doesn't work for me on 1.10, and in the change log I found the following entry related to the map directive:

Changes with nginx 1.11.0

  • Feature: the "map" directive supports combinations of multiple variables as resulting values.
  • Hey RazerM have you actually tried this variable interpolation within a newer Nginx version >= 1.11? – Ander Apr 14 at 9:38
  • Yes, it works on 1.15 – RazerM Apr 14 at 13:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.