目录

autoindex自动索引模块

 status状态监控模块

auth基于用户的访问控制模块

 limit_conn_module连接请求频率限制模块

log_format日志访问模块

日志格式变量参数含义:


首先了解个概念nginx配置文件中一个http字段下可以有多个server字段,一个server字段下可以有多个location字段

autoindex自动索引模块

nginx默认不起用目录索引,更不允许列出网站目录提供下载。

Syntax:    autoindex on | off;                索引功能的开或关
Default:    autoindex off;                默认关闭
Context:    http, server, location        场景:全局、某个虚拟主机、某个虚拟主机的目录 

写法与实现:

首先创一个下载目录 并在目录下写一个index.html的文件

mkdir -p /www/download && echo "<h1><marquee>www.test.com</marquee></h1>" > /www/index.html

在这个目录下随便创建些文件:

nginx autoindex相关配置 nginx autoindex on_运维

 然后在sever字段下添加个location字段:

vi /etc/nginx/conf.d/www.conf
server {
        listen 80;
        server_name www.gxd.com;

        location /{
                root /www; #这个文件需要自己创建
                index index.html index.htm;
        }
        location /download{

                root /www; #这里注意:就写存放的一级目录即可 他会自动去www目录下找download
                autoindex on;  #开启索引目录
                charset utf8,gbk;  #设置字符编码
                autoindex_exact_size on;  #显示文件可下载大小
                autoindex_localtime on;  #下载时显示文件创建时间


        }
}

写完以后检查:

nginx -t

nginx autoindex相关配置 nginx autoindex on_服务器_02

 然后重启:

systemctl restart nginx

接下来访问的时候在ip地址后面加上/download  不加就会访问刚才写的index.html文件

nginx autoindex相关配置 nginx autoindex on_重启_03

 status状态监控模块

 stub_status;                                       启用状态化追踪
 access_log off  |  on;                         关闭或开启status的日志记录

allow 192.168.1.0/24;                          仅允许1.0网段访问
deny all;                                               拒绝其他所有

写法与实现:

在刚才我们写的扩展配置中加入

vi /etc/nginx/conf.d/www.conf
location /status{
                stub_status;   #启动状态划追踪
                access_log off; #状态化追踪日志记录关闭
}

nginx autoindex相关配置 nginx autoindex on_服务器_04

 重启nginx或者重载

 重启nginx

systemctl restart nginx

重载nginx 

systemctl reload nginx

重启完成后我们访问一下网页 ip地址+/status 访问

nginx autoindex相关配置 nginx autoindex on_nginx autoindex相关配置_05

可以看状态信息:


Active connections: #当前活跃的连接数 server accepts handled requests #服务器接收已处理的请求 下面几个 “1” 分别代表 从左往右 server  accepts  当前的总tcp连接数   handled  成功的连接数  requests 总HTTP请求数 Reading: 0 Writing: 1 Waiting: 0 # 读Reading 写Writing 等待Waiting


这样做有个缺陷就是谁都可以访问我们的状态化信息,如果想做个限制不让别人看到 那么我们需要在加入两条配置

还是在刚才的配置文件写入

vi /etc/nginx/conf.d/www.conf
location /status{
                stub_status;   #启动状态划追踪
                access_log off; #状态化追踪日志记录关闭
                allow 172.16.1.0/24;  #允许1网段的访问
                deny all;             #拒绝除1网段以外都拒绝
}

 写完以后要习惯检查一下 是否有问题:

nginx -t

nginx autoindex相关配置 nginx autoindex on_nginx autoindex相关配置_06

 重启ngin

systemctl restart nginx

在访问查看 

auth基于用户的访问控制模块

 首先通过yum下载一个名字为httpd-tools是给Nginx设置访问密码增加安全性

yum -y install httpd-tools

安装 完成后设置账号(admin)密码(123456)

htpasswd -b -c /etc/nginx/.auth_conf admin 123456

nginx autoindex相关配置 nginx autoindex on_nginx autoindex相关配置_07

 接下来在刚才的写的配置文件中加入 (针对那个文件或者目录做信息验证就写在那个location字段内)

auth_basic      "input your password:"; #开启用户验证启用描述
 auth_basic_user_file /etc/nginx/.auth_conf;  #用户验证文件路径

nginx autoindex相关配置 nginx autoindex on_运维_08

 再次使用nginx -t检查后重启nginx

nginx autoindex相关配置 nginx autoindex on_nginx_09

 我们再次进入网页查看是否需要登录验证(ip后面加上/status)

nginx autoindex相关配置 nginx autoindex on_重启_10

 limit_conn_module连接请求频率限制模块

 limit_conn_zone $binary_remote_addr zone=addr:10m;  创建个缓存区域,用来记录客户端的二进制ip地址,区域名字为addr,大小为10m  大小和名字可随意更改

写法  在http字段内写入 (这条要长期使用的话加入到全局配置文件/etc/nginx/nginx.conf内)

vi /etc/nginx/nginx.conf

nginx autoindex相关配置 nginx autoindex on_nginx autoindex相关配置_11

 写好后在扩展配置文件中加入:

vi /etc/nginx/conf.d/www.conf
limit_conn addr 1; #一个ip同一时间点只允许建立一个连接  addr是刚创建的缓存
区名字

 

nginx autoindex相关配置 nginx autoindex on_服务器_12

 

检查 

nginx -t

nginx autoindex相关配置 nginx autoindex on_nginx autoindex相关配置_13

  重启:

systemctl restart nginx

以上是做单个ip的限制,好处防止一个ip地址伪造多个客户端对网站进行攻击

下面我们做一个连接速率的限制

limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s; #创建缓存区域名字为 one 缓存大小10m 设置每秒连接频率

在全局配置文件/etc/nginx/nginx.conf 内http字段加入

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

nginx autoindex相关配置 nginx autoindex on_nginx_14

 在扩展配置文件内加入一行

vi /etc/nginx/conf.d/www.conf

 # limit_req zone=one burst=5 #one是刚才创建的缓存区名字 burst表示突发量 + 每秒的一次访问 = 最大同一个ip每秒访问次数 

limit_req zone=one burst=5;

nginx autoindex相关配置 nginx autoindex on_nginx autoindex相关配置_15

 写完后习惯性的验证:

nginx -t

nginx autoindex相关配置 nginx autoindex on_nginx autoindex相关配置_16

 重启nginx生效

systemctl restart nginx

然后访问网页

谷歌浏览器摁一下F12   看一下network        requests 请求次数

nginx autoindex相关配置 nginx autoindex on_运维_17

log_format日志访问模块

日志存放日志:/var/log/nginx 

日志格式变量参数含义:

#nginx日志格式的变量:
    $remote_addr            #记录客户端的ip地址
    $remote_user            #记录客户端的用户名
    $time_local            #通用的时间格式
    $time_iso8601            #iso8601时间格式
    $request            #请求的方法和请求的HTTP协议
    $status                #请求状态码
    $body_bytes_sent        #服务器回应的字节数,不包含头部大小
    $bytes_sent            #服务器回应的总字节数
    $msec                #日志写入时间,单位为秒,精度为毫秒
    $http_referer            #记录链接访问源地址
    $http_user_agent        #记录客户端浏览器信息
    $http_x_forwarded_for        #跨越代理服务器,记录客户机的原始ip
    $request_length            #请求包的长度(请求头+请求正文)
    $request_time            #请求花费的时间,单位为秒,精度为毫秒

我们先看一下默认日志内容:

cd /var/log/nginx && tail -f 1 access.log

nginx autoindex相关配置 nginx autoindex on_重启_18

 可以看到默认的日志时间格式是国外的时间写法,我们改为中国的时间格式

vi /etc/nginx/nginx.conf
$time_iso8601

nginx autoindex相关配置 nginx autoindex on_运维_19

 保存重启nginx访问一下再看看日志

nginx autoindex相关配置 nginx autoindex on_nginx autoindex相关配置_20

 看到日志格式发生了变化 ,其他的更改方法我就不一一罗列了

以上就是本章的全部内容,后面可能会更新