美文网首页linux运维
第九章:Nginx常用模块

第九章:Nginx常用模块

作者: chenkang | 来源:发表于2019-10-16 12:32 被阅读0次

第一节:索引目录模块

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

相关文章

  • nginx 2

    1 nginx 常用模块整理 nginx 常用模块整理1 http核心模块 ngx_http_core_modu...

  • nginx与lvs详解

    详细描述常见nginx常用模块和模块的使用示例 nginx常见的模块分类: 核心模块:core moduleNgi...

  • Nginx与lvs详解

    详细描述常见nginx常用模块和模块的使用示例 nginx常见的模块分类: 核心模块:core moduleNgi...

  • Nginx配置referer校验,实现简单的防盗链详解

    1、Nginx Referer模块 nginx模块ngx_http_referer_module通常用于阻挡来源非...

  • nginx常用模块使用及原理了解

    nginx常用模块了解及使用 nginx常用模块介绍及使用 对客户端进行限制相关配置-预防攻击 一般是下面的需求:...

  • nginx 的 rewrite

    其实nginx的rewrite模块是在日常nginx配置中十分常用的模块,应该是除了 real_ip 模块、匹配时...

  • 第九章:Nginx常用模块

    第一节:索引目录模块 1.应用场景: 简易下载 内网YUM仓库,配合其他软件 2.参数解释: 官方模块说明:htt...

  • nginx常用模块

    nginx常用模块 1、日志模块 ngx_http_log_module 2、压缩传输模块 ngx_http_gz...

  • Nginx常用模块

    本文介绍了几个Nginx常用模块的使用方法。 ngx_http_ssi_module 官方说明文档: 要点: 在 ...

  • Nginx 常用模块

    Linux System Environment 标注:本章节教程以Nginx以源码编译安装环境讲解,默认Ngin...

网友评论

    本文标题:第九章:Nginx常用模块

    本文链接:https://www.haomeiwen.com/subject/ajgymctx.html