Nginx模块

1、Nginx WEB模块

连接状态stub_status_module

目的:展示用户和nginx链接数量信息。

1 查询模块是否安装:

nginx -V 2>&1 | grep stub_status 

1 访问默认站点的状态模块(注意:未启用,错误验证)

http://10.18.41.64/nginx_status

2 配置状态模块

 vim /etc/nginx/conf.d/default.conf

server {

location /nginx_status {	
stub_status;
allow all;
    }
}

 3 重启服务再次访问

systemctl restart nginx

观察连接数 和请求数。

Active connections: 22    当前活动的连接数

server accepts handled requests    服务器接受处理请求 

17 17 24

17        总连接数connection(TCP)

17        成功的连接数connection (TCP)

24        总共处理的请求数requests(HTTP)

Reading: 2      读取客户端Header的信息数         请求头

Writing: 1        返回给客户端的header的信息数     响应头

Waiting: 19        等待的请求数,开启了keepalive

keepalived        长连接设置

Httpd守护进程,一般都提供了keep-alive timeout时间设置参数。比如nginx的keepalive_timeout,和Apache的KeepAliveTimeout。这个 keepalive_timout时间值意味着:一个http产生的tcp连接在传送完最后一个响应后,还需要hold住 keepalive_timeout秒后,才开始关闭这个连接。当httpd守护进程发送完一个响应后,理应马上主动关闭相应的tcp连接,设置 keepalive_timeout后,httpd守护进程会想说:”再等等吧,看看浏览器还有没有请求过来”,这一等,便是 keepalive_timeout时间。如果守护进程在这个等待的时间里,一直没有收到浏览发过来http请求,则关闭这个http连接。

随机主页random_index_module

随机主页属于微更新,非必要操作。将主页设置成随机页面,是一种微调更新机制

1 创建主页目录

mkdir /app    #目录随意

2 创建多个主页

touch /app/{blue.html,green.html,red.html,.yellow.html}

在不同的页面书写不同的内容,例如

<html>
<head>
<title>green color</title>
</head>
<body style="background-color:green">
<h1>green color!</h1>
</body>
</html>

更换颜色把此处的单词换成其他颜色即可

替换模块sub_module

目的:网页内容替换,如果我们用模板生成网站的时候,因为疏漏或者别的原因造成代码不如意,但是此时因为文件数量巨大,不方便全部重新生成,那么这个时候我们就可以用此模块来暂时实现纠错。另一方面,我们也可以利用这个实现服务器端文字过滤的效果。

vim /etc/nginx/conf.d/default.conf

  server {

sub_filter 9 'CESHI;
sub_filter_once on;
location / {
root   /usr/share/nginx/html;
index  index.html index.htm;
       }

}

 重启服务,测试页面

1.只替换了一处。

2.将单次替换关闭,再次刷新页面,即可看见全文替换。sub_filter_once off

文件压缩ngx_http_gzip_module

启动该模块,使文件传输前进行压缩,提升传输效率。

1 观察未压缩传输

拷贝图片至网站主目录、拷贝tar包至网站主目录,压缩包的后缀使用.html、拷贝文本至文件主目录

2 启用压缩功能

vim /etc/nginx/conf.d/default.conf

http {

gzip on;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png; 
gzip_static on;

}

systemctl restart nginx

注意缓存,压缩包和图片类对象本身已经自带压缩功能。所以压缩比例较小低。
文本类对象在压缩试验中,压缩比例体现优越。再通过浏览器下载文件并观察下载后大小。

页面缓存ngx_http_headers_module

expires起到控制页面缓存的作用,合理的配置expires可以减少很多服务器的请求要配置expires,可以在http段中或者server段中或者location段中加入。
Nginx(expires 缓存减轻服务端压力),

原理:无缓存,每次访问服务器,均是全文传输。开启缓存可以加速浏览网站。

vim /etc/nginx/conf.d/default.conf

    location / {
        root   /usr/share/nginx/html
        index  index.html index.htm;
        expires 24h;
    }

浏览页面,观察响应头中出现服务器回复的缓存时间(单位秒)

防盗链ngx_http_referer_module

1 搭建一个a.com网站        在主页中插入图片

vim index.html


<img src='1.jpg' />        #注意要将1.jpg图片拷贝至网站主目录。

2 搭建一个b.com网站

在主页中盗链A网站的图片

vim index.html


<img src='http://A网站的域名或者地址/1.jpg' />    #注意网站主目录中,根本没有图片。

3 访问两个网站页面。均能正常显示图片。

4 注意b.com网站的日志

192.168.100.254 - - [08/Jan/2024:20:07:03 +0800] "GET / HTTP/1.1" 200 42 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:57.0) Gecko/20100101 Firefox/57.0" "-"

5 注意a.com网站的日志,日志不正常。日志莫名其妙的产生了。观察referer字段,发现被盗链了。

192.168.100.254 - - [08/Jan/2024:20:07:03 +0800] "GET /1.jpg HTTP/1.1" 200 1635350 "http://192.168.100.20/" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:57.0) Gecko/20100101 Firefox/57.0"

6  启动a.com防盗链功能

location / {
        root   /a.com;
        index  index.html index.htm;
     
        valid_referers none blocked *.a.com;
        if ($invalid_referer) {
            return 403;
        }
    }

重启服务

7 再次访问b.com网站,盗链失败。

8 如果希望某些网站能够使用(盗链)资源:


location ~* \.(gif|jpg|png|bmp)$ {
root /a.com
    valid_referers none blocked  *.icloud.top server_names ~tianyun ~\.google\. ~\.baidu\.;
    if ($invalid_referer) {
        return 403;
        #rewrite .* http://qfcloud.top/403.jpg;
    }
}

location / {
        root   /a.com;
        index  index.html index.htm;
     
        valid_referers none blocked *.a.com        server_name  192.168.100.*  ~tianyun ~\.google\. ~\.baidu\.   b.com;
        if ($invalid_referer) {
            return 403;
        }
    }

9 再次盗链,合法盗链成功。

2、Nginx 访问限制

启动请求频率限制ngx_http_limit_req_module

0 测试未限制情况下的访问

yum install -y httpd-tools
ab -n 100 -c 10 http://tianyun.me/

 1 启动限制

vim /etc/nginx/nginx.conf

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

限制请求  二进制地址  限制策略的名称   占用10M空间  允许每秒1次请求

 limit_req zone=req_zone;  子配置文件引用

http {
    limit_req_zone $binary_remote_addr zone=req_zone:10m rate=1r/s;     定义

    server {
        location / {
            root /usr/share/nginx/html;
            index index.html index.htm;
            limit_req zone=req_zone;  		引用
            #limit_req zone=req_zone burst=5;
            #limit_req zone=req_zone burst=5 nodelay; 
        }
    }
}

子配置文件引用

ab -n 100 -c 10 http://tianyun.me/

This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Benchmarking localhost (be patient).....done


Server Software:          nginx/1.12.1
Server Hostname:        tianyun.me
Server Port:                80

Document Path:          /
Document Length:       671 bytes

Concurrency Level:      10
Time taken for tests:    0.006 seconds
Complete requests:      100                 
Failed requests:            99                      失败的请求
   (Connect: 0, Receive: 0, Length: 99, Exceptions: 0)
Write errors:                0
Non-2xx responses:      99                      有问题的相应。
Total transferred:         73273 bytes
HTML transferred:       53834 bytes
Requests per second:  16131.63 [#/sec] (mean)
Time per request:       0.620 [ms] (mean)
Time per request:       0.062 [ms] (mean, across all concurrent requests)
Transfer rate:             11543.10 [Kbytes/sec] received

tail -f /var/log/nginx/error.log

2024/01/08 20:24:08 [error] 23287#23287: *720 limiting requests, excess: 5.109 by zone "req_zone", 
client: 27.216.240.201, server: localhost, request: "GET / HTTP/1.0", host: "tianyun.me"

 limiting requests:由于限制请求导致。

启动连接频率限制ngx_http_limit_conn_module 

通过IP地址,限制链接(TCP)。

vim /etc/nginx/nginx.conf

http {
    limit_conn_zone $binary_remote_addr zone=conn_zone:10m;
}
server {
    location / {
        ...
        limit_conn conn_zone 1;
    }
}  

单个IP,同时只允许有一个tcp连接

2 测试

yum install -y httpd-tools

ab -n 100 -c 10 http://服务器IP地址/

This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Benchmarking localhost (be patient).....done

Server Software:         nginx/1.12.1    
Server Hostname:        tianyun.me
Server Port:                80

Document Path:           /
Document Length:        671 bytes       文档长度

Concurrency Level:      10         当前并发数
Time taken for tests:    0.006 seconds     消耗总时间
Complete requests:      100         完成请求数
Failed requests:           0         失败请求数
Write errors:               0
Total transferred:        90400 bytes   总的传输大小
HTML transferred:       67100 bytes         http传输大小
Requests per second:  15873.02 [#/sec] (mean)   每秒钟处理多少个请求。

3、Nginx访问控制

基于主机IPngx_http_access_module

1 限制主机访问

vim /etc/nginx/conf.d/default.conf


 server {
  allow 10.18.45.65;
  allow 10.18.45.181;
  deny all;
}

2 测试

服务器无法访问

基于用户ngx_http_auth_basic_module

1. 建立认证文件

yum install -y httpd-tools
htpasswd -cm /etc/nginx/conf.d/passwd user10    #会话设置密码
htpasswd -m /etc/nginx/conf.d/passwd user20    #会话设置密码

观察口令文件是否生成。已生成

cat /etc/nginx/conf.d/passwd

user10:$apr1$UE/tLtDM$nVm686kAMYb/ArqQDUi8U/
user20:$apr1$bmn0E/gK$enkXKb2V5uFvUy9wdIHlP.

vim /etc/nginx/conf.d/default.conf

    server {

            auth_basic "nginx access test!";
            auth_basic_user_file /etc/nginx/conf.d/passwd;

...
}

#提示消息引用认证文件

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值