# Server\_name capture and $document\_root

**URL:** <https://www.ruby-forum.com/t/server-name-capture-and-document-root/163001>\
**Category:** NGINX\
**Created:** [March 16, 2009, 2:44pm UTC](https://www.ruby-forum.com/t/server-name-capture-and-document-root/163001 "2009-03-16T14:44:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rapsey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/3be4f8/32.png) [@Rapsey](https://www.ruby-forum.com/u/Rapsey)\
**Post date:** [March 16, 2009, 2:44pm UTC](https://www.ruby-forum.com/t/server-name-capture-and-document-root/163001/1 "2009-03-16T14:44:40Z")

</div>

I’m trying to use the new feature from 0.7.40 - captures in server\_name.  
I use $document\_root in SCRIPT\_FILENAME for fastcgi. The problem is that  
the  
captured part of server\_name is not being used. If I check my access  
log, it  
will say this:  
404 … /var/www/htdocs//index.php

For a regular index.php, the path will be correct and the captured part  
will  
be in it:  
200 … /var/www/htdocs/domain/index.html

This is the gist of my config:

server {  
listen 80;  
server\_name ~^(www.)?(.+).(.+)$;  
root /var/www/htdocs/$2;

log\_format main ‘$status “$request” $body\_bytes\_sent $remote\_addr  
$document\_root$fastcgi\_script\_name’;  
…

location ~ .php$ {  
fastcgi\_param SCRIPT\_FILENAME $document\_root$fastcgi\_script\_name;  
… # normal fastcgi params  
}  
}  
}

Is this a bug or an incorrect config on my part?

thank you,  
Sergej

---

<div class="post-metadata">

**Author:** ![Rapsey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/3be4f8/32.png) [@Rapsey](https://www.ruby-forum.com/u/Rapsey)\
**Post date:** [March 16, 2009, 2:52pm UTC](https://www.ruby-forum.com/t/server-name-capture-and-document-root/163001/2 "2009-03-16T14:52:57Z")

</div>

On Mon, Mar 16, 2009 at 02:35:06PM +0100, Rapsey wrote:

> This is the gist of my config:  
> location ~ .php$ {  
> fastcgi\_param SCRIPT\_FILENAME $document\_root$fastcgi\_script\_name;  
> … # normal fastcgi params  
> }  
> }  
> }
> 
> Is this a bug or an incorrect config on my part?

The “root /var/www/htdocs/$2” is evaluated in “location ~ .php$”  
context,  
therefore, the “.php$” regex clears $2. The woraround is:

server {

server\_name ~^(www.)?(.+).(.+)$;  
set $domain $2;  
root /var/www/htdocs/$domain;

BTW, you better to use 0.7.42, as there are bugfixes in captures.

---

<div class="post-metadata">

**Author:** ![Rapsey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/3be4f8/32.png) [@Rapsey](https://www.ruby-forum.com/u/Rapsey)\
**Post date:** [March 16, 2009, 3:01pm UTC](https://www.ruby-forum.com/t/server-name-capture-and-document-root/163001/3 "2009-03-16T15:01:49Z")

</div>

Works perfectly. Thank you.

Sergej

2009/3/16 Igor S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
