(三)nginx(第三方模块,变量,日志--->json格式, 下载autoindex, 压缩gzip)

注意

如果用windows下面的浏览器测试nginx
name就需要配置windows下面的hosts文件

C:\Windows\System32\drivers\etc\hosts

一.nginx 第三方模块(必须重新编译安装)

注意版本要求:用nginx-14版本做实验

第三方模块是对nginx 的功能扩展,第三方模块需要在编译安装nginx 的时候
使用参数--add-module=PATH指定路径添加,有的模块是由公司的开发人员针对业务需求定制开发的,有的模块是开源爱好者开发好之后上传到github进行开源的模块,nginx支持第三方模块,需要重新编译源码才能支持

开源的echo模块,实现输出变量等信息(用于调试)
https://github.com/openresty/echo-nginx-module

示例:

#git是一款克隆工具,在互联网上的内容给下载下来
	yum install git –y
	
	# 这个目录下面可以存放一些源码包
	cd /usr/local/src
	git clone https://github.com/openresty/echo-nginx-module.git
	cd nginx-1.16.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
#重要是最后一步---指定echo 模块路径

# make && make install


vim /apps/nginx/conf/conf.d/pc.conf
server {
	location /test {
		#不需要创建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; 

支持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日志

记录访问日志
ngx_http_log_module模块

只能在http中定义
指定日志格式记录请求

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日志		记录在哪个文件    是否压缩     缓冲区多大
 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"}

定义json格式

在朱培志文件中http下定义

在http模块下面
http{
	#如果是apt或者yum安装的将access_json改为log_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
access_log /apps/nginx/logs/access_json.log access_json;

清除日志文件—一般清空就可以(但不要删除,否则需要重启nginx才会生成日志文件)

>  /var/log/nginx/access.log
 或者
 cat  /dev/null > /var/log/nginx/access.log

访问统计

#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


最后执行的时候用
python   access_json.py 	access_json.log

open_log_file_cache缓存记录

open_log_file_cachemax=N [inactive=time] [min_uses=N] [valid=time];

open_log_file_cache off;
缓存各日志文件相关的元数据信息

max:缓存的最大文件描述符数量
min_uses:在inactive指定的时长内访问大于等于此值方可被当作活动项
inactive:非活动时长
valid:验证缓存中各缓存项是否为活动项的时间间隔

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;
}

四. ngx_http_autoindex下载

配置文件下载服务
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/

将/dev/sr0挂载到/download下
在这里插入图片描述
在这里插入图片描述

五. ngx_http_gzip_module压缩

将自己的服务器作为,下载服务器
也就是说,将自己的文件放到一个指定的文件夹下面,启用–>下载服务器功能,相当于ftp服务器
要与ngx_http_autoindex_module一块用

ngx_http_gzip_module

用gzip方法压缩响应数据,节约带宽
gzip on | off;
启用或禁用gzip压缩

gzip_comp_level level;
压缩比由低到高:1 到 9, 默认:1

gzip_disable regex …;
匹配到客户端浏览器不执行压缩
示例:gzip_disable “MSIE[1-6].”;

gzip_min_length length;
启用压缩功能的响应报文大小阈值

gzip_http_version 1.0 | 1.1;
设定启用压缩功能时,协议的最小版本,默认:1.1

gzip_buffers number size;
支持实现压缩功能时缓冲区数量及每个缓存区的大小
默认:32 4k 或 16 8k

gzip_types mime-type …;
指明仅对哪些类型的资源执行压缩操作;即压缩过滤器
默认包含有text/html,不用显示指定,否则出错

gzip_vary on | off;
如果启用压缩,是否在响应报文首部插入“Vary: Accept-Encoding”

gzip_proxied off | expired | no-cache | no-store | private |
no_last_modified | no_etag | auth | any …;

nginx充当代理服务器时,对于后端服务器的响应报文,在何种条件下启用压缩功能


	off:不启用压缩
	expired,no-cache, no-store,private:对后端服务器的响应报文首部
	Cache-Control值任何一个,启用压缩功能

示例

gzip on;
gzip_comp_level 6;
gzip_min_length 64;
gzip_vary on;
gzip_types text/xml text/css application/javascript;

测试用:
压缩的是body部分

curl --compress www.qcq.net

特殊的

自己添加favicon.ico

格式favicon.ico
将这个文件保存到本地
在这里插入图片描述

然后放到nginx网页文件路径中
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值