Nginx 核心配置详解

时间:2023-03-09 21:49:39
Nginx 核心配置详解

Nginx 核心配置详解

Nginx 四层访问控制:

准备两个客户端,做访问测试使用。
centos7 IP:192.168.39.7
centos6 IP:192.168.39.6
[root@ubuntu images1]#vim /apps/nginx/conf/conf.d/pc.conf
# nginx的访问控制是基于先后顺序来决定优先级的。
location ~* \.(gif|jpg|jpeg|bmp|png|tiff|tif|ico|wmf|js)$ {
root /data/nginx/images1;
index index.html;
deny 192.168.39.7; # 拒绝这个IP访问
allow 192.168.39.0/24; # 允许这个网段访问(结合含义允许这个网段访问但是因为deny在前已经明确过“7”IP不可以访问,所以是除了“7”IP以外的这个网段都可以访问)
allow 10.1.1.0/16; # 允许这个网段访问 (我没有设置这个网段,添加他的意义是可以添加多个网段)
allow 2001:0db8::/32; # 允许ipv6地址
deny all; # 拒绝所有IP访问(结合在一起另一层含义拒绝除这一行以上的所有IP)
}
# 检查语法
[root@ubuntu images]#/apps/nginx/sbin/nginx -t
nginx: [warn] low address bits of 10.1.1.0/16 are meaningless in /apps/nginx/conf/conf.d/pc.conf:50 # 这个错不用管,因为我没有这个网段的地址)
nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /apps/nginx/conf/nginx.conf test is successful
# 重启服务或重新加载服务
[root@ubuntu images]#systemctl restart nginx.service
# 分别在centos6、7上做本地域名解析
[root@centos7 ~]$vim /etc/hosts
192.168.39.184 www.OpengSD.net
[root@magedu ~]$vim /etc/hosts
192.168.39.184 www.OpengSD.net # centos7访问测试我拒绝了这个IP访问所以显示403
[root@centos7 ~]$curl http://www.opengsd.net/1.jpg
<html>
<head><title>403 Forbidden</title></head>
<body>
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.16.1</center>
</body>
</html> # centos6访问测试
[root@magedu ~]$curl www.OpengSD.net/1.jpg # 因为是图片没有图形界面显示不了(但是没有报403拒绝),下面有新的测试访问。