Nginx模块

Nginx常用基础模块

1.Nginx目录索引

2.Nginx状态监控

3.Nginx访问控制

4.Nginx资源限制

5.Nginx访问限制

6.Nginx Location

1. Nginx目录索引

 ngx_http-autoindex_module模块处理以斜杠字符(‘/’)结尾的请求,并生成目录列表。

 当ngx_http-module模块找不到索引文件时,通常会将请求传递给模块

[root@web01 /etc/nginx/conf.d]# vim share.huhao.com.conf
server {
   listen 80;
   server_name share.huhao.com;
   root  /;
   #默认均为关闭,手动添加
   autoindex on;  (目录索引)
 }                                                                            
"share.huhao.com.conf" 6L, 97C written                                                              
[root@web01 /etc/nginx/conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 /etc/nginx/conf.d]# systemctl  reload nginx

输出如下显示结果

server{
 #展示文件大小
 autoindex_exact_size;

}



2. stub—status 状态模块

  • ngx_http_stub_status_module 显示Nginx状态

stub_status

应用场景

[admin@admin /etc/nginc/conf.d]# vim www.huhao.com.conf

server {
  listen 80;  
  server_name www.huhao.com  
  access_log /var/log/www_access.log main;  
  
  location / {
  root /code/www 
  index index.html; 
  
  location /status {
  stub_status;  #状态默认
  access_log off; #关闭日志
   }
  }
}


stub_status

输出如下显示结果


命令行状态显示


3.访问限制 allow-deny

  • ngx_http_access_module 访问限制模块

1.allow    准许   某个ip或网段访问

2.deny    拒绝

3.使用allow和deny 完成白名单和黑名单功能

4.白名单:常用,用来限制核心目录,文件,禁止外界访问

5黑名单:deny,屏蔽ip地址


应用场景


白名单 在名单中的可以访问

[root@web01 /etc/nginx/conf.d]# vim www.huhao.com.conf 
server {
   listen 80;
   server_name www.huhao.com;
   access_log  www_access.log mian;
   
   
   location / {
    root /code/www;
    index index.html index.php;
   }
   
   #白名单 在名单中的可以访问
   
   ## allow
   ## 最后deny all
   location /status {
   stub_status;
   allow 10.0.0.7
   allow 10.0.0.1  #原下日志记录403 
   allow 172.16.1.0/24;
   deny all;
   
   }
}

[root@admin ~]# nginx  -t
      
[root@admin ~]# systemctl reload nginx 



命令行状态显示



黑名单 用来屏蔽某一个ip地址

 # 黑名单  用来屏蔽某一个ip地址
 
 # 先deny
 ## 最后allow all
server {
    listen 80; 
    server_name www.huhao.com;
    access_log /var/log/www_access.log main;
   
   location / {
     root /code/www;
     index index.html;
   }
   
    #
    location /status {
    deny 10.0.0.1;
    }  
}

[root@admin ~]# nginx -t 

[root@admin ~]# systemctl reload nginx 


4.用户授权模块 auth_basic

  • ngx_http_auth_basic_module 用户授权模块
  • 限制用户访问,访问的时候输入用户名和密码

#让用户访问 www.huhao.com/status 需要输入用户名和密码



创建auth_basic_user_file 需要的密码文件

conf.d/status htpasswd

登录IP地址需要输入用户名以及密码

1、安装httpd-tools
[root@admin ~]# yum install httpd-tools -y 

2、生成用户名密码文件
#创建用户名和密码
#-c 创建新文件 
#-b 在命令行中输入密码,而不是提示输入
[root@web01 /etc/nginx/conf.d]#htpasswd  -bc  /etc/nginx/conf.d/status.pass  huhao 123456
Adding password for user huhao

[root@web01 /etc/nginx/conf.d]#ll /etc/passwd -d
-rw-r--r-- 1 root root 1309 Apr 19 11:45 /etc/passwd

#给密码权限600
[root@web01 /etc/nginx/conf.d]#chmod 600 status.pass 

#把-c 去掉 创建新文件,不加-c是追加
[root@web01 /etc/nginx/conf.d]#htpasswd  -b  /etc/nginx/conf.d/status.pass  huhao 0123

3、将密码文件路径加入Nginx配置
#修改Nginx配置
[root@web01 /etc/nginx/conf.d]#cat www.huhao.com.conf 
server {
   listen 80;
   server_name www.huhao.com;
   access_log  /var/log/nginx/www_access.log main; 

   #站点目录文件
   location  / {
   root  /code/www;
   index index.html; 
  }
  
 #图标报错403,强制返回200 
 location /favicon.ico {
  access_log off;
  return 200;
 }

 location /status {
 stub_status;  #显示状态信息
 #显示登录提示
 auth_basic "Welcome to Login:"; 
 #指定密码文件
 auth_basic_user_file /etc/nginx/conf.d/status.pass;
}
}

4、重启Nginx
[root@admin ~]# nginx -t

[root@admin ~]systemctl reload nginx

[root@web01 /etc/nginx/conf.d]#ll /etc/nginx/conf.d/
-rw------- 1 root root  44 May 10 20:57 status.pass

#错误日志状态信息报403 密码文件加入属于用户
[root@web01 /etc/nginx/conf.d]#chown  nginx  /etc/nginx/conf.d/status.pass 


输出如下显示结果

命令输出显示结果


5.请求限制

  • limit_req request模块 限制请求(http)
  • limit_conn connection 限制连接(tcp)
  • limit_rate core模块 限速速度

限制请求

  • limit_req 限制请求
  1. limit_req #限制请求
  2. limit_req_zone #创建木桶空间以及木桶处理的速率

limit_req 用于限制每个已定义密钥的请求处理速率,特别是来自单个IP地址的请求的处理速率。限制是使用"漏桶"方法完成的



http {
limit_req_zone $binart_remote_addr zone=one:10m reta=1r/s;
#$binary_remote_addr 用户ip地址,占用空间更少
#$zone=one:10m 指定空间名字:大小
#$rate=1r/s   指定木桶处理速度

}

#补充:客户端IP地址用作密钥。请注意,这里使用的
不是$remote_addr,而是$binary_remote_addr变量。
$binary_remote\u addr变量的大小对于IPV4地址总
是4字节,对于IPV6地址总是16字节。

limit_req

limit_req zone=one burst=5;

zone=one #指定limit_req_zone   创建的木桶空间
burst=5  #并发5


#1. 准备环境
挂载光盘  复制里面软件包 到 /code/share/centos
[root@web01 /etc/nginx/conf.d]#cat share.huhao.com.conf 
server {
   listen 80;
   server_name share.huhao.com;
   root  /code/share/;
   autoindex on;
   autoindex_exact_size off;
   autoindex_format json;
 }

#2.配置limit_req
##limit_req_zone
[root@web01 /etc/nginx]#vim nginx.conf 

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

   limit_req_zone $binary_remote_addr  zone=one:10m rate=1r/s;
    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

   include /etc/nginx/conf.d/*.conf;
"nginx.conf" 33L, 715C written   
 
 
[root@web01 /etc/nginx/conf.d]#vim share.huhao.com.conf 
server {
   listen 80;
   server_name share.huhao.com;
   root  /code/share/;
   autoindex on;
   limit_req zone=one;
   autoindex_exact_size off;
   autoindex_format json;
 }
"share.huhao.com.conf" 9L, 176C written

[root@web01 /etc/nginx/conf.d]#nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: [emerg] zero size shared memory zone "one"
nginx: configuration file /etc/nginx/nginx.conf test failed
[root@web01 /etc/nginx/conf.d]#nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

[root@web01 /etc/nginx/conf.d]#systemctl  reload nginx 


#命令行 模拟访问
[root@admin ~]# ab -n 99 -c 6 http://share.huhao.com/


 limit_req zone=one burst=5 nodelay;




浏览器输出显示结果


模拟访问的同时,查看访问日志和错误日志



反复操作点击 报错503

limit_conn 连接数限制模块

ngx\u http\u limit_conn模块用于限制每个已定义密钥的连接数,特别是来自单个IP地址的连接数。



并非所有连接都被计算在内。只有当一个连接有一个正在由服务器处理的请求并且整个请求头已经被读取时,它才会被计数。

# 1.环境准备与配置
[root@web01 /etc/nginx]#grep -C5  limit_conn_zone  nginx.conf 
   log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                     '$status $body_bytes_sent "$http_referer" '
                     '"$http_user_agent" "$http_x_forwarded_for"';
   
  #limit_req_zone $binary_remote_addr  zone=one:10m rate=1r/s;
   limit_conn_zone $binary_remote_addr zone=addr:10m;
  access_log  /var/log/nginx/access.log  main;

   sendfile        on;
   #tcp_nopush     on;


# 2.配置限制策略
[root@web01 /etc/nginx/conf.d]#vim share.huhao.com.conf 
server {
  listen 80;
  server_name share.huhao.com;
  root  /code/share/;
  autoindex on;
  #limit_req zone=one burst=5;
  limit_conn addr  1;
  #allow 10.0.0.7;
  autoindex_exact_size off;
  autoindex_format html;
}                                                                                                                    
"share.huhao.com.conf" 11L, 228C written                                                            
[root@web01 /etc/nginx/conf.d]#nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 /etc/nginx/conf.d]#systemctl  reload nginx


#其他服务器
[root@backup ~]# ab- n 20 -c 2 -H "Host: share.huhao.com" http://10.0.0.7/index/


limit_rate 速率限制

  • limit_rate 限速
  • limit_rate_after 下载多少文件后再进行限速
1.环境准备
dd if=/dev/zero of=/code/share/vsecret.avi bs=1M count=500



2.配置文件
[root@web01 /etc/nginx/conf.d]#vim share.huhao.com.conf 
server {
   listen 80;
   server_name share.huhao.com;
   root  /code/share/;
   autoindex on;
   #limit_req zone=one burst=5;
   #limit_conn addr  1;
   #allow 10.0.0.7;
   limit_rate 200k; #限速200k
   autoindex_exact_size off;
   autoindex_format html;
 }

[root@web01 /etc/nginx/conf.d]#nginx -t

[root@web01 /etc/nginx/conf.d]#systemctl  reload nginx.service 






各种限制模块

  • limit_req 模块 限制请求(http)
  • limit_conn 模块 限制连接(tcp)
  • limit_rate core模块 限速速度
  • 请求的限制

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值