第九章:Nginx常用模块

第一节:索引目录模块

1.应用场景:

  • 简易下载
  • 内网YUM仓库,配合其他软件

2.参数解释:

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

  • autoindex on;
    打开索引模式
  • autoindex 常用参数
  • autoindex_exact_size off;
    默认为 on, 显示出文件的确切大小,单位是 bytes。
    修改为 off,显示出文件的大概大小,单位是 kB 或者 MB 或者 GB。
  • autoindex_localtime on;
    默认为 off,显示的文件时间为 GMT 时间。
    修改为 on, 显示的文件时间为文件的服务器时间。
  • charset utf-8,gbk;
    默认中文目录乱码,添加上解决乱码

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.字段解释

  • Active connections # 当前活动的连接数
  • accepts # 当前的总连接数 TCP
  • handled # 成功的连接数 TCP
  • requests # 总的 http 请求数
  • Reading # 请求
  • Writing # 响应
  • Waiting # 等待的请求数,开启了 keepalive

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

  • keepalive_timeout 0; # 类似于关闭长连接
  • keepalive_timeout 65; # 65s 没有活动则断开连接

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
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 220,639评论 6 513
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 94,093评论 3 396
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 167,079评论 0 357
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,329评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,343评论 6 397
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 52,047评论 1 308
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,645评论 3 421
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,565评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 46,095评论 1 319
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,201评论 3 340
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,338评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 36,014评论 5 347
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,701评论 3 332
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,194评论 0 23
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,320评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,685评论 3 375
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,345评论 2 358

推荐阅读更多精彩内容