Welcome! Log In Create A New Profile

Advanced

Issue with auto subdomain nd trailing slash

February 22, 2013 06:27AM
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 ^ 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 ^ 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 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: 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 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 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, and the regexp matches, the sub domain is properly calculated.
In the second case (no trailing slash), $host is wrong: it contains XXXX.net instead of 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
Subject Author Posted

Issue with auto subdomain nd trailing slash

shumisha February 22, 2013 06:27AM

Re: Issue with auto subdomain nd trailing slash

Francis Daly February 22, 2013 07:58AM

Re: Issue with auto subdomain nd trailing slash

shumisha February 22, 2013 08:39AM



Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 190
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready