nginx配置server模塊的問(wèn)題
問(wèn)題描述
假設(shè)有5個(gè)二級(jí)域名:
aaa.example.combbb.example.comccc.example.comddd.example.comeee.example.com
在配置nginx的時(shí)候,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;includefastcgi_params; }#... #... #...}
問(wèn)題:1、有5個(gè)二級(jí)域名,必須要寫(xiě)5個(gè)server模塊嗎?可以寫(xiě)個(gè)通用的只用一個(gè)server模塊嗎?
2、如果寫(xiě)5個(gè)server模塊的話,每個(gè)server模塊中的location ~ .php${ }模塊是一樣的,這個(gè)location ~ .php${ }模塊可以只寫(xiě)一遍來(lái)共用嗎?也就是可以把它弄到server模塊的上一層模塊http模塊去嗎?
3、看到很多示例的root和index都要寫(xiě)兩遍,server里面寫(xiě)一遍,下一層的location / { }模塊中再寫(xiě)一遍,這是為什么?
問(wèn)題解答
回答1:server_name ~^(?<site>(aa|bb|cc).example.com)$;root /var/www/$site/public; # location 里的root如果一樣,可以不需要了,index也一樣,大多數(shù)人是復(fù)制的回答2:
1.當(dāng)你的5個(gè)域名指向同一根目錄,表示同一站點(diǎn)時(shí),server_name可以指定多個(gè)域名,用空格分隔;當(dāng)你的5個(gè)域名表示不同站點(diǎn),就要配置多個(gè)server段,通常用include指令來(lái)引入多個(gè)conf文件,每個(gè)域名是一個(gè)conf文件。2.location 指令只能是在server、location里;詳見(jiàn)官方文檔說(shuō)明:3.location里的root index 可以共用server里的root index.
server name是可以指定多個(gè)域名的,用空格分隔
相關(guān)文章:
1. javascript - 按鈕鏈接到另一個(gè)網(wǎng)址 怎么通過(guò)百度統(tǒng)計(jì)計(jì)算按鈕的點(diǎn)擊數(shù)量2. java - jdbc如何返回自動(dòng)定義的bean3. python - 請(qǐng)問(wèn)這兩個(gè)地方是為什么呢?4. 請(qǐng)教一個(gè)mysql去重取最新記錄5. 大家都用什么工具管理mysql數(shù)據(jù)庫(kù)?6. Python處理Dict生成json7. mysql的循環(huán)語(yǔ)句問(wèn)題8. python - 為什么match匹配出來(lái)的結(jié)果是<_sre.SRE_Match object; span=(0, 54), match=’’>9. mysql優(yōu)化 - mysql 一張表如果不能確保字段列長(zhǎng)度一致,是不是就不需要用到char。10. mysql updtae追加數(shù)據(jù)sql語(yǔ)句
