这是做子域的好做法

我试图用我的web应用程序创build子域,但是,我没有在nginx的经验,我一直在试图从SFfind一个稳定的解决scheme,但不幸的是我找不到任何好的解决scheme。

我试图做的问题是创build灵活的子域,例如,如果我有像dev.example.com这样的任何子域,它应该沿着/var/www/example.com/www/dev文件目录,和任何types的子域名(WWW除外)将试图find一个目录,如果存在,将其作为根目录。

 /var/www/example.com/www/{subdomain} 

是当前目录查找,如果它不存在,默认的根将是:

 /var/www/example.com/www/ 

这是我的域sites-enabledsites-enabledconfiguration文件。

 server { server_name example.com www.example.com; root /var/www/example.com/www; index index.php index.htm index.html; error_page 404 /404.html; error_page 500 502 503 504 /50x.html; access_log /var/www/example.com/logs/access.log; error_log /var/www/example.com/logs/errors.log; error_page 404 /index.php; location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /var/www/example.com/www$fastcgi_script_name; include fastcgi_params; } location /pma { auth_basic "Website development"; auth_basic_user_file /var/www/example.com/www/dev/authfile; } location /dev { auth_basic "Website development"; auth_basic_user_file /var/www/example.com/www/dev/authfile; } location ~ /\.ht { deny all; } } server { server_name pma.example.com; index index.php; root /var/www/example.com/www/pma; access_log /var/www/example.com/logs/access.log; error_log /var/www/example.com/logs/errors.log; location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /var/www/example.com/www$fastcgi_script_name; include fastcgi_params; } location / { auth_basic "Website development"; auth_basic_user_file /var/www/example.com/www/dev/authfile; } } server { server_name dev.example.com; index index.php; root /var/www/example.com/www/dev; access_log /var/www/example.com/logs/access.log; error_log /var/www/example.com/logs/errors.log; location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /var/www/example.com/www$fastcgi_script_name; include fastcgi_params; } location / { auth_basic "Website development"; auth_basic_user_file /var/www/example.com/www/dev/authfile; if ($request_uri ~* ^(/home(/index)?|/index(.php)?)/?$) { rewrite ^(.*)$ / permanent; } if ($host ~* ^www\.(.*)) { set $host_without_www $1; rewrite ^/(.*)$ $scheme://$host_without_www/$1 permanent; } if ($request_uri ~* index/?$) { rewrite ^/(.*)/index/?$ /$1 permanent; } if (!-d $request_filename) { rewrite ^/(.+)/$ /$1 permanent; } if ($request_uri ~* ^/system) { rewrite ^/(.*)$ /index.php?/$1 last; break; } if (!-e $request_filename) { rewrite ^/(.*)$ /index.php?/$1 last; break; } } location ~ /\.ht { deny all; } } 

编辑:更新conf文件:

 server { #regex capture assigning the subdomain to $subdomain server_name ~^(?<subdomain>.+)\.example\.com$; if ($host ~* ^www\.(.*)) { set $remove_www $1; rewrite ^(.*)$ http://$remove_www$1 permanent; } #if the directory doesn't exist, redirect to the main site if (!-d /var/www/example.com/www/$subdomain) { rewrite . example.com redirect; } #if we have made it here, set the root to the above directory root /var/www/example.com/www/$subdomain; #the rest of your config index index.php; access_log /var/www/example.com/logs/access.log; error_log /var/www/example.com/logs/errors.log; location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /var/www/example.com/$subdomain$fastcgi_script_name; include fastcgi_params; } # this needs to be enabled for dev.example.com and pma.example.com only location / { auth_basic "Authentication Required"; auth_basic_user_file /var/www/example.com/$subdomain/authfile; } location ~ /\.ht{ deny all; } } 

如果您正在寻找基于标准模板(例如,每个用户的子域)的自动子域,则可以在server_name指令中使用正则expression式捕获。 这种方法将允许您将server_name的一部分分配给一个variables,以便在您的configuration中使用(例如设置path)。

通常情况下,将“真正的”子域放在网站根目录之上是为了更好地分离网站(它具有阻止通过主站点访问的优势),并防止模糊不清目录是否映射到子域名或不是。 例如,考虑“dev”子域根目录的以下path:/var/www/example.com/subdomains/dev/www。 这也可以让你为你的开发站点维护单独的日志,例如/var/www/example.com/subdomains/dev/logs)。

下面的示例使用您的pma子域作为模板,并将子域根保留在主站点下。

 server{ #regex capture assigning the subdomain to $subdomain server_name ~^(?<subdomain>.+)\.example\.com$; #if the directory doesn't exist, redirect to the main site if (!-d /var/www/example.com/www/$subdomain) { rewrite . example.com redirect; } #if we have made it here, set the root to the above directory root /var/www/example.com/www/$subdomain; #the rest of your config index index.php; access_log /var/www/example.com/logs/access.log; error_log /var/www/example.com/logs/errors.log; location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /var/www/domain.com/$subdomain$fastcgi_script_name; include fastcgi_params; } location / { auth_basic "Authentication Required"; auth_basic_user_file /var/www/example.com/$subdomain/authfile; } location ~ /\.ht{ deny all; } } 

上述想法只有在所有子域都遵循相同模板的情况下才有效。 在你发布的configuration中,pma子域名和dev子域名是完全不同的(因为dev子域有很多重写,而pma没有)。 任何不遵循所使用的“模板”的子域将需要自己的服务器块和configuration。 值得一提的是,在两个服务器块可用的情况下(例如,一个服务器名称为静态server_name,另一个服务器名称为regex server_name),静态server_name将优先。