Skip Location Based On Query String Parameter?

We’d like to bypass a location if a query string varaible is present. Is
this an approriate place to use “if”, and if so, how should it be done?
If
the URL is a forward slash, followed by anything, followed by a forward
slash, it attempts to find a static html.gz file in the cache folder
matching the url, and then passes it to apache. This is the desired
behavior, except when the URL contains ?nocache=true. If the nocache
parameter is present, it should skip the cache check and go straight to
the
proxy pass.

This is the config we’re using now:

location ~ ^/([a-zA-Z0-9-]+)/ { #Use cache if possible, then proxy
pass
try_files /cache/$1.html.gz /cache/$1.html @apache;
}

location / {
include /etc/nginx/apache-pass;
}

Pass the PHP script to AquaCart

location @apache {
include /etc/nginx/apache-pass;
}

What’s the correct way to bypass the ^/([a-zA-Z0-9-]+)/ location when a
specific query string parameter is set?

Posted at Nginx Forum:

On Thu, Nov 13, 2014 at 06:20:05AM -0500, nrahl wrote:

Hi there,

If the nocache
parameter is present, it should skip the cache check and go straight to the
proxy pass.

Untested, but I think that if I needed this, I would probably add an
“if” inside the location{}.

It would need testing, and it does depend on what is in the
“apache-pass”
file, but presuming that it does do “proxy_pass” and does not do
anything
that is invalid in an “if in location”, then

location ~ ^/([a-zA-Z0-9-]+)/ { #Use cache if possible, then proxy pass

   if ($arg_nocache = true) {
     include /etc/nginx/apache-pass;
   }
  try_files /cache/$1.html.gz /cache/$1.html @apache;

}

could possibly work.

(At least until the config is changed in the future to add more if()s
in that location. So don’t do that.)

f

Francis D. [email protected]

     include /etc/nginx/apache-pass;
   }
  try_files /cache/$1.html.gz /cache/$1.html @apache;

}

could possibly work.

When trying to use if(){include} I get the error, “‘include’ directive
is
not allowed here”, I guess include itself is one of those things not
allowed
in an if statement? I found another post on the board that says “if”
isn’t
allowed in include because they are both block directives and no one
wants
to fix it because “if” is a hack anyway.

Maybe it can be done by using a nested location blocks, something like:

location (!$arg_nc) { # the qs parameter “ns” does not exist
location ~ ^/([a-zA-Z0-9-]+)/ {
try_files /cache/$1.html @apache;
}
}

Posted at Nginx Forum:

I found soemthing that I think works, but it feels very hackish:

location ~ ^/([a-zA-Z0-9-]+)/ {
error_page 418 = @apache; #proxy pass
recursive_error_pages on;
if ($arg_nc = 1) {
return 418;
}
try_files /cache/$1.html @apache;
}

Is this the best (only?) way to bypass a location based on query sting?

Posted at Nginx Forum:

On Sun, Jan 04, 2015 at 09:12:40AM -0500, nrahl wrote:

Hi there,

could possibly work.

When trying to use if(){include} I get the error, “‘include’ directive is
not allowed here”,

You are correct; I hadn’t tested it – I had read “Context: any” on
Core functionality and had incorrectly assumed.

But if I just use the “proxy_pass” directive instead of the “include”,
then it seems to work for me (going to the upstream without attempting
/cache/ files first).

Maybe it can be done by using a nested location blocks, something like:

Query string does not take part in location matches, so this won’t work.

f

Francis D. [email protected]