# Variable $1 is Empty

**URL:** <https://www.ruby-forum.com/t/variable-1-is-empty/197438>\
**Category:** NGINX\
**Created:** [October 14, 2010, 1:03pm UTC](https://www.ruby-forum.com/t/variable-1-is-empty/197438 "2010-10-14T13:03:33Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dobai-Pataky\_BSSSSl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/77aa72/32.png) [@Dobai-Pataky\_BSSSSl](https://www.ruby-forum.com/u/Dobai-Pataky_BSSSSl)\
**Post date:** [October 14, 2010, 1:03pm UTC](https://www.ruby-forum.com/t/variable-1-is-empty/197438/1 "2010-10-14T13:03:33Z")

</div>

Hi everyone,

This is my first time installing nginx. I install it on my Debian  
5.0.3 VPS and use Debian’s repository to install it (version: 0.6.32).

I’m having problem with using regular expression at server\_name.  
Here’s my configuration:

server {  
listen 80;  
server\_name ~^(.+).mydomain.com$;  
location / {  
root /home/$1/public\_html;  
index index.html index.htm;  
}  
}

When I hit [myusername.mydomain.com](http://myusername.mydomain.com), where  
/home/myusername/public\_html/ is exist. It gives me a 404 error. And  
when I view the log file, seems like $1 is null or something.

2010/10/14 10:47:08 [error] 4924#0: \*3 “/home//public\_html/index.html”  
is not found (2: No such file or directory), client: 173.245.73.113,  
server: ~^(.+).mydomain.com$, request: “GET / HTTP/1.1”, host:  
“[myusername.mydomain.com](http://myusername.mydomain.com)”

Am I doing wrong?  
Any helps will be appreciated, thanks in advance.

Indra

---

<div class="post-metadata">

**Author:** ![Indra\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/a6a055/32.png) [@Indra\_S](https://www.ruby-forum.com/u/Indra_S)\
**Post date:** [October 14, 2010, 1:58pm UTC](https://www.ruby-forum.com/t/variable-1-is-empty/197438/2 "2010-10-14T13:58:56Z")

</div>

I think you should change

server {  
listen 80;  
server\_name ~^(.+).mydomain.com$;  
location / {  
root /home/$1/public\_html;  
index index.html index.htm;  
}  
}

to

server {  
listen 80;  
server\_name ~^(.+).mydomain.com$;  
set $name $1;  
location / {  
root /home/$name/public\_html;  
index index.html index.htm;  
}  
}

2010/10/14 Indra S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

---

<div class="post-metadata">

**Author:** ![Indra\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/a6a055/32.png) [@Indra\_S](https://www.ruby-forum.com/u/Indra_S)\
**Post date:** [October 14, 2010, 2:52pm UTC](https://www.ruby-forum.com/t/variable-1-is-empty/197438/3 "2010-10-14T14:52:14Z")

</div>

Hello!

On Thu, Oct 14, 2010 at 05:56:34PM +0700, Indra S. wrote:

> server\_name ~^(.+).mydomain.com$;  
> 2010/10/14 10:47:08 [error] 4924#0: \*3 “/home//public\_html/index.html”  
> is not found (2: No such file or directory), client: 173.245.73.113,  
> server: ~^(.+).mydomain.com$, request: “GET / HTTP/1.1”, host:  
> “[myusername.mydomain.com](http://myusername.mydomain.com)”
> 
> Am I doing wrong?  
> Any helps will be appreciated, thanks in advance.

Changes with nginx 0.7.44:

…  
\*) Bugfix: if there is the single server for given address:port  
pair,  
then captures in regular expressions in a “server\_name” directive  
did not work.  
…

Adding another server listening on the same socket should resolve  
the issue, though I would recommend to upgrade. 0.6.\* branch is  
legacy and not supported, and you are using an old version from  
this branch.

Additionally, it’s _really bad_ idea to use enumerated captures in  
server\_name. Things may be screwed up easily - it’s enough to add  
another regexp to config (e.g. regexp location) to break anything.

There are two possible safe aproaches:

1. Use named captures as available in nginx 0.8.25+.

See [Server names](http://nginx.org/en/docs/http/server_names.html) for details.

1. For old versions - use server-level if, i.e.

Maxim D.

---

<div class="post-metadata">

**Author:** ![Indra\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/a6a055/32.png) [@Indra\_S](https://www.ruby-forum.com/u/Indra_S)\
**Post date:** [October 14, 2010, 2:59pm UTC](https://www.ruby-forum.com/t/variable-1-is-empty/197438/4 "2010-10-14T14:59:10Z")

</div>

Hello!

On Thu, Oct 14, 2010 at 04:51:05PM +0400, Maxim D. wrote:

[…]

> Adding another server listening on the same socket should resolve  
> the issue, though I would recommend to upgrade. 0.6.\* branch is  
> legacy and not supported, and you are using an old version from  
> this branch.

Ah, as António pointed out - 0.6.\* don’t have server\_name  
captures at all, so the only options is upgrade or use “if”  
matching.

Maxim D.

---

<div class="post-metadata">

**Author:** ![Indra\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/a6a055/32.png) [@Indra\_S](https://www.ruby-forum.com/u/Indra_S)\
**Post date:** [October 15, 2010, 10:59pm UTC](https://www.ruby-forum.com/t/variable-1-is-empty/197438/5 "2010-10-15T22:59:08Z")

</div>

On 14 Out 2010 11h56 WEST, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> listen 80;  
> server\_name ~^(.+).mydomain.com$;  
> location / {  
> root /home/$1/public\_html;  
> index index.html index.htm;  
> }  
> }

According to [Server names](http://nginx.org/en/docs/http/server_names.html) you need  
version \>= 0.7.40 in order to use regex with capturing groups in  
server\_name. Apart from that your config seems correct.

— appa

---

<div class="post-metadata">

**Author:** ![Indra\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/a6a055/32.png) [@Indra\_S](https://www.ruby-forum.com/u/Indra_S)\
**Post date:** [October 14, 2010, 6:16pm UTC](https://www.ruby-forum.com/t/variable-1-is-empty/197438/6 "2010-10-14T18:16:53Z")

</div>

On Thu, Oct 14, 2010 at 7:58 PM, Maxim D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Ah, as Antnio pointed out - 0.6.\* don’t have server\_name  
> captures at all, so the only options is upgrade or use “if”  
> matching.
> 
> Maxim D.

Antnio, Maxim,  
Thanks for your reply, if that’s the problem, I will install the  
latest version of Nginx.
