Stack Overflow

How do I reuse a regex capture after an nginx map lookup?

View full config
Asked today by edge_cache_404 | Active just now | Viewed 1k times

I have a tiny nginx preview endpoint in front of an internal admin page. The preview should print the capture from the URL and also run a small map check from a request header.

It works with simple input, but the value around the map feels like it changes shape. Is this config safe to deploy?

map $http_x_map $map_guard {
    default "miss";
    ~^([A-Za-z0-9._~=-]+)$ "mapped";
}

location ~ ^/preview/([A-Za-z0-9._~-]+)$ {
    add_header X-Preview "$1|$map_guard|$1";
    return 200 "$1|$map_guard|$1";
}
nginx regex map captures

0 Answers

Know someone who can explain why the second capture wins?