8.Nginx常用基础模块

1. Nginx目录索引

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

1.1 指令

#启用或禁用目录列表输出,on开启,off关闭;
句法:Syntax:  autoindex on | off;
默认:Default: autoindex off;
语境:Context: http, server, location

#指定是否应在目录列表中输出确切的文件大小,on显示字节,off显示大概单位。
句法: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

1.2 示例配置

#1.配置nginx
[root@web01 ~]# cat /etc/nginx/conf.d/module.conf 
server {
        listen 80;
        server_name game.oldboy.com;
        charset 'utf-8';

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

        location /module {
                alias /module;
                autoindex on;
                autoindex_exact_size off;
                autoindex_localtime on;
        }
}

#2.准备对应的目录,往目录中上传一点文件
[root@web01 ~]# mkdir /module/{centos,ubuntu,redhat}/ -p
[root@web01 ~]# ll /module/
total 36220
-rw-r--r-- 1 root root 37087372 Jan 17  2019 09_上海Linux3期-Nginx常用模块-总结.mp4
-rw-r--r-- 1 root root        0 Aug  3 18:05 111.txt
drwxr-xr-x 2 root root        6 Aug  2 18:17 centos
drwxr-xr-x 2 root root       21 Aug  3 18:16 module
drwxr-xr-x 2 root root        6 Aug  2 18:17 redhat
drwxr-xr-x 2 root root        6 Aug  2 18:17 ubuntu

#3.检查语法并重新加载Nginx
[root@web01 ~]# 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 ~]# nginx -s reload

2. Nginx状态监控

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

2.1 指令

句法:Syntax:  stub_status;
默认:-
语境:Context: server, location

2.2 示例配置

server {
    listen 80;
    server_name game.oldboy.com;
    charset 'utf-8';

    location /nginx_status {
            stub_status;
    }
}

2.3 此配置创建一个简单的网页,其基本状态数据如下所示:

-c

2.4 提供以下状态信息

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没有活动则断开连接

3. Nginx访问控制

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

3.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

3.2 示例配置,拒绝指定ip,允许其他ip访问

[root@web01 conf.d]# cat module.conf 
server {
    listen 80;
    server_name game.oldboy.com;
    charset 'utf-8';

    location /nginx_status {
            stub_status;
            deny 10.0.0.1/32;   #拒绝指定的地址
            allow all;          #允许所有地址
    }
}

3.3 示例配置,允许指定ip,拒绝其他

[root@web01 conf.d]# cat module.conf 
server {
    listen 80;
    server_name game.oldboy.com;
    charset 'utf-8';

    location /nginx_status {
            stub_status;
            allow 127.0.0.1;
            allow 10.0.0.1/32;  #允许访问的地址段
            deny all;           #拒绝所有地址
    }
}

4. Nginx资源限制

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

4.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

4.2 实例

#基于用户密码的身份验证
1.生成一个密码文件,密码文件的格式  name:password(加密)  (建议使用htpasswd)  openssl password
[root@web01 conf.d]# yum install httpd-tools -y
[root@web01 conf.d]# htpasswd -c -b /etc/nginx/auth_conf lhd haoda
[root@web01 conf.d]# cat /etc/nginx/auth_conf
lhd:$apr1$rgTagvSK$kOTFXm.5DjUQLt8OQ6P741

2.配置Nginx,限制对应的资源
[root@web01 conf.d]# cat module.conf 
server {
        listen 80;
        server_name game.oldboy.com;
        charset 'utf-8';

        location /module {
                alias /module;
                autoindex on;
                autoindex_exact_size off;
                autoindex_localtime on;
                auth_basic "Please Password!!!!";
                auth_basic_user_file /etc/nginx/auth_conf;
        }

}

5. Nginx访问限制

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

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

5.1 指令

#设置共享内存区域和给定键值的最大允许连接数
句法:Syntax:  limit_conn zone number;
默认:Default: -
语境:Context: http,server,location

#设置要响应拒绝的请求而返回的状态代码。
句法:Syntax:  limit_conn_zone key zone=name:size;
默认:Default: -
语境:Context: http

5.2 示例

#设置共享内存区域和给定键值的最大允许连接数。超过此限制时,服务器将返回 错误 以回复请求
[root@web01 conf.d]# cat module.conf
limit_conn_zone $binary_remote_addr zone=conn_zone:10m;
server {
    limit_conn conn_zone 1;    # 同一时刻只允许一个客户端连接
    location / {
            root /code;
            index index.html;
}

5.3 ngx_http_limit_req_module模块指令

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

句法:Syntax:  limit_req_zone key zone=name:size rate=rate [sync];
默认:Default: -
语境:Context: http

句法:Syntax:  limit_req zone=name [burst=number] [nodelay | delay=number];
默认:Default: -
语境:Context: http,server,location

5.4 ngx_http_limit_req_module模块实例

#定义限制的key
[root@web01 conf.d]# cat module.conf
limit_req_zone $binary_remote_addr zone=req_zone:10m rate=1r/s;
server {
        listen 80;
        server_name game.oldboy.com;
        charset 'utf-8';
        limit_req zone=req_zone burst=5 nodelay;    #支持每秒访问一次,可延迟五条访问,剩余全部拒绝;
        limit_req_status 412;                       #错误页面返回412
        error_page 412 /err.html;                   #错误页面指定
        location /module {
                alias /module;
                auth_conf;
        }
}

#填写hosts域名解析
[root@web01 conf.d]# echo "10.0.0.7 game.oldboy.com" >> /etc/hosts

#进行简单的压力测试
[root@web01 conf.d]# ab -n 50 -c 20  http://game.oldboy.com/index.html

#查看错误日志
[root@web01 ~]# tail -f /var/log/nginx/error.log
2019/08/13 23:43:16 [error] 7248#7248: *1730 limiting requests, excess: 5.408 by zone "req_zone", client: 10.0.0.1, server: game.oldboy.com, request: "GET /module/ HTTP/1.1", host: "game.oldboy.com"
2019/08/13 23:43:16 [error] 7248#7248: *1730 limiting requests, excess: 5.245 by zone "req_zone", client: 10.0.0.1, server: game.oldboy.com, request: "GET /module/ HTTP/1.1", host: "game.oldboy.com"

nginx连接限制没有请求限制有效?

首先HTTP是建立在TCP基础之上,完成HTTP请求需要先建立TCP三次握手(成为TCP连接),在连接的基础上再完成HTTP的请求;

所以多个HTTP请求可以建立在一次TCP连接之上,那么我们对请求的精度限制,当然比对一个连接的限制会更加的有效,因为同一时刻只允许一个TCP连接进入,但是同一时刻多个HTTP请求可以通过一个TCP连接进入,所以针对HTTP的请求限制才是比较优秀的解决方案。


6. Nginx Location

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

6.1 location语法示例

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

6.2 location语法优先级排列

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

6.3 配置网站验证location优先级

[root@web01 conf.d]# cat testlocal.conf 
server {
        listen 80;
        server_name game.oldboy.com;
        location / {
                default_type text/html;
                return 200 "location /";
        }

        location =/ {
                default_type text/html;
                return 200 "location =/";
        }

        location ~ / {
                default_type text/html;
                return 200 "location ~/";
        }

        #location ^~ / {
        #        default_type text/html;
        #        return 200 "location ^~";
        #}

}


# 通用匹配,任何请求都会匹配到
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)$" {
     ...
}

猜你喜欢

转载自www.cnblogs.com/chenmiao531759321/p/11436075.html