linux运维

第九章:Nginx常用模块

2019-10-16  本文已影响0人  chenkang

第一节:索引目录模块

1.应用场景:

2.参数解释:

官方模块说明:
http://nginx.org/en/docs/http/ngx_http_autoindex_module.html

3.操作步骤:编写nginx自配置文件

[root@web01 /data/yum]# cat /etc/nginx/conf.d/index.conf 
server {
    listen       80;
    server_name  yum.mysun.com;
    location / {
        autoindex on;
        autoindex_exact_size off;
        autoindex_localtime on;
        autoindex_format html;
        charset utf-8,gbk;
        root   /data/yum;
        index  index.html index.htm;
    }
}

4.创建数据目录

mkdir /data/yum

5.下载测试软件

yum install --downloadonly --downloaddir=/data/yum mariadb screen -y
echo "biubiubiubiu" >test.txt
echo "biubiubiubiu" >test.txt

第二节:状态监控

1.字段解释

注意, 一次 TCP 的连接,可以发起多次 http 的请求, 如下参数可配置进行验证

2.配置文件

[root@web01 ~]# cat /etc/nginx/conf.d/status.conf 
server {
   listen 80;
   server_name  status.oldboy.com;
   stub_status on;
   access_log off;
}

3.测试访问

[root@web01 ~]# curl status.oldboy.com
Active connections: 1 
server accepts handled requests
 4 4 6 
Reading: 0 Writing: 1 Waiting: 0 

第三节:基于用户名密码访问控制

1.安装httpd-tools工具

yum install httpd-tools -y 

2.生成密码文件

htpasswd -b -c /etc/nginx/auth_conf admin admin

3.配置nginx配置文件

server {
    listen       80;
    server_name  yum.mysun.com;
    location / {
        autoindex on;
        autoindex_exact_size off;
        autoindex_localtime on;
        autoindex_format html;
        charset utf-8,gbk;
        auth_basic "mysunnnnn!";
        auth_basic_user_file auth_conf;
        root   /data/yum;
        index  index.html index.htm;
    }
}

4.检查并重启nginx

nginx -t 
systemctl restart nginx 

第四节:基于IP访问控制

1.命令解释

http://nginx.org/en/docs/http/ngx_http_access_module.html

2.参数配置

#拒绝10网段,允许其他网段
deny 10.0.0.0/24 ;
allow all; 

#拒绝10.0.0.1访问,允许其他访问
deny 10.0.0.1 ;
allow all;

#只允许172网段访问
allow 172.16.1.0/24;
deny all;

#只允许172.16.1.7可以访问
allow 172.16.1.7;
deny all;

第五节:根据请求限速

1.参数解释:

定义一条规则

limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;

limit_req_zone          #引用限速模块
$binary_remote_addr     #判定条件,每个请求的IP
zone=one:10m            #定义一个zone名称
rate=1r/s;              #限制速度,1秒1次

引用一条限速规则

limit_req zone=two burst=5 nodelay;

limit_req               #引用限速规则语法
zone=one                #引用哪一条规则
burst=5                 #令牌桶,允许排队的数量
nodelay;                #如果不希望在请求被限制时延迟过多的请求,则应使用参数nodelay

2.Nginx配置文件

[root@web01 /etc/nginx/conf.d]# cat index.conf

limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
limit_req_zone $binary_remote_addr zone=two:10m rate=2r/s;

server {
    listen       80;
    server_name  yum.mysun.com;
    location / {
        limit_req zone=two burst=5 nodelay;
        autoindex on;
        autoindex_exact_size off;
        autoindex_localtime on;
        autoindex_format html;
        charset utf-8,gbk;
        root   /data/yum;
        index  index.html index.htm;
    }
}

3.测试访问

一秒1次
for i in {1..10000};do curl -I 10.0.0.7;sleep 1;done

一秒2次
for i in {1..10000};do curl -I 10.0.0.7;sleep 0.5;done

一秒5次
for i in {1..10000};do curl -I 10.0.0.7;sleep 0.2;done

相当于
for i in {1..10000};
do
curl -I 10.0.0.7;
sleep 1;
done

4.ab压测工具:

ab -c 10 -n 100 10.0.0.7/
option:-c 并发数
       -n 请求总数

第六节: location匹配

1.配置语法解释

https://www.jianshu.com/p/2026360ff272

2.配置参数

[root@web01 ~]# cat /etc/nginx/conf.d/index.conf

server {
    listen       80;
    server_name  yum.mysun.com;

    location / {
        return 200  "location / \n";
    }

    location = / {
        return 200 "location = \n";
    }

    location /documents/ {
        return 200 "location /documents/ \n";
    }

    location ^~ /image/ {
        return 200 "location ^~ /images/ \n";

    }

    location ~* \.(gif|jpg|jpeg)$ {
        return 200 "location ~* \.(gif|jpg|jpeg) \n";
    }

    location ~* /mysun/ {
        return 200 "location !~ mysun \n";
    }

    location ~ /myqu/ {
        return 200 "location ~ myqu \n";
    }
}

3.测试命令

curl yum.mysun.com
curl yum.mysun.com/documents
curl yum.mysun.com/documents/
curl yum.mysun.com/documents/
curl yum.mysun.com/documents
curl yum.mysun.com/documents/
curl yum.mysun.com/hahahah
curl yum.mysun.com/images/
curl yum.mysun.com/imagesssss/
curl yum.mysun.com/imagesssss
curl yum.mysun.com/imaaaaaa
curl yum.mysun.com/ima22222
curl yum.mysun.com/sunsunsun.jpg
curl yum.mysun.com/sunsunsun.jPg
curl yum.mysun.com/sunsunSSS.jPg
curl yum.mysun.com/mysun/
curl yum.mysun.com/mysUn/
curl yum.mysun.com/mYsUn/
curl yum.mysun.com/myqu/
curl yum.mysun.com/myqU/
curl yum.mysun.com/mYqU/
curl yum.mysun.com/image/hahah.jpg
上一篇下一篇

猜你喜欢

热点阅读