目录
一、Nginx 配置虚拟主机
1.基于域名的虚拟主机
示例:
[root@nginx-server ~]# vim /usr/local/nginx/conf/nginx.conf
##检查nginx配置是否正确
[root@nginx-server nginx]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
##创建Nginx配置中的目录
[root@nginx-server nginx]# mkdir -p /web/rz
[root@nginx-server nginx]# mkdir -p /web/rz.mail.com
##在对应访问目录下创建文件
[root@nginx-server nginx]# echo "<h1>rz.local</h1>" >/web/rz/index.html
[root@nginx-server nginx]# echo "<h1>rz.mail.com</h1>" >/web/rz.mail.com/index.html
##查看当前路径
[root@nginx-server nginx]# pwd
/usr/local/nginx
##重启服务
[root@nginx-server nginx]# /usr/local/nginx/sbin/nginx -s reload
本地windows hosts配置:
C:\Windows\System32\drivers\etc
验证:
2.基于 IP 地址的虚拟主机
临时添加IP进行测试:
#重启nginx
[root@nginx-server ]# /usr/local/nginx/sbin/nginx -s reload
验证:
3.基于端口的虚拟主机
[root@nginx-server ~]# vim /usr/local/nginx/conf/nginx.conf
[root@nginx-server ~]# /usr/local/nginx/sbin/nginx -s reload
验证:
二、Nginx 的访问控制
1.基于用户的访问控制
[root@nginx-server ~]# yum -y install httpd-tools
[root@nginx-server ~]# htpasswd -c -b /usr/local/nginx/passwd.db admin 123123
Adding password for user admin
[root@nginx-server ~]# cat /usr/local/nginx/passwd.db
admin:$apr1$GADrdzyG$6uCFSuPzQQoJsQ4sG060h.
[root@nginx-server ~]# chmod 400 /usr/local/nginx/passwd.db
[root@nginx-server ~]# chown nginx:nginx /usr/local/nginx/passwd.db
[root@nginx-server ~]# ll /usr/local/nginx/passwd.db
-r-------- 1 nginx nginx 44 Aug 11 07:30 /usr/local/nginx/passwd.db
[root@nginx-server ~]# vim /usr/local/nginx/conf/nginx.conf
##重启服务
[root@nginx-server ~]# /usr/local/nginx/sbin/nginx -s reload
测试验证:
##用户名密码是上面设置的admin/123123
2.基于 IP 的访问控制
deny ip/ip 段
allow ip/ip 段
规则匹配从上往下,如匹配,则停止
[root@nginx-server ~]# vim /usr/local/nginx/conf/nginx.conf
[root@nginx-server ~]# /usr/local/nginx/sbin/nginx -s reload
客户端测试:
修改访问策略,再次进行测试:
[root@nginx-server ~]# vim /usr/local/nginx/conf/nginx.conf
[root@nginx-server ~]# /usr/local/nginx/sbin/nginx -s reload