Issue with auto subdomain nd trailing slash

shumisha nginx-forum at
Fri Feb 22 11:27:27 UTC 2013

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

Here is the start of the config:

server {

    listen 80;

    # base domain
    set $root_domain;

    server_name *;

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

    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 ^$host permanent;
     set $root_path /var/www/$root_domain/$sub_domain/public;

    if ($sub_domain = "") {
# debugging instruction: figure out what's the value of $host
rewrite ^$host permanent;
      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;

        # 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
    try_files $uri =404;
    location ~ \.php$ {
        include /etc/nginx/fastcgi_params;
        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 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:, and /admin/ is a folder under
the subD1 web site, everything goes well, the index file (index.php) is
Now if one requests (without trailing slash),
the sub domain is not recognized anymore, and the root path is set to the
path to 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, and the regexp matches, the sub domain is properly
In the second case (no trailing slash), $host is wrong: it contains
instead of, 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:,236469,236469#msg-236469

More information about the nginx mailing list