3

How to trig this rule only when I have GET parameters(query string) in url, otherwise I will match on an alias.

location ~^/static/photos/.* {
    rewrite ^/static/photos/(.*)$  /DynamicPhotoQualitySwitch/photos/$1  break;
    expires     7d;
    proxy_pass http://foofoofoo.com;
    include /etc/nginx/proxy.conf;
     }
5
  • Do you mean GET parameters? e.g. /static/photos/photo1.png?size=small
    – GCon
    Commented Jun 29, 2013 at 2:02
  • Yes, please provide an example.
    – Nicholas
    Commented Jun 29, 2013 at 11:18
  • Christophe, Since you didn't accept the answer I assume that solution did not work for you. Nginx is great so I'm happy to help solve any problem you may have with it, just let me know if you are stuck with anything. Commented Jul 8, 2013 at 22:28
  • @hoonto in fact it solved my problem I just forget to tag your answer as solved thank you Commented Aug 22, 2014 at 10:02
  • Oh good, I'm glad to hear it. Thanks! Commented Aug 22, 2014 at 18:30

1 Answer 1

8
+250

The 1st way that I know of is using a regex against the $args parameter like so:

    if ($args ~ "^(\w+)=") { 

Or the 2nd way is to use the convenient $is_args like so:

    if ($is_args != "") {  

Remember that in both styles you need to put a space between the if and the opening parenthesis; "if (" not "if(" as well as a space after the closing parenthesis and the opening brace; ") {" rather than "){".

Full example using the 1st style above, nginx.conf:

location ~^/static/photos/.* { 
    include /etc/nginx/proxy.conf; 
    if ($args ~ "^(\w+)=") { 
            rewrite ^/static/photos/(.*)$  /DynamicPhotoQualitySwitch/photos/$1  break;
            expires     7d;
            proxy_pass http://foofoofoo.com; 
    }
}

Full example using the 2nd style above, nginx.conf:

location ~^/static/photos/.* { 
    include /etc/nginx/proxy.conf; 
    if ($is_args != "") {  
            rewrite ^/static/photos/(.*)$  /DynamicPhotoQualitySwitch/photos/$1  break;
            expires     7d;
            proxy_pass http://foofoofoo.com; 
    }
}

Note that the proxy.conf include goes outside of the if statement.

Version:

[nginx@hip1 ~]$ nginx -v
nginx version: nginx/1.2.6 

And some info on the $args and $is_args variables:

http://nginx.org/en/docs/http/ngx_http_core_module.html

Reading the docs is always useful, I just discovered that $query_string is the same as $args, so where I have $args above, you could also use $query_string according to the docs.

IMPORTANT

It is important to note however, that If can be Evil!

And therefore either test thoroughly or use the recommendation provided in the link above to change the URL inside location statement in a way similar to the example provided there, something like:

    location ~^/static/photos/.* {
        error_page 418 = @dynamicphotos;
        recursive_error_pages on;

        if ($is_args != "") {
            return 418;
        }

        # Your default, if no query parameters exist:
        ...
    }

    location @dynamicphotos {
        # If query parameters are present:
        rewrite ^/static/photos/(.*)$  /DynamicPhotoQualitySwitch/photos/$1  break;
        expires     7d;
        include /etc/nginx/proxy.conf; 
        proxy_pass http://foofoofoo.com; 
    }

Your Answer

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

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