第二节:小白学nginx之nginx的模块

小白学nginx之nginx的模块

nginx中有一些比较常见的模块,我们可以列出来一些常用的

1.目录索引

autoindex:列出目录索引


[root@web01 module]# cat /etc/nginx/conf.d/autoindex.conf
server {
listen 80;
server_name module..com;
charset utf-8,gbk;
location / {
    root /module;
    autoindex on;
    autoindex_exact_size off;
    autoindex_localtime on;
}

}


2.准备对应的目录,往目录中上传一点文件
```bash
mkdir /module/{centos,ubuntu,redhat}/ -p

3.检查语法并重新加载Nginx

nginx -t 
systemctl restart nginx
nginx状态页面
location /nginx_status {
        stub_status;
}

最终展示的结果:

Active connections: 2 
server accepts handled requests
        3           3   33 
Reading: 0 Writing: 1 Waiting: 1 

Active connections  # 当前活动客户端连接数,包括Waiting等待连接数。
accepts             # 已接受总的TCP连接数。
handled             # 已处理总的TCP连接数。
requests            # 客户端总的http请求数。

Reading             # 当前nginx读取请求头的连接数。
Writing             # 当前nginx将响应写回客户端的连接数。
Waiting             # 当前等待请求的空闲客户端连接数。

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

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

基于来源的IP地址做限制

1.拒绝10.0.0.1来源IP访问,其他人允许。

location /nginx_status {
      stub_status;
      deny 10.0.0.1/32;
      allow all;
}

2.允许10.0.0.1来源IP访问,其他人全部拒绝。

location /nginx_status {
      stub_status;
      allow 10.0.0.1/32;
      deny all;
}

3.实际配置监控Nginx状态时,仅允许该服务器的回环地址访问127.0.0.1

location /nginx_status {
     stub_status;
     allow 127.0.0.1;
     deny all;
}
基于用户密码的身份验证

1.生成一个密码文件,密码文件的格式 name:password(加密) (建议使用htpasswd)

[root@web01 conf.d]# yum install httpd-tools -y
[root@web01 conf.d]# htpasswd -c -b /etc/nginx/auth_conf oldboy oldboy
[root@web01 conf.d]# cat /etc/nginx/auth_conf
oldboy:$apr1$Kp87VSae$658Nt5bm4iiblQkUvP7u61

2.配置Nginx,限制对应的资源

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

    auth_basic "Please Password!!!";
    auth_basic_user_file /etc/nginx/auth_conf;
}
nginx的连接限制

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

http标签段定义连接限制

http{
    limit_conn_zone $binary_remote_addr zone=conn_zone:10m;
}
server {
    # 同一时刻只允许一个客户端连接
    limit_conn conn_zone 1; 

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

使用ab工具进行压力测试

[root@localhost ~]# yum install -y httpd-tools
[root@localhost ~]# ab -n 500 -c 2  http://127.0.0.1/index.html

2019/01/14 11:11:22 [error] 29962#29962: *19 limiting connections by zone "conn_zone", client: 47.110.176.164, server: www.localhost.com, request: "GET / HTTP/1.1", host: "www.localhost.com"
2019/01/14 11:11:23 [error] 29962#29962: *19 limiting connections by zone "conn_zone", client: 47.110.176.164, server: www.localhost.com, request: "GET / HTTP/1.1", host: "www.localhost.com"
2019/01/14 11:11:25 [error] 29962#29962: *19 limiting connections by zone "conn_zone", client: 47.110.176.164, server: www.localhost.com, request: "GET / HTTP/1.1", host: "www.localhost.com"
2019/01/14 11:11:25 [error] 29962#29962: *19 limiting connections by zone "conn_zone", client: 47.110.176.164, server: www.localhost.com, request: "GET / HTTP/1.1", host: "www.localhost.com"

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

1.定义限制的key(基于什么来做限制,IP地址)

[root@web01 conf.d]# cat test1.oldboy.com.conf 
limit_req_zone $binary_remote_addr zone=req_zone:10m rate=1r/s;
server {
    listen 80;
    server_name test1.oldboy.com;

    limit_req zone=req_zone burst=5 nodelay;
    limit_req_status 412;
    error_page 412 /err.html;    #这个文件必须存在/code/test1/err.html

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

2.填写hosts域名解析

echo "10.0.0.7 test1.oldboy.com" >> /etc/hosts

3.进行简单的压力测试

 ab -n 50 -c 20 http://test1.oldboy.com/index.html
nginx location

1.Location语法示例

location [=|^~|~|~*|!~|!~*|/] /uri/ { ...
}

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

通用匹配,任何请求都会匹配到

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)$" {
    ...
}
上一篇
下一篇
Copyright © 2022 Egon的技术星球 egonlin.com 版权所有 帮助IT小伙伴学到真正的技术