在上一篇Nginx服務構建及訪問狀態統計的基礎上,嘗試完成Nginx虛擬主機及訪問控制實驗。php
RHEL6-5(IP地址192.168.100.110)
Win7-1(IP地址192.168.100.202)html配置虛擬主機實驗過程:
1.修改主配置文件
# vim /usr/local/nginx/conf/nginx.conf 在配置文件的末尾單獨插入就不會有影響,注意格式(主要就是注意括號問題!) server { server_name www.benet.com; location / { root /var/www/benet; index index.html index.php; } } server { server_name www.accp.com; location / { root /var/www/accp; index index.html index.php; } } } //這個括號須要把原文件末尾的括號給去掉,用nginx -t 去檢測配置文件是否配置正確//
# mkdir /var/www/benet /var/www/accp # vim /var/www/benet/index.html //添加並編輯benet首頁文件 this is benet //寫入首頁內容 # vim /var/www/accp/index.html //添加並編輯accp首頁文件 this is accp //寫入首頁內容
# service nginx restart //重啓nginx服務
# htpasswd -c /usr/local/nginx/passwd.db zhangsan
# chown nginx /usr/local/nginx/passwd.db # chmod 400 /usr/local/nginx/passwd.db
# vim /usr/local/nginx/conf/nginx.conf location / { auth_basic "secret"; auth_basic_user_file /usr/local/nginx/passwd.db; root html; index index.html index.htm; }
# nginx –t //測試語法 # service nginx reload //重啓服務
# vim /usr/local/nginx/conf/nginx.conf location / { deny 192.168.100.110; allow all; root html; index index.html index.htm; }
# service nginx reload //重啓服務
測試成功!nginx