首頁  >  問答  >  主體

nginx設定server模組的問題

假設有5個二級域名:

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

在設定nginx的時候,server模組是這樣的:

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;
    }
    
    #...
    #...
    #...
}

問題:
1、有5個二級域名,必須要寫5個server模組嗎?可以寫個通用的只用一個server模組嗎?

2、如果寫5個server模組的話,每個server模組中的location ~ \.php${ }模組是一樣的,這個location ~ \.php${ } 模組可以只寫一遍來共用嗎?也就是可以把它弄到server模組的上一層模組http模組去嗎?

3、看到很多範例的root和index都要寫兩遍,server裡面寫一遍,下一層的location / { }模組中再寫一遍,這是為什麼?

世界只因有你世界只因有你2714 天前476

全部回覆(3)我來回復

  • 我想大声告诉你

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

    雷雷

    回覆
    0
  • 巴扎黑

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

    1.當你的5個域名指向同一根目錄,表示同一站點時,server_name可以指定多個域名,用空格分隔;當你的5個域名表示不同站點,就要配置多個server段,通常用include指令來引入多個conf文件,每個網域是一個conf文件。
    2.location 指令只能是在server、location裡;詳見官方文件說明:

    3.location裡的root index 可以共用server裡的root index.

    回覆
    0
  • 淡淡烟草味

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

    server name是可以指定多個網域的,用空格分隔

    回覆
    0
  • 取消回覆