하위 도메인을 만드는 것이 좋은 방법입니까?

하위 도메인을 만드는 것이 좋은 방법입니까?

웹 애플리케이션으로 하위 도메인을 만들려고 하는데 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의 일부를 변수에 할당할 수 있습니다(예: 경로 설정).

일반적으로 웹 루트 위에 '실제' 하위 도메인을 배치하고, 사이트를 더 효과적으로 분리하고(메인 사이트를 통한 액세스를 방지하는 이점이 있음) 디렉터리가 매핑되는지 여부에 대한 모호성을 방지하는 것이 좋습니다. 하위 도메인인지 아닌지. 예를 들어 '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이 우선한다는 점을 언급할 가치가 있습니다.

관련 정보