这是制作子域的好习惯吗

Lew*_*wis 5 nginx subdomain

我正在尝试使用我的 Web 应用程序创建子域,但是,我在 nginx 方面没有太多经验,我一直在尝试从 SF 中找到稳定的解决方案,但不幸的是我找不到任何好的解决方案。

我试图做的问题是创建灵活的子域,例如,如果我有任何的子域像dev.example.com它应该沿的文件目录去/var/www/example.com/www/dev,和任何类型的子域(除WWW)将试图找到一个目录(如果存在) ,使其成为根。

/var/www/example.com/www/{subdomain}
Run Code Online (Sandbox Code Playgroud)

是要查找的当前目录,如果不存在,则默认根目录为:

/var/www/example.com/www/
Run Code Online (Sandbox Code Playgroud)

这是sites-enabled我的域的配置文件。

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑:更新的 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;
    }
}
Run Code Online (Sandbox Code Playgroud)

cyb*_*x86 1

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

通常最好的做法是将“真实”子域放在 Web 根目录之上,以更好地分隔站点(并且具有防止通过主站点访问的优点),并防止目录是否映射到某个目录的歧义。子域与否。例如,请考虑“dev”子域的根路径为以下路径:/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;
    }

}
Run Code Online (Sandbox Code Playgroud)

仅当所有子域都遵循相同的模板时,上述想法才真正有效。在您发布的配置中,pma 子域和 dev 子域有很大不同(因为 dev 子域有许多重写,而 pma 子域没有)。任何不遵循您正在使用的“模板”的子域都将需要自己的服务器块和配置。值得一提的是,如果有两个服务器块适用(例如,一个具有静态 server_name,一个具有正则表达式 server_name),则静态 server_name 将优先。