Welcome! Log In Create A New Profile

Advanced

Redirect of wildcard subdomain to subfolder

Posted by Ancient 
Redirect of wildcard subdomain to subfolder
February 01, 2012 10:21AM
Hello,
copying my post in russian here as well.

I spend a lot of time trying to configure following scenario -

*.domain.com should take files from /srv/www/vhosts/domain.com/*/
also PHP files should be handled by apache with same scenario.

So currently it works super fine on pure apache with following rewrite rule

RewriteEngine On
RewriteCond %{HTTP_HOST} ^([^\.]+)\.domain\.com
RewriteCond /srv/www/vhosts/domain\.com/%1 -d
RewriteRule ^(.*) /%1/$1 [L]

and I wish to install nginx as frontend (it should server static, not just proxy)

I found 2 options
1)
server {
listen 80;
server_name ~^(.*)\.nginxdomain\.com$;
#if directory doesn't exist
if (!-d /home/domains/nginxdomain.com/public/$1) {
rewrite . http://www.nginxdomain.com/ redirect;
}

# Sets the correct root
root /home/domains/nginxdomain.com/public/$1;

it doesnt work, when for example I request picture.jpg Nginx searhes for file under ../domain/jpg/.. (jpg instead of subdomain)
or if picture.png it would got to ../domain/png/..

2)
if ($host !~* ^www\.domain\.tld$) {}
if ($host ~* ^([^.]+)\.domain\.tld$) {
set $auto_subdomain $1;
}
if (-d /var/www/domain.tld/www/$auto_subdomain) {}
if (-f /var/www/domain.tld/www/$auto_subdomain$uri) {
rewrite ^(.*)$ /$auto_subdomain$uri;
break;
}

I dont really understand this, but it doesnt work like that.


I would appreciate any help,

thanks,
Igor S.
Re: Redirect of wildcard subdomain to subfolder
February 02, 2012 11:38AM
if ($host ~* ^([^.]+)\.facebook\.rc\.77test\.co\.uk$) {
set $auto_subdomain $1;
}
root /srv/www/vhosts/facebook.rc.77test.co.uk/$auto_subdomain/;



this seems to be working properly, can someone advice is it fine in terms of performance, if I ll have more than 100 subfolders.

thanks.
ohm
Re: Redirect of wildcard subdomain to subfolder
March 07, 2012 07:11PM
@Ancient - your approach worked for me.
I am also wondering if there is any performance issue with this.
I know explicit location blocks for each subdomain is the best performance, but for practical reasons the above config is very useful.
Any issues with it?
Re: Redirect of wildcard subdomain to subfolder
March 28, 2012 11:47AM
We have been running it on production server for a week now without problems. Server load decreased by ~30%.
Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 94
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 500 on July 15, 2024
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready