1️⃣ ngx_http_access_module
http_access_module
包含了http的访问权限控制的一个模块,前面一节的limit_except
指令里用过,还可以用在http, server, location等地方。
allow
- 允许访问指定的网络或地址。如果unix:指定了特殊值(NGINX1.5.1),则允许访问所有UNIX域套接字。
deny
- 拒绝访问指定的网络或地址。如果unix:指定了特殊值(NGINX 1.5.1),则拒绝所有UNIX域套接字的访问。
- 示例:
location /admin {
deny 192.168.1.1;
allow 192.168.1.0/24;
allow 10.1.1.0/16;
allow 2001:0db8::/32;
deny all;
}
- 依次检查规则,直到找到第一个匹配项。在此示例中,仅允许对IPv4网络 10.1.1.0/16(192.168.1.0/24 不包括地址192.168.1.1)和IPv6网络进行访问2001:0db8::/32
2️⃣ ngx_http_auth_basic_module
实现基于用户的访问控制,使用basic机制进行用户认证
-
auth_basic
- 使用“ HTTP基本身份验证”协议启用用户名和密码的验证。
- 指定的参数用作realm。参数值可以包含变量(1.3.10,1.2.7)。
- 该特殊值off允许取消auth_basic从先前配置级别继承的指令的效果。
-
auth_basic_user_file
- 由
htpasswd
命令(http-tools
提供)实现加密文本文件
- 示例:实现使用basic机制进行用户认证
apt -y install apache2-utlis # ubuntu的htpasswd工具包
htpasswd -c -m /apps/nginx/conf.d/.auth_passwd user1 # 生成密码文件
# 修改配置文件
server {
listen 80;
server_name www.studylinux.com;
location / {
root /data/nginx/pc;
location /admin/{
root /data/nginx/;
auth_basic "admin auth";
auth_basic_user_file /apps/nginx/conf.d/.auth_passwd;
}
}
error_page 404 =200 /404.html;
location /404.html {
root /data/nginx/error_page;
}
}
echo "auth passwd" > /data/nginx/admin/index.html
nginx -t
nginx -s reload
3️⃣ ngx_http_stub_status_module
用于输出nginx的基本状态信息
-
stub_status
- 示例:
location =/basic_status {
stub_status;
allow 172.16.0.0/16;
deny all;
}
Active connections: 291
server accepts handled requests
16630948 16630948 31070465
Reading: 6 Writing: 179 Waiting: 106
-
输出信息
Active connections
: 活动状态的连接数
accepts
:已经接受的客户端请求的总数
handled
:已经处理完成的客户端请求的总数
requests
:客户端发来的总的请求数
Reading
:正在读取客户端请求报文首部的连接的连接数
Writing
:正在向客户端发送响应报文过程中的连接数
Waiting
:正在等待客户端发出请求的空闲连接数
4️⃣ ngx_http_log_module
指定http访问日志格式和路径的模块
▶ 1.log_format:定义日志格式
- string可以使用nginx核心模块及其它模块内嵌的变量。
- Nginx内嵌变量:http://nginx.org/en/docs/varindex.html
- nginx主配置文件里面有示例
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
-
参数分析
$remote_addr
:指的就是客户端地址。
$remote_user
:Basic认证时候的用户名
[$time_local]
: 中括号括起来的本地时间(通用的日志格式的那种)
$request
:request URI
$status
:响应码
body_bytes_sent
:返回的body大小
http_referer
:跳转链接,从哪个网页跳转过来,可以用来看外链。
http_user_agent
:用户浏览器类型
http_x_forwarded_for
:简称XFF头,它代表客户端,也就是HTTP的请求端真实的IP,在通过了HTTP 代理或者负载均衡服务器时会添加该项。
- httpd的日志格式默认使用的combined模式,combined格式的默认定义为:
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
- 这里写了各种参数代表的意义:http://blog.csdn.net/hytfly/article/details/11209909
- 官方详细参数解析:http://httpd.apache.org/docs/2.4/mod/mod_log_config.html#formats
▶ 2.accsess_log:定义日志路径 格式,日志模式
- 可以在不同的区域自定义访问日志文件路径和文件名,还有日志格式,以及相关的缓冲的配置
▶ 3.自定义json格式日志
log_format access_json '{"@timestamp":"$time_iso8601",'
'"host":"$server_addr",' '"clientip":"$remote_addr",'
'"size":$body_bytes_sent,' '"responsetime":$request_time,'
'"upstreamtime":"$upstream_response_time",'
'"upstreamhost":"$upstream_addr",'
'"http_host":"$host",'
'"uri":"$uri",'
'"domain":"$host",'
'"xff":"$http_x_forwarded_for",'
'"referer":"$http_referer",'
'"tcp_xff":"$proxy_protocol_addr",'
'"http_user_agent":"$http_user_agent",'
'"status":"$status"}';
access_log /apps/nginx/logs/access_json.log access_json;
open_log_file_cache
- 缓存各日志文件相关的元数据信息。
max
:缓存的最大文件描述符数量
min_uses
:在inactive指定的时长内访问大于等于此值方 可被当作活动项
inactive
:非活动时长
valid
:验正缓存中各缓存项是否为活动项的时间间隔
5️⃣ ngx_http_gzip_module
gzip 是否开启压缩功能
gzip_comp_level: gzip压缩级别1-9,平时用3-5
gzip disable: 禁止IE6之类的浏览器压缩
gzip_min_length: gzip压缩的最⼩⽂件,⼩于设置值的⽂件将不会压缩
gzip_buffers: 支持实现压缩功能时为其配置的缓冲区数量及每个缓存区的大小。
gzip_proxied:nginx作为代理服务器接收到从被代理服务器发送的响应报文后,在何种条件下启用压缩功能的。
-
off
:对代理的请求不启用。
-
no-cache
, no-store
,private
:表示从被代理服务器收到的响应报文首部的Cache-Control
的值为此三者中任何一个,则启用压缩功能。
gzip_types mime-type: 压缩过滤器,仅对此处设定的MIME类型的内容启用压缩功能。
gzip_vary on | off: 如果启⽤压缩,是否在响应报⽂⾸部插⼊Vary: Accept-Encoding
- 示例:开启压缩,响应报文首部插入“Vary: Accept-Encoding”,支持压缩html, js, css, txt文件
server {
listen 80;
server_name www.studylinux.com;
location / {
root /data/nginx/pc;
gzip on;
gzip_comp_level 3;
gzip_min_length 32;
gzip_types text/css application/javascript text/plain;
gzip_vary on;
}
nginx -t
nginx -s reload
# 制作txt文件要大于配置文件要求 否则压缩不启用 上述配置32K
# 开始测试
root@ubuntu1804-31:~# curl -I www.studylinux.com/ceshi.txt
HTTP/1.1 200 OK
Server: nginx
Date: Mon, 06 Jan 2020 14:04:13 GMT
Content-Type: text/plain
Last-Modified: Mon, 06 Jan 2020 14:03:54 GMT
Connection: keep-alive
Vary: Accept-Encoding # 响应报文
ETag: W/"5e133e4a-2504ba"
Content-Encoding: gzip # 启用压缩传输