首页 > nginx虚拟主机和二级域名对应问题

nginx虚拟主机和二级域名对应问题

使用nginx的时候,虚拟主机和二级域名对应问题,大家是怎么做的,例如,有两个二级域名,对应两个文件夹:

域名         文件夹
111.aa.com   /var/www/111.aa.com
222.aa.com   /var/www/222.aa.com

那么,在配置文件中,就对应两个server
#111.aa.com

#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

#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个server,能否写在1个server中呢,能否给个示例?
2、写100个server和1个server,那种方式更好呢?


如果配置都差不多,可以通过nginx的变量来判断,这样只要写1个配置就可以了

可参考的内容:
http://bneijt.nl/blog/post/name-based-vi...
http://www.sitepoint.com/set-automatic-v...


如果是100个不同的项目, 当然要写100个server(会存在很多定制的情况, 最常见的就是rewrite). 如果是有一个项目对应多个域名的情况, server数量可以减少一些. 即便有方法将100个项目的配置写在一个server里, 那配置文件我估计也会非常复杂, 管理起来会很头疼. 倒不如写100个server来的方便.

配置 多 不等于 复杂
配置 多 不等于 复杂
配置 多 不等于 复杂

"多" 从来不应该是配置管理担心的事情.

【热门文章】
【热门文章】