nginx的http协议扩展

ngx_http_access_module模块

可实现基于ip的访问控制功能

allow address | CIDR | unix: | all

deny address | CIDR | unix: | all;

http, server, location, limit_except
自上而下检查,一旦匹配,将生效,条件严格的置前
示例:

location /about {
	root /data/nginx/html/pc;
	index index.html;
	deny 192.168.1.1;
	allow 192.168.1.0/24;
	allow 10.1.1.0/16;
	allow 2001:0db8::/32;
	deny all; #先允许小部分,再拒绝大部分
}

ngx_http_auth_basic_module模块

实现基于用户的访问控制,使用basic机制进行用户认证

auth_basic string | off;

auth_basic_user_file file;

location /admin/ {
	auth_basic "Admin Area";
	auth_basic_user_file /etc/nginx/.ngxpasswd; 
}

用户口令文件:
1、明文文本:格式name:password:comment
2、加密文本:由htpasswd命令实现
httpd-tools所提供

ngx_http_autoindex_module

配置文件下载服务

autoindex on | off;

自动文件索引功能,默为off

autoindex_exact_size on | off;

计算文件确切大小(单位bytes),off 显示大概大小(单位K、M),默认on

autoindex_localtime on | off ;

显示本机时间而非GMT(格林威治)时间,默认off

autoindex_format html | xml | json | jsonp;

显示索引的页面文件风格,默认html

配置文件下载服务生产案例

location /download {
	autoindex on; 
	autoindex_exact_size off; 
	autoindex_localtime on;
	autoindex_format json;
	limit_rate 100k;
	root /data/nginx/html/pc;
	index index.html;
}
mkdir /data/nginx/html/pc/download/

ngx_http_stub_status_module模块

用于输出nginx的基本状态信息, 输出信息示例:
Active connections: 291
server accepts handled requests #下面三个数分别对应accepts,handled,requests
16630948 16630948 31070465
Reading: 6 Writing: 179 Waiting: 106
Active connections:当前状态,活动状态的连接数
accepts:统计总值,已经接受的客户端请求的总数
handled:统计总值,已处理完成的客户端请求总数,一般和accepts相同,除非拒绝
requests:统计总值,客户端发来的总的请求数
Reading:当前状态,正在读取客户端请求报文首部的连接的连接数
Writing:当前状态,正在向客户端发送响应报文过程中的连接数
Waiting:当前状态,正在等待客户端发出请求的空闲连接数

stub_status;

示例:

location /nginx_status {
	stub_status;
	allow 127.0.0.1;
	allow 172.16.0.0/16;
	deny all;
 }

nginx 第三方模块

第三模块是对nginx 的功能扩展,第三方模块需要在编译安装nginx 的时候使用参数–add-module=PATH指定路径添加,有的模块是由公司的开发人员针对业务需求定制开发的,有的模块是开源爱好者开发好之后上传到github进行开源的模块,nginx支持第三方模块,需要重新编译源码才能支持
开源的echo模块,实现输出变量等信息
https://github.com/openresty/echo-nginx-module
示例:

yum install git –y
cd /usr/local/src
git clone https://github.com/openresty/echo-nginx-module.git
cd nginx-1.14.0/
useradd –r –s /sbin/nologin nginx
yum install gcc pcre-devel openssl-devel zlib-devel perl-ExtUtils-Embed
 ./configure \
--prefix=/apps/nginx \
--user=nginx --group=nginx \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-http_perl_module \
--with-pcre \
--with-stream \
--with-stream_ssl_module \
--with-stream_realip_module \
--add-module=/usr/local/src/echo-nginx-module
 make && make install
vim /apps/nginx/conf/conf.d/pc.conf
location /test {
	index index.html;
	default_type text/html;
	echo "hello world,main-->";
	echo_reset_timer;
	echo_location /sub1;
	echo_location /sub2;
	echo "took $echo_timer_elapsed sec for total.";
}
location /sub1 {
	echo_sleep 1;
	echo sub1;
}
location /sub2 {
	echo_sleep 1;
	echo sub2;
}

nginx 变量使用

nginx的变量可以在配置文件中引用,作为功能判断或者日志等场景使用,变量可以分为内置变量和自定义变量,内置变量是由nginx模块自带,通过变量可以获取到众多的与客户端访问相关的值

常见内置变量

$remote_addr;#存放了客户端的地址,注意是客户端的公网IP
$args#变量中存放了URL中的指令
http://www.magedu.net/main/index.do?id=090&partner=search
以上:id=090&partner=search 即为 $args
$document_root#保存了针对当前资源的请求的系统根目录,如/apps/nginx/html
$cookie_name; #表示key为 name 的cookie值
$document_uri#保存了当前请求中不包含指令的URI,注意是不包含请求的指令,如http://www.magedu.net/main/index.do?id=090&partner=search会被定义为/main/index.do
$host#存放了请求的host名称
$http_user_agent#客户端浏览器的详细信息
$http_cookie#客户端的cookie信息
$limit_rate#如果nginx服务器使用limit_rate配置了显示网络速率,则会显示,如果没有设置, 则显示0
$remote_port#客户端请求Nginx服务器时客户端随机打开的端口
$remote_user#已经经过Auth Basic Module验证的用户名
$request_body_file#做反向代理时发给后端服务器的本地资源的名称
$request_method#请求资源的方式,GET/PUT/DELETE等
$request_filename#当前请求的资源文件的路径名称,由root或alias指令与
URI请求生成的文件绝对路径,如/apps/nginx/html/main/index.html
$request_uri#包含请求参数的原始URI,不包含主机名
如:main/index.do?id=090&partner=search。 
$scheme#请求的协议,如ftp,https,http等 
$server_protocol#请求资源的协议版本,如HTTP/.0,HTTP/.,HTTP/.0等 
$server_addr#保存了服务器的IP地址
$server_name#请求的服务器的主机名 
$server_port#请求的服务器的端口

自定义变量

自定义变量名称和值,使用指令set $variable value
格式如下:

 set $variable value; 

支持:server, location, if
示例:

set $name magedu;
echo $name;
set $my_port $server_port;
echo $my_port;
echo "$server_name:$server_port";

ngx_http_log_module模块

指定日志格式记录请求

log_format name string …;

string可以使用nginx核心模块及其它模块内嵌的变量

access_log path [format [buffer=size] [gzip[=level]] [flush=time] [if=condition]];

access_log off; #禁用访问日志
访问日志文件路径,格式及相关的缓冲的配置
buffer=size
flush=time
示例

 log_format compression '$remote_addr-$remote_user [$time_local] ' 
'"$request" $status $bytes_sent ' 
'"$http_referer" "$http_user_agent" "$gzip_ratio"';
 access_log /spool/logs/nginx-access.log compression buffer=32k;

自定义json日志格式

nginx 的默认访问日志记录内容相对比较单一,默认的格式也不方便后期做日志统计分析,生产环境中通常将nginx日志转换为json日志,然后配合使用ELK做日志收集-统计-分析
json格式的访问日志示例:

{"@timestamp":"2019-02-
22T08:55:32+08:00","host":"192.168.7.102","clientip":"192.168.0.1","size":162,"resp
onsetime":0.000,"upstreamtime":"-","upstreamhost":"-
","http_host":"www.magedu.net","uri":"/favicon.ico","domain":"www.magedu.net","xff"
:"-","referer":"-","tcp_xff":"","http_user_agent":"Mozilla/5.0 (Windows NT 6.1;
Win64; x64; rv:65.0) Gecko/20100101 Firefox/65.0","status":"404"}

配置

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;

json格式的日志访问统计

#cat nginx_json.py
#!/usr/bin/env python
#coding:utf-8
status_200= []
status_404= []
with open("access_json.log") as f:
	for line in f.readlines():
	line = eval(line)
	if line.get("status") == "200":
		status_200.append(line.get)
	elif line.get("status") == "404":
		status_404.append(line.get)
	else:
		print("状态码 ERROR")
f.close()
print "状态码200的有--:",len(status_200)
print "状态码404的有--:",len(status_404)
# python nginx_json.py
状态码200的有--: 1910
状态码404的有--: 13

open_log_file_cache off;

open_log_file_cache max=N [inactive=time] [min_uses=N] [valid=time];
缓存各日志文件相关的元数据信息
max:缓存的最大文件描述符数量
min_uses:在inactive指定的时长内访问大于等于此值方可被当作活动项
inactive:非活动时长
valid:验证缓存中各缓存项是否为活动项的时间间隔

关于favicon.ico

favicon.ico 文件是浏览器收藏网址时显示的图标,当使用浏览器访问页面时,浏览器会自己主动发起请求获取页面的favicon.ico文件,但是当浏览器请求的favicon.ico文件不存在时,服务器会记录404日志,而且浏览器也会显示404报错
解决方案:
服务器不记录访问日志:

location = /favicon.ico {
	log_not_found off; #文件没发现事件不记录error_log
	access_log off; #不记录access_log
}

将图标保存到指定目录访问:

location ~ ^/favicon\.ico$ {
location = /favicon.ico {
	root /data/nginx/html/pc/images;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值