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
Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,236469,236469#msg-236469
on 2013-02-22 12:28
on 2013-02-22 13:57
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 ? I guess it will show http://XXXX.net/admin/. You then separately request 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 (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 Daly francis@daoine.org
on 2013-02-22 14:40
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 http://subdomain.xxxx.net/admin to 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; 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: http://forum.nginx.org/read.php?2,236469,236477#msg-236477
Please log in before posting. Registration is free and takes only a minute.
Existing account
(Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
Log in with Google account | Log in with Yahoo account
No account? Register here.