在上一篇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