# Issue with auto subdomain nd trailing slash

**URL:** <https://www.ruby-forum.com/t/issue-with-auto-subdomain-nd-trailing-slash/226828>\
**Category:** NGINX\
**Created:** [February 22, 2013, 12:28pm UTC](https://www.ruby-forum.com/t/issue-with-auto-subdomain-nd-trailing-slash/226828 "2013-02-22T12:28:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![shumisha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a6a055/32.png) [@shumisha](https://www.ruby-forum.com/u/shumisha)\
**Post date:** [February 22, 2013, 12:28pm UTC](https://www.ruby-forum.com/t/issue-with-auto-subdomain-nd-trailing-slash/226828/1 "2013-02-22T12:28:07Z")

</div>

Hi All,

I am running into a weird issue, after having configured nginx 0.7.67  
(comes  
with debian 6) for serving PHP pages, with automatic subdomains.  
It works fine for the most part, but when requesting a url that  
correspond  
to an actual folder, but without a trailing slash, the subdomain  
recognition  
fails.

Here is the start of the config:

server {

```
listen 80;

# base domain
set $root_domain XXXX.net;

server_name XXXX.net *.XXXX.net;

if ($host ~* ^www\.XXXX\.net$) {
  rewrite ^(.*) $scheme://XXXX.net$1 permanent;
  break;
}

set $sub_domain "";
if ($host ~* ^(.*)\.XXXX\.net$) {
  set $sub_domain $1;
}

if ($sub_domain) {

```

# debugging instruction: figure out what’s the value of $host

rewrite ^ [Google](http://google.com?q=$host) permanent;  
break;  
set $root\_path /var/www/$root\_domain/$sub\_domain/public;  
}

```
if ($sub_domain = "") {

```

# debugging instruction: figure out what’s the value of $host

rewrite ^ [Google](http://google.com?q=$host) permanent;  
break;  
set $root\_path /var/www/$root\_domain/www/public;  
}

#if the directory doesn’t exist, prevent access  
if (!-d $root\_path) {  
return 403;  
}

```
#if we have made it here, set the root to the computed directory
root $root_path;

# logging
access_log /var/log/nginx/$sub_domain.$root_domain.access.log;
error_log /var/log/nginx/$sub_domain.$root_domain.error.log;

location / {
    index index.html index.php;

    # serve static files that exist without running other rewrite 

```

tests  
if (-f $request\_filename) {  
expires 30d;  
break;  
}

```
    # send all non-existing file or directory requests to index.php
    if (!-e $request_filename) {
        rewrite ^/(.*) /index.php last;
    }
}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
try_files $uri =404;
location ~ \.php$ {
    include /etc/nginx/fastcgi_params;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $root_path$fastcgi_script_name;
}

# optional authentication
# would require to be put in its own server block
# if only valid for some sub-domais
#location / {
# auth_basic "Authentication Required";
# auth_basic_user_file $root_path/private/authfile;
#}

location ~ /\.ht{
    deny all;
}

```

}

With this setup, you can access [XXXX.net](http://XXXX.net) and any subdomain is served  
based  
on the corresponding folder under web root. All of that works very fine  
except in one case:

If you request: [xxxx.net&nbsp;-&nbsp;This website is for sale!&nbsp;-&nbsp;xxxx Resources and Information.](http://subD1.XXXX.net/admin/), and /admin/ is a folder  
under  
the subD1 web site, everything goes well, the index file (index.php) is  
served.  
Now if one requests [xxxx.net&nbsp;-&nbsp;This website is for sale!&nbsp;-&nbsp;xxxx Resources and Information.](http://subD1.XXXX.net/admin) (without trailing  
slash),  
the sub domain is not recognized anymore, and the root path is set to  
the  
path to [XXXX.net](http://XXXX.net) instead of that associated with subD1.

Now the real weird thing is that it is the $host variable that’s wrong.  
In  
the first case (request for a folder with trailing slash), $host  
contains  
[subD1.XXXX.net](http://subD1.XXXX.net), and the regexp matches, the sub domain is properly  
calculated.  
In the second case (no trailing slash), $host is wrong: it contains  
[XXXX.net](http://XXXX.net)  
instead of [subD1.XXXX.net](http://subD1.XXXX.net), and so the regexp doesn’t match and the sub  
domain is not recognized.

Please note that my question is not about trying to redirect urls with  
or  
without trailing slash to its counterpoint. My issue is with the  
subdomain  
regexp not matching because the $host value is wrong.

Any pointer as to what’s going on? Am I doing something wrong?

Thanks for any pointer and best regards

Posted at Nginx Forum:

> **[Issue with auto subdomain nd trailing slash](https://forum.nginx.org/read.php?2,236469,236469#msg-236469)**
>
> Hi All, I am running into a weird issue, after having configured nginx 0.7.67 (comes with debian 6) for serving PHP pages, with automatic subdomains. It works fine for the most part, but when requesting a url that correspond to an actual folder, but...

---

<div class="post-metadata">

**Author:** ![shumisha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a6a055/32.png) [@shumisha](https://www.ruby-forum.com/u/shumisha)\
**Post date:** [February 22, 2013, 1:57pm UTC](https://www.ruby-forum.com/t/issue-with-auto-subdomain-nd-trailing-slash/226828/2 "2013-02-22T13:57:31Z")

</div>

On Fri, Feb 22, 2013 at 06:27:27AM -0500, shumisha wrote:

> I am running into a weird issue, after having configured nginx 0.7.67 (comes  
> with debian 6) for serving PHP pages, with automatic subdomains.  
> It works fine for the most part, but when requesting a url that correspond  
> to an actual folder, but without a trailing slash, the subdomain recognition  
> fails.

What is probably happening is that when you ask for /dir, you get a http  
redirect to /dir/. That redirect must include the full url, including  
the hostname. And nginx has to choose which hostname to use. And it  
doesn’t choose the one that you want it to choose.

What is the output of

curl -i [http://subD1.XXXX.net/admin](http://subD1.XXXX.net/admin)

? I guess it will show [http://XXXX.net/admin/](http://XXXX.net/admin/). You then separately  
request [http://XXXX.net/admin/](http://XXXX.net/admin/), and therefore shouldn’t be surprised  
that the subdomain recognition fails, because in this request there is  
no subdomain.

Possibly setting server\_name\_in\_redirect  
([Module ngx\_http\_core\_module](http://nginx.org/r/server_name_in_redirect)) to “off” will work for  
you? The docs say that default is off, but I think that may have been  
different in the 0.7 series.

## f

Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![shumisha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a6a055/32.png) [@shumisha](https://www.ruby-forum.com/u/shumisha)\
**Post date:** [February 22, 2013, 2:40pm UTC](https://www.ruby-forum.com/t/issue-with-auto-subdomain-nd-trailing-slash/226828/3 "2013-02-22T14:40:10Z")

</div>

Hi Francis,

Thanks a lot for your message, I think I got things under control thanks  
to  
you!

You’re right, there’s indeed a 301 thrown by nginx from  
[xxxx.net&nbsp;-&nbsp;This website is for sale!&nbsp;-&nbsp;xxxx Resources and Information.](http://subdomain.xxxx.net/admin) to [xxxx.net&nbsp;-&nbsp;This website is for sale!&nbsp;-&nbsp;xxxx Resources and Information.](http://xxxx.net/admin/)  
(as shown by the curl output).

I had tried server\_name\_in\_redirect to off before, to no avail, but I  
tried  
again, just in case. I looked up the documentation again, and I  
realized:

- it’s probably not active on my version, as nginx still uses the first  
server in the server\_name directive regardless of the directive value.  
However, it’s a valid config value
- but that got me thinking and I simply switched the orders of server,  
so  
that it reads:

server\_name \*.xxxx.net [xxxx.net](http://xxxx.net);

So now the automatic redirect still happens (I think it should be  
configurabe, maybe it is) but by placing the \*.xxxx.net server first in  
the  
server\_name directive, the sub domain is preserved and the redirect now  
happens with the correct host.

Thanks again for your help 🙂

Rgds

Posted at Nginx Forum:

> **[Re: Issue with auto subdomain nd trailing slash](https://forum.nginx.org/read.php?2,236469,236477#msg-236477)**
>
> Hi All, I am running into a weird issue, after having configured nginx 0.7.67 (comes with debian 6) for serving PHP pages, with automatic subdomains. It works fine for the most part, but when requesting a url that correspond to an actual folder, but...
