Forum: NGINX Rewriting $request_filename

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
W. andrew L. (Guest)
on 2009-02-19 02:56
(Received via mailing list)
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
Nick P. (Guest)
on 2009-02-19 17:58
(Received via mailing list)
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
some advantages, the two biggest of which are the ability to see the
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
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" (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
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
$my_var variable.

If you need to output multiple cookies, you can just use multiple
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.
This topic is locked and can not be replied to.