# Rewriting $request\_filename

**URL:** <https://www.ruby-forum.com/t/rewriting-request-filename/160711>\
**Category:** NGINX\
**Created:** [February 19, 2009, 1:56am UTC](https://www.ruby-forum.com/t/rewriting-request-filename/160711 "2009-02-19T01:56:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![WSandrew\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d07c76/32.png) [@WSandrew\_L](https://www.ruby-forum.com/u/WSandrew_L)\
**Post date:** [February 19, 2009, 1:56am UTC](https://www.ruby-forum.com/t/rewriting-request-filename/160711/1 "2009-02-19T01:56:40Z")

</div>

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:

> <https://gist.github.com/loe/66637>

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.

---

<div class="post-metadata">

**Author:** ![WSandrew\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d07c76/32.png) [@WSandrew\_L](https://www.ruby-forum.com/u/WSandrew_L)\
**Post date:** [February 19, 2009, 4:58pm UTC](https://www.ruby-forum.com/t/rewriting-request-filename/160711/2 "2009-02-19T16:58:01Z")

</div>

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](http://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.
