Problems with nginx virtual host and second-level domain name correspondence
某草草
某草草 2017-05-16 17:17:54
0
2
644

When using nginx, how do you deal with the corresponding problem between the virtual host and the second-level domain name? For example, there are two second-level domain names, corresponding to two folders:

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

Then, in the configuration file, there are two corresponding 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;
    #}
}

Question:
1. If there are 100 second-level domain names, 100 server must be written. Can it be written in 1 server? Can you give me an example?
2. Write 100 server and 1 server, which method is better?

某草草
某草草

reply all(2)
Peter_Zhu

If there are 100 different projects, of course you have to write 100 servers (there will be many customizations, the most common one is rewrite). If there is a project corresponding to multiple domain names, the number of servers can be reduced. Even if there are The method is to write the configuration of 100 projects in one server. I estimate that the configuration file will be very complicated and will be a headache to manage. It is better to write 100 servers.

Configuration is not equal to complexity
Configuration is not equal to complexity
Configuration is not equal to complexity

"Many" should never be a concern for configuration management.

左手右手慢动作

If the configurations are similar, you can judge by nginx variables, so you only need to write one configuration

Reference content:
http://bneijt.nl/blog/post/name-based-vi...
http://www.sitepoint.com/set-automatic-v...

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template