Системное администрирование →  Nginx, apache2: автоматические субдомены

Поведаю вам как сделать автоматические субдомены для связки nginx-apache.
Имеем структуру папок такую

path/to/website/example.com
		example.com/www //домен по умолчанию, обращение через example.com, www.example.com
		example.com/subomain //subdomain.example.com
		example.com/other-sub //other-sub.example.com

Конфиг NGinx:
server {
    listen   80;
    server_name example.com *.example.com;
    set $subdomain "www";
    if ($host ~* ^([a-z0-9-\.]+)\.example.com$) {
        set $subdomain $1;
    }
    if ($host ~* ^www.example.com$) {
        set $subdomain "www";
    }
    access_log /var/logs/nginxaccess_example.com.log main;
    error_log /var/logs/nginxerror_example.com.log;
    # Статическиое наполнение отдает сам nginx
    # back-end этим заниматься не должен 
    location ~* \.(jpg|jpeg|gif|png|ico|css|bmp|swf|js)$ {
        root path/to/website/example.com/$subdomain;
    }
    # Перенаправление на back-end
    location / {
        root path/to/website/example.com/$subdomain;
        proxy_pass  http://127.0.0.1:8181;
		proxy_set_header X-Real-IP $remote_addr;
		proxy_set_header X-Forwarded-for $remote_addr;
		proxy_set_header Host $host;
		proxy_connect_timeout 120;
		proxy_send_timeout 120;
		proxy_read_timeout 180;
    }
}

Apache
Вариант #1 — Используем vhost_alias
Конфиг:
<VirtualHost *:8181>
	ServerName example.com
	CustomLog /var/logs/apache2_example.com.log combined
	DocumentRoot /path/to/website/example.com/www
</VirtualHost>
<VirtualHost *:8181>
	ServerName www.example.com
	ServerAlias *.example.com
	CustomLog /var/logs/apache2_subdomainexample.com.log combined
	VirtualDocumentRoot /path/to/website/example.com/%1
</VirtualHost>	

Вариант #2 — Используем mod_rewrite:
Конфиг:
<VirtualHost *:8181>
	ServerName example.com
	CustomLog /var/logs/apache2_example.com.log combined
	DocumentRoot /path/to/website/example.com/www
	ServerAlias *.example.com
	RewriteEngine On
	RewriteCond %{HTTP_HOST} !^www.example.com$
	RewriteCond %{HTTP_HOST} ^((.*)\.)example.com$
	RewriteRule ^/(.*) /%2/$1
</VirtualHost>


На этом все. Выражаю благодарность adword'у.

  • Warning: strtotime() [function.strtotime]: It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'Europe/Moscow' for 'MSK/4,0/no DST' instead in /home/pyha/pyhapyha.ru/templates/compiled/%%F3^F3B^F3B39BC9%%topic_list.tpl.php on line 147

    Warning: strtotime() [function.strtotime]: It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'Europe/Moscow' for 'MSK/4,0/no DST' instead in /home/pyha/pyhapyha.ru/templates/compiled/%%F3^F3B^F3B39BC9%%topic_list.tpl.php on line 147
    0

  • Warning: strtotime() [function.strtotime]: It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'Europe/Moscow' for 'MSK/4,0/no DST' instead in /home/pyha/pyhapyha.ru/include/function.php on line 326

    Warning: strtotime() [function.strtotime]: It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'Europe/Moscow' for 'MSK/4,0/no DST' instead in /home/pyha/pyhapyha.ru/include/function.php on line 326

    Warning: date() [function.date]: It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'Europe/Moscow' for 'MSK/4,0/no DST' instead in /home/pyha/pyhapyha.ru/include/function.php on line 329

    Warning: date() [function.date]: It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'Europe/Moscow' for 'MSK/4,0/no DST' instead in /home/pyha/pyhapyha.ru/include/function.php on line 329

    Warning: date() [function.date]: It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'Europe/Moscow' for 'MSK/4,0/no DST' instead in /home/pyha/pyhapyha.ru/include/function.php on line 334

    Warning: date() [function.date]: It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'Europe/Moscow' for 'MSK/4,0/no DST' instead in /home/pyha/pyhapyha.ru/include/function.php on line 334
    28 января 2010, 16:32
  • mario
  • 8