Nginx location match regex not working

2019-08-04 19:13发布

问题:

I am unable to match location with below mentioned pattern, I want to set expires header to 24 hrs. but it is not working. It works if I just use below mentioned regex :

location ~* .*abc\.php.* {
expires 24h;
}

Below example does not work.

location ~* .*abc\.php.*xyz=detail.*login_something=.* {
expires 24h;
}

There is lot of content in between and after of "abc.php" & "xyz=detail" & "login_something=" so I have to use .* only.

Thanks in advance!

回答1:

There are multiple ways to achieve what you are trying to do, but the simplest method is to apply your mega regex to a variable that contains the entire URI (including the query string). This would be $request_uri

The second problem is how to manipulate expires and again, rather than use multiple blocks and have to reimplement PHP directives in each one, just use the map directive as detailed in the expires documentation.

For example:

map $request_uri $expires {
    default                                   off;
    ~*abc\.php.*xyz=detail.*login_something=  24h;
}

server {
    ...
    expires $expires;
    ...
}


标签: regex nginx