Problems with nginx configuration server module
世界只因有你
世界只因有你 2017-05-16 17:09:58
0
3
561

Assume there are 5 second-level domain names:

aaa.example.com
bbb.example.com
ccc.example.com
ddd.example.com
eee.example.com

When configuring nginx, the server module looks like this:

server {
    listen 443 ssl http2;
    
    server_name  aaa.example.com;
    
    root   /var/www/aaa.example.com/public;
    index  index.php index.html index.htm;
    
    location / {
        root   /var/www/aaa.example.com/public;
        try_files $uri $uri/ /index.php?$query_string;
        index  index.php index.html index.htm;
    }


    location ~ \.php$ {
        try_files $uri /index.php =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass   unix:/dev/shm/php-fpm.sock;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
    
    #...
    #...
    #...
}

Question:
1. There are 5 second-level domain names. Do I need to write 5 server modules? Can I write a general-purpose server module that only uses one?

2. If you write 5 server modules, the location ~ \.php${ } modules in each server module are the same. This location ~ \.php${ } Can a module be written only once and shared? That is, can it be moved to the http module, the upper module of the server module?

3. In many examples, the root and index must be written twice, once in the server, and again in the next layer's location / { } module. Why is this?

世界只因有你
世界只因有你

reply all(3)
我想大声告诉你
server_name ~^(?<site>(aa|bb|cc)\.example\.com)$;
root /var/www/$site/public; # location 里的root如果一样,可以不需要了,index也一样,大多数人是复制的
巴扎黑

1. When your five domain names point to the same root directory and represent the same site, server_name can specify multiple domain names, separated by spaces; when your five domain names represent different sites, you must configure multiple server segments, usually with include directive to introduce multiple conf files, each domain name is a conf file.
2.location directive can only be used in server and location; see official documentation for details:

3. The root index in location can share the root index in server.

淡淡烟草味

Server name can specify multiple domain names, separated by spaces

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