nginx常用模块

一、目录索引

1)应用场景

可以使用nginx作为简易的文件下载服务器!

2)参数说明

Syntax: autoindex on | off;
Default: autoindex off;
Context: http, server, location

# autoindex 常用参数
autoindex_exact_size off;
默认为 on, 显示出文件的确切大小,单位是 bytes。
修改为 off,显示出文件的大概大小,单位是 kB 或者 MB 或者 GB。

autoindex_localtime on;
默认为 off,显示的文件时间为 GMT 时间。
修改为 on, 显示的文件时间为文件的服务器时间。

charset utf-8,gbk;
默认中文目录乱码,添加上解决乱码

3)配置文件

[root@nginx ~]# vim /etc/nginx/conf.d/download.conf
server {
    listen 80;
    server_name download.lzj.com;
    location / {
        root /usr/share/nginx/html/download;
        charset utf-8,gbk;
        autoindex on;
        autoindex_localtime on;
        autoindex_exact_size off;
    }
}
[root@nginx ~]# nginx -t
[root@nginx ~]# systemctl restart nginx

4)创建测试数据

[root@nginx ~]# mkdir /usr/share/nginx/html/download
[root@nginx ~]# touch /usr/share/nginx/html/download/{1..10}.txt

5)测试

20200330100013

二、状态监控

1)状态字段解释

Active connections # 当前活动的连接数
accepts # 当前的总连接数 TCP
handled # 成功的连接数 TCP
requests # 总的 http 请求数
Reading # 请求
Writing # 响应
Waiting # 等待的请求数,开启了 keepalive
# 注意, 一次 TCP 的连接,可以发起多次 http 的请求, 如下参数可配置进行验证
keepalive_timeout 0; # 类似于关闭长连接
keepalive_timeout 65; # 65s 没有活动则断开连接

2)配置文件

[root@nginx ~]# vim /etc/nginx/conf.d/status.conf 
server {
   listen 80;
   server_name  status.lzj.com;
   stub_status on;
   access_log off;
}

3)访问测试

[root@nginx ~]# curl status.lzj.com
Active connections: 1 
server accepts handled requests
 4 4 6 
Reading: 0 Writing: 1 Waiting: 0 

三、访问控制

1)基于IP的访问控制

① 配置语法
#允许配置语法
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
② 配置案例1:拒绝特定主机访问
[root@nginx ~]# vim /etc/nginx/conf.d/01-www.conf 
server   {
    listen       80;
    server_name  www.lzj.com;
    location / {
        root   /usr/share/nginx/html/www;
        index  index.html index.htm;
        deny 192.168.1.253;
        allow all;
    }
}
③ 192.168.1.253主机测试

20200330101034

④ nginx本机测试
[root@nginx ~]# curl www.lzj.com
www
⑤ 配置案例2:只允许特定的主机访问
[root@nginx ~]# vim /etc/nginx/conf.d/01-www.conf 
server   {
    listen       80;
    server_name  www.lzj.com;
    location / {
        root   /usr/share/nginx/html/www;
        index  index.html index.htm;
        allow 192.168.1.253;
        deny all;
    }
}
⑥ 192.168.1.253主机访问

20200330101413

⑦ nginx本机访问
[root@nginx ~]# curl www.lzj.com
<html>
<head><title>403 Forbidden</title></head>
<body>
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.16.1</center>
</body>
</html>

2)基于用户认证的访问控制

① 配置语法
#访问提示字符串
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
② 配置文件
#1.需要安装 httpd-tools,该包中携带了 htpasswd 命令
[root@nginx ~]# yum install httpd-tools -y
#2.创建新的密码文件, -c 创建新文件 -b 允许命令行输入密码
[root@nginx ~]# htpasswd -b -c /etc/nginx/auth_conf lzj 123456
#用户名lzj,密码123456
Adding password for user lzj
#3.nginx 配置调用
[root@nginx ~]# cat /etc/nginx/conf.d/01-server   {
    listen       80;
    server_name  www.lzj.com;
    location / {
        auth_basic "Auth access Blog Input your Passwd!";
        auth_basic_user_file auth_conf;
        root   /usr/share/nginx/html/www;
        index  index.html index.htm;
    }
}
⑤ 访问测试

20200330102052

四、访问限制

经常会遇到这种情况,服务器流量异常,负载过大等等。对于大流量恶意的攻击访问, 会带来带宽的浪费,服务器压力,影响业务,往往考虑对同一个 ip 的连接数,请求数、进行限制。
ngx_http_limit_conn_module 模块可以根据定义的 key 来限制每个键值的连接数,如同一个 IP 来源的连接数。

  • limit_conn_module 连接频率限制
  • limit_req_module 请求频率限制

1)请求限制

① 配置语法
#模块名 ngx_http_limit_req_module
Syntax: limit_req_zone key zone=name:size rate=rate;
Default: —
Context: http
Syntax: limit_conn zone number [burst=number] [nodelay];
Default: —
Context: http, server, location
② 配置文件
http {
      limit_req_zone $binary_remote_addr zone=req_zone:10m rate=1r/s;
	  #http段配置请求限制,  rate限制速率,限制⼀秒钟最多⼀个IP请求
}
[root@nginx ~]# cat /etc/nginx/conf.d/01-www.conf 
server   {
    listen       80;
    server_name  www.lzj.com;
    limit_req zone=req_zone burst=3 nodelay;
	 #请求超过1r/s,剩下的将被延迟处理,请求数超过burst定义的数量,多余的请求返回503
    access_log  /var/log/nginx/www.access.log  main;
    location / {
        root   /usr/share/nginx/html/www;
        index  index.html index.htm;
    }
}
③ 访问测试
[root@nginx ~]# yum install httpd-tools -y
[root@nginx ~]# ab -n 20 -c 2 http://www.lzj.com/

④ 为什么限制请求的效果更好

http 协议的连接与请求,首先 HTTP 是建立在 TCP 基础之上, 在完成 HTTP 请求需要先建立TCP 三次握手(称为 TCP 连接) ,在连接的基础上在完成 HTTP 的请求。

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

五、location

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

1)语法介绍

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

2)location语法优先级

20200330104222

3)配置location匹配规则实战

[root@nginx ~]# cat /etc/nginx/conf.d/01-www.conf 
server {
    listen       80;
    server_name  www.lzj.com;
    root   /usr/share/nginx/html/www;
    location / {
       return 200  "location / \n";
    }
    location = / {
        return 200 "location = \n";
    }

    location /documents/ {
        return 200 "location /documents/ \n";
    }
    location ^~ /images/ {
        return 200 "location ^~ /images/ \n";

    }
    location ~* \.(gif|jpg|jpeg)$ {
        return 200 "location ~* \.(gif|jpg|jpeg) \n";
    }
    access_log off;
}

4)测试location匹配规则

#精确匹配=/
[root@nginx ~]# curl www.lzj.com
location = 
#没有满足的请求,所以匹配了/
[root@nginx ~]# curl www.lzj.com/lzj.html
location / 
#匹配了/documents
[root@nginx ~]# curl www.lzj.com/documents/oldboy.html
location /documents/ 
#没有满足的条件,匹配/
[root@nginx ~]# curl www.lzj.com/oldboy/documents/oldboy.html
location / 
#正则匹配了文件名
[root@nginx ~]# curl www.lzj.com/oldboy.jpg
location ~* \.(gif|jpg|jpeg) 
#~*匹配正则不区分大小写优先于/documents
[root@nginx ~]# curl www.lzj.com/documents/oldboy.jpg
location ~* \.(gif|jpg|jpeg) 
#^~优先匹配于~*
[root@nginx ~]# curl www.lzj.com/images/oldboy.jpg   
location ^~ /images/ 
posted @ 2020-11-22 23:11  吕振江  阅读(178)  评论(0编辑  收藏  举报
浏览器标题切换
浏览器标题切换end