Rewriting $request_filename

I have two rails applications running on one domain. I need to modify
the $request_filename under the location /signup so static assets for
my signup app are served by nginx, and not mongrel (which does
currently work). I’ve tried doing it a few different ways but it
doesn’t seem to work.

My full config:

I first tried using a regular expression on the location:

location ~* /signup(.*) {
set $modified_request_filename $1;
}

That wasn’t working as I expected so I tried using an if block:

if ($request_filename ~* ^/signup(.*)) {
set $modified_request_filename $1;
}

In both cases $modified_request_filename appears to be blank. (only
checked by proxy_passing to
http://airstrip$modified_request_filename). How can I get some
debugging output of this variable’s value, and how can I test for the
existence of a file, minus the /signup prefix which will always be
there.

One way would be to modify the log format to include your variable.

However, I sometimes use a different method that’s a bit of a hack but
has
some advantages, the two biggest of which are the ability to see the
output
on the client and that it’s easier to rip out the config when you’re
finished. This second point is especially true if you don’t already use
a
custom log format.

You can add a cookie to the response to show the value of a variable.

add_header Cookie “mod_req=$modified_request_filename”;

And then can test that using “curl -i domain.com” (or -I if all you care
about are the headers) or with Firefox and one of its cookie/header
inspection plugins.

Note that you can add multiple cookies by calling “add_header Cookie”
multiple times (with different cookie names, of course), but it appears
that
nginx only adds the cookie to the response once the request is finished
being handled, so only the final value of a variable can be passed as a
cookie value.

For example, the following:

set $my_var “a”;
add_header Cookie “var_1=$my_var”;
set $my_var “${my_var}b”;
add_header Cookie “var_2=$my_var”;

will return two cookies with the response:

Cookie: var_1=ab
Cookie: var_2=ab

I had expected the first cookie to be “var_1=a” and the second to be
“var_2=ab”, but both have a value of “ab”, which is the final value of
the
$my_var variable.

If you need to output multiple cookies, you can just use multiple
variables
that don’t change after being set initially.

Again, it’s a bit of a hack, but as it’s only temporary, I don’t have a
problem with it.