nginx設定server模組的問題
世界只因有你
世界只因有你 2017-05-16 17:09:58
0
3
468

假設有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 / { }模組中再寫一遍,這是為什麼?

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

全部回覆(3)
我想大声告诉你

雷雷

巴扎黑

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

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

淡淡烟草味

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

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!