nginx rule for wildcard url

Posted February 16, 2016 35.9k views

Hello, I need to set some headers to all urls with certain scheme ie:

so i can read it something like this*/*/

i have an if (I know if is evil) like this one:

if ($uri ~ "/php-fpm-status/") {
        set $bypass "true";
        add_header Something "value";

but in this case i need something like this:

if ($uri ~ "/*/*/") {
        set $bypass "true";
        add_header Something "value";

I really suck on regular expressions, so any help would be nice.
or maybe a regular expressions dummy to regular tool/convertor link :D

edited by asb

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Submit an Answer
3 answers


As you said, if is indeed evil :) You should use a location block instead:

location ~ ^/(.*?)/(.*?)/?$ {
    add_header name value;

This regular expression should match all URIs of the format /something/something with an optional trailing slash. Inside the block, you have the variables $1 and $2 available which are mapped to the first and second wildcard group (.*) respectively.

In case you’re interested, I personally like to use Regex101 to test regular expressions.