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?
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