nginx配置server模塊的問題
問題描述
假設(shè)有5個二級域名:
aaa.example.combbb.example.comccc.example.comddd.example.comeee.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;includefastcgi_params; }#... #... #...}
問題:1、有5個二級域名,必須要寫5個server模塊嗎?可以寫個通用的只用一個server模塊嗎?
2、如果寫5個server模塊的話,每個server模塊中的location ~ .php${ }模塊是一樣的,這個location ~ .php${ }模塊可以只寫一遍來共用嗎?也就是可以把它弄到server模塊的上一層模塊http模塊去嗎?
3、看到很多示例的root和index都要寫兩遍,server里面寫一遍,下一層的location / { }模塊中再寫一遍,這是為什么?
問題解答
回答1:server_name ~^(?<site>(aa|bb|cc).example.com)$;root /var/www/$site/public; # location 里的root如果一樣,可以不需要了,index也一樣,大多數(shù)人是復(fù)制的回答2:
1.當你的5個域名指向同一根目錄,表示同一站點時,server_name可以指定多個域名,用空格分隔;當你的5個域名表示不同站點,就要配置多個server段,通常用include指令來引入多個conf文件,每個域名是一個conf文件。2.location 指令只能是在server、location里;詳見官方文檔說明:3.location里的root index 可以共用server里的root index.
server name是可以指定多個域名的,用空格分隔
相關(guān)文章:
1. docker-compose 為何找不到配置文件?2. boot2docker無法啟動3. docker images顯示的鏡像過多,狗眼被亮瞎了,怎么辦?4. java中返回一個對象,和輸出對像的值,意義在哪兒5. android - E/dalvikvm: Could not find class java.nio.file.Path,6. python - linux怎么在每天的凌晨2點執(zhí)行一次這個log.py文件7. android - 哪位大神知道java后臺的api接口的對象傳到前端后輸入日期報錯,是什么情況?求大神指點8. mysql數(shù)據(jù)庫每次查詢是一條線程嗎?9. python是怎么實現(xiàn)過濾 #注釋代碼的?10. 請問一下各位老鳥 我一直在學(xué)習(xí)獨孤九賤 現(xiàn)在是在tp5 今天發(fā)現(xiàn) 這個系列視頻沒有實戰(zhàn)
