03-Nginx模块

03-Nginx模块

Nginx-常用模块

1、Nginx目录索引模块

!! ngx_http_autoindex_module模块处理以斜杠字符(‘/’)结尾的请求,并生成目录。当ngx_http_autoindex_module模块找不到索引文件时,通常会将请求传递给模块。

(1)指令

   #启用或禁用目录列表输出,on开启,off关闭。 
   Syntax:   autoindex on | off; 
   Default:  autoindex off;
   Context:  http, server, location

   #指定是否应在目录列表中输出确切的文件大小,on为显示字节,off显示大概单位(KB、MB、GB...)。
   Syntax:   autoindex_exact_size on | off;
   Default:  autoindex_exact_size on;
   Context:  http, server, location

   #指定目录列表中的时间是应该本地时区还是UTC输出。on本地时区,off为UTC时间(原本的时间戳---创建的时间)。
   Syntax:   autoindex_localtime on | off;
   Default:  autoindex_localtime off;
   Context:  http, server, location

(2)示例配置

#第一种(直接访问module.zxc.com 就显示目录列表)
[root@Server-1 ~]# cat /etc/nginx/conf.d/autoindex.conf
server {
 listen 80;
 server_name module.zxc.com;    #没有域名就要添加host记录
 charset utf-8,gbk;             #解决出现中文乱码


 location / {
  root /module;
  autoindex on;                    #启用目录列表输出     
  autoindex_exact_size off;        #输出确切的文件大小
  autoindex_localtime on;          #指定目录列表中的时间为UTC时间
 }
}

#2.准备对应的目录,往目录中上传一点文件
[root@Server-1 ~]# mkdir /module/{centos,ubuntu,redhat}/ -p

#3.检查语法并重新加载Nginx
[root@Server-1 ~]# nginx -t 
[root@Server-1 ~]# systemctl restart nginx
#第二种使用方式(访问module.zxc.com/download 才是显示目录列表)
[root@Server-1 ~]# cat /etc/nginx/conf.d/autoindex.conf 
server {
 listen 80;
 server_name module.zxc.com;     #没有域名就要添加host记录
 charset utf-8,gbk;              #解决出现中文乱码

 location / {
  root /code;
  index index.html index.htm;
 }

 location /download {          # /download此目录是在 /module/download(如果想访问/download,可以将下面(root  /module 改 alias /module))
  root /module;
  autoindex on;                 #启用目录列表输出     
  autoindex_exact_size off;     #输出确切的文件大小
  autoindex_localtime on;       #指定目录列表中的时间为UTC时间
 }
}
#注:download目录下不能有index文件

(3)效果展示03-Nginx模块

2、Nginx状态监控模块

!! ngx_http_stub_status_module模块提供基本状态信息的访问。默认情况下不构建此模块,应使用--with-http_stub_status_module配置参数启动它。

(1)指令

   Syntax:  stub_status;
   Default: —
   Context: server, location

(2)示例配置

#ngx_http_stub_status_module直接加在最后面就行
#注:yum安装的自带ngx_http_stub_status_module,源码编译的需要自己添加。
[root@Server-1 download]# cat /etc/nginx/conf.d/autoindex.conf
server {
        listen 80;
        server_name module.zxc.com;
        charset utf-8,gbk;

        location / {
                root /code;
                index index.html index.htm;
        }

        location /download {
                root /module;
                autoindex on;
                autoindex_exact_size off;
                autoindex_localtime on;
        }

        location /nginx_status{
                stub_status;
        }
}

状态参数

Active connections  # 当前活动客户端连接数,包括Waiting等待连接数。
accepts # 已接受总的TCP连接数。
handled # 已处理总的TCP连接数。
requests # 客户端总的http请求数。
Reading # 当前nginx读取请求头的连接数。
Writing # 当前nginx将响应写回客户端的连接数。
Waiting # 当前等待请求的空闲客户端连接数。

#注意, 一次TCP的连接,可以发起多次http的请求, 如下参数可配置进行验证
keepalive_timeout 0; # 类似于关闭长连接
keepalive_timeout 65; # 65s没有活动则断开连接

(3)效果展示03-Nginx模块

3、Nginx基于IP的访问控制模块

!! ngx_http_access_module模块允许限制对某些客户端地址的访问。

(1)指令

  #允许配置语法
  Syntax:  allow address | CIDR | unix: | all;
  Default: —
  Context: http, server, location, limit_except

  #拒绝配置语法
  Syntax:  deny address | CIDR | unix: | all;
  Default: —
  Context: http, server, location, limit_except

(2)示例配置,拒绝指定的IP访问该网站的/nginx_status,其他IP全部允许访问

 #基于来源的IP地址做限制
 1.拒绝192.168.2.5来源IP访问,其他人允许。
  location /nginx_status {
                stub_status;
                deny 192.168.2.5;
                allow all;
        }
  
 2.允许192.168.2.5来源IP访问,其他人全部拒绝。
     location /nginx_status {
                stub_status;
                allow 192.168.2.5;
                deny all;
        }
  
 3.实际配置监控Nginx状态时,仅允许该服务器的回环地址访问127.0.0.1
     location /nginx_status {
                stub_status;
                allow 127.0.0.1;
                deny all;
        }

4、Nginx基于用户登入认证模块

!! ngx_http_auth_basic_module模块允许使用HTTP基本身份验证,验证用户名和密码来限制对资源的访问。

(1)指令

  #使用HTTP基本身份验证协议启用用户名和密码验证。
  Syntax:  auth_basic string | off;
  Default: auth_basic off;
  Context: http, server, location, limit_except
  
  #使用指定保存用户名和密码的文件
  Syntax:  auth_basic_user_file file;
  Default: —
  Context: http, server, location, limit_except

(2)指定保存用户名和密码的文件,格式如下:

  #可以使用htpasswd程序或者“openssl passwd”命令生成对应的密码;
  
  #需要安装依赖组件
  [root@Server-1 /]# yum install httpd-tools -y
  [root@Server-1 /]# htpasswd -c /etc/nginx/auth_conf zxc
  New password:
  Re-type new password:
  Adding password for user zxc
  [root@Server-1 /]# cat /etc/nginx/auth_conf
  zxc:$apr1$o.7uPYHH$PS94Len/FJCIWVlKWvVtR0
  
  
  #配置Nginx,限制对应的资源
   location /download {
              root /module;
              autoindex on;
              autoindex_exact_size off;
              autoindex_localtime on;
              auth_basic "Please Authorized  password!";   #认证时的描述
              auth_basic_user_file /etc/nginx/auth_conf;  #指定密码文件

5、Nginx连接数访问限制模块

!!   经常会遇到这种情况,服务器流量异常,负载过大等等。对于大流量恶意的攻击访问,会带来带宽的浪费,服务器压力大,影响业务。往往考虑对同一个IP的连接数、请求数,进行限制。

    ngx_http_limit_conn_module模块用于限制定义key的连接数,特别来自单个IP地址的连接数。并非所有连接都被计算在内,仅当连接已经读取了整个请求头时才计算连接。

(1)指令

   Syntax:  limit_conn_zone key zone=name:size;
   Default: —
   Context: http

   Syntax:  limit_conn zone number;
   Default: —
   Context: http, server, location

(2)示例配置,设置共享内存区域和给定的键值的最大允许连接数。超过此限制时,服务器将返回错误以回复请求。

#在HTTP标签段定义连接限制

http{
    limit_conn_zone $binary_remote_addr zone=conn_zone:10m;  #创建10M连接空间。
    #注:$binary_remote_addr IPv4占用4个字节,IPv6占用16个字节,$remote_addr占5~15字节。
}
server {
    limit_conn conn_zone 1;  # 同一时刻只允许一个客户端连接

    location / {
        root /code;
        index index.html;
    }

(3)使用ab工具进行压力测试

[root@Server-1 /]# yum install -y httpd-tools
[root@Server-1 /]# ab -n 50 -c 2  http://127.0.0.1/index.html

6、Nginx请求数访问限制模块

!! ngx_http_limit_req_module模块用于限制定义key(对象)的请求的处理速率,特别单一的IP地址请求的处理速率。

(1)指令

    Syntax:  limit_req_zone key zone=name:size rate=rate ;
    Default: —
    Context: http
   
    Syntax:  limit_req zone=name [burst=number] [nodelay];
    Default: —
    Context: http, server, location

(2)示例配置,设置共享内存区域和请求的最大突发大小。过多的请求被延迟,直到它们的数量超过最大突发大小,在这种情况下请求以错误终止。默认情况下,最大突发大小等于零。

#定义限制的key(基于什么来做限制,IP地址)
http{
     limit_req_zone $binary_remote_addr zone=req_zone:10m rate=1r/s; #创建10M请求空间。
     
     #注:limit_req_zone 只能配置在 http 范围内;
   $binary_remote_addr 表示客户端请求的IP地址;
   rate 请求频率,每秒允许多少请求(1r/s 每秒一次);
}
server {
 limit_req zone=req_zone burst=5 nodelay;  

 #注:bursh是缓冲区,超过了访问频次限制的请求可以先放到这个缓冲区内。
     nodelay参数允许请求在排队的时候就立即被处理,也就是说只要请求能够进入burst队列,就会立即被后台worker处理。
     请注意,这意味着burst设置了nodelay时,系统瞬间的QPS可能会超过rate设置的阈值。nodelay参数要跟burst一起使用才有作用。
  超过访问频次而且缓冲区也满了的时候就会直接返回503
  
 limit_req_status 412;      #默认返回503,可以指定返回错误状态
 error_page 412 /req_error.html;    #这个文件必须存在/code/req_error.html

 location / {
  root /code;
  index index.html;
 }
}

2.填写hosts域名解析
echo "192.168.2.4 module.zxc.com" >> /etc/hosts

3.进行简单的压力测试
 ab -n 50 -c 20 http://test1.oldboy.com/index.html

7、Nginx location模块

!! 使用Nginx Location可以控制访问网站的路径,但一个server可以有多个location配置,多个location的优先级该如何区分?

(1)location语法示例

   location [=|^~|~|~*|!~|!~*|/] /uri/ { ...
    }
    
   匹配符      匹配规则                   优先级
     =         精确匹配                     1
     ^~        以某个字符串开头             2
     ~         区分大小写的正则匹配         3
     ~*        不区分大小写的正则匹配       4
     !~        区分大小写不匹配的正则       5
     !~*       不区分大小写不匹配的正则     6
     /         通用匹配,任何请求都会匹配到  7

(2)示例配置

    #通用匹配,任何请求都会匹配到
    location / {
    ...
    }


    #严格区分大小写,匹配以.php结尾的都走这个location    
    location ~ .php$ {
    ...
    }
    
   
    #严格区分大小写,匹配以.jsp结尾的都走这个location 
    location ~ .jsp$ {
    ...
    }



    #不区分大小写匹配,只要用户访问.jpg,gif,png,js,css 都走这条location
    location ~* .*.(jpg|gif|png|js|css|mp4)$ {
    ...
    }

    #不区分大小写匹配
    location ~* ".(sql|bak|tgz|tar.gz|.git)$" {
    ...
    }

                                                                                                      来都来了 点个赞再走吧~~~03-Nginx模块

原文始发于微信公众号(运维库):03-Nginx模块

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/64327.html

(0)
小半的头像小半

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!