关于nginx服务器块对应的二级域名

关于nginx服务器块对应的二级域名

有2个二级域名,对应的文件夹有2个,如下:

domain name    folder
111.aa.com     /var/www/111.aa.com
222.aa.com     /var/www/222.aa.com

其中nginx.conf,有 2 个服务器块,如下所示:

#111.aa.com

server {
    listen       80;
    server_name  111.aa.com;

    charset utf-8;
    #access_log  /var/log/nginx/log/host.access.log  main;

    location / {
        root   /var/www/111.aa.com;
        index  index.php index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}


    location ~ \.php$ {
        root           /var/www/111.aa.com;
        fastcgi_pass   unix:/dev/shm/php-fpm.sock;
        try_files $uri /index.php =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        fastcgi_param  QUERY_STRING     $query_string;
        include        fastcgi_params;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

#222.aa.com

server {
    listen       80;
    server_name  222.aa.com;

    charset utf-8;
    #access_log  /var/log/nginx/log/host.access.log  main;

    location / {
        root   /var/www/222.aa.com;
        index  index.php index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}


    location ~ \.php$ {
        root           /var/www/222.aa.com;
        fastcgi_pass   unix:/dev/shm/php-fpm.sock;
        try_files $uri /index.php =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        fastcgi_param  QUERY_STRING     $query_string;
        include        fastcgi_params;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

问题:
1、一定要写两个服务器块吗?如果有100个二级域名,我要写100个服务器块,写在一个服务器块里可以吗?怎么写在一个服务器块里?有例子吗?

2、写入一个服务器块和写入100个服务器块分别写入哪个更好?

答案1

1、一定要两个服务器块吗?

不,你可以用以下方法实现它:

server_name  111.aa.com 222.aa.com;

或者:

server_name  *.aa.com;

然后:

location / {
        root   /var/www/$server_name;
        index  index.php index.html index.htm;
    }

看:http://nginx.org/en/docs/http/server_names.html

2、写入一个服务器块和写入100个服务器块分别写入哪个更好?

每个块一个服务器为您提供了更大的粒度。因此,如果以后 999.aa.com 有一些不同的要求,您可以只更新其单个块,重新加载 nginx 即可。

所以,这关乎控制程度。但两种方法都可以。

相关内容