Home  >  Q&A  >  body text

Problems with nginx configuration server module

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?

世界只因有你世界只因有你2714 days ago478

reply all(3)I'll reply

  • 我想大声告诉你

    我想大声告诉你2017-05-16 17:11:58

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

    reply
    0
  • 巴扎黑

    巴扎黑2017-05-16 17:11:58

    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.

    reply
    0
  • 淡淡烟草味

    淡淡烟草味2017-05-16 17:11:58

    Server name can specify multiple domain names, separated by spaces

    reply
    0
  • Cancelreply