這是製作子網域的好習慣嗎

這是製作子網域的好習慣嗎

我正在嘗試使用我的網路應用程式建立子網域,但是,我在nginx方面沒有太多經驗,我一直在嘗試從SF找到穩定的解決方案,但不幸的是我找不到任何好的解決方案。

我想要做的問題是創建靈活的子域,例如,如果我有任何子域,那麼dev.example.com它應該沿著 的文件目錄/var/www/example.com/www/dev,並且任何類型的子域(WWW 除外)將嘗試查找目錄(如果存在) ,將其設為根。

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

是目前要尋找的目錄,如果不存在,則預設根目錄為:

/var/www/example.com/www/

這是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;
    }
}

編輯:更新的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;
    }
}

答案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;
    }

}

只有當所有子域都遵循相同的模板時,上述想法才真正有效。在您發佈的配置中,pma 子網域和 dev 子網域有很大不同(因為 dev 子網域有許多重寫,而 pma 子網域沒有)。任何不遵循您正在使用的「範本」的子網域都將需要自己的伺服器區塊和配置。值得一提的是,如果有兩個伺服器區塊適用(例如,一個具有靜態 server_name,一個具有正規表示式 server_name),則靜態 server_name 將優先。

相關內容