@[TOC](Nginx 第三方模块echo 自定义 小节6)
nginx 第三方模块(echo)
- 第三模块是对nginx 的功能扩展,第三方模块需要在编译安装Nginx 的时候使用参数--add-module=PATH指定路径添加,有的模块是由公司的开发人员针对业务需求定制开发的,有的模块是开源爱好者开发好之后上传到github进行开源的模块,nginx支持第三方模块需要从源码重新编译支持
- 开源的echo模块,实现输出变量等信息
- 示例:
yum install git -y 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 -y
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
示例1:
nginx10
屏幕打印
[root@nginx10 ~]# yum install git -y
[root@nginx10 ~]# cd /usr/local/src/
#克隆
[root@rip10 src]# git clone https://github.com/openresty/echo-nginx-module.git
[root@rip10 src]# ls
echo-nginx-module nginx-1.16.1 nginx-1.16.1.tar.gz
#重新编译
[root@rip10 src]# cd nginx-1.16.1/
[root@rip10 nginx-1.16.1]# ./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-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module --add-module=/usr/local/src/echo-nginx-module
[root@rip10 nginx-1.16.1]# make && make install
[root@rip10 nginx-1.16.1]# vim /apps/nginx/conf/nginx.conf
...
#access_log logs/host.access.log main;
location /echo { <--
default_type text/plain; <--加上此项、可在浏览器直接打开、负责会默认下载页面
echo hello; <--屏幕上打印'hello'
echo $remote_addr; <--显示自己ip地址
} <--
location / {
...
#启动服务
[root@rip10 nginx-1.16.1]# nginx
#重新加载服务
[root@rip10 nginx-1.16.1]# nginx -s reload
centos6
修改hosts文件'192.168.37.10 www.test.com'
[root@centos6 ~]$ vim /etc/hosts
192.168.37.10 www.test.com
[root@centos6 ~]$ curl 192.168.37.10/echo
hello
192.168.37.6
[root@centos6 ~]$ curl www.test.com/echo
hello
192.168.37.6
windows
修改hosts文件'192.168.37.10 www.test.com'
示例2:
nginx10
测试返回路径花的时间
[root@rip10 nginx-1.16.1]# vim /apps/nginx/conf/nginx.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;
}
···
centos6
[root@centos6 ~]$ curl 192.168.37.10/test
hello world,main-->
sub1
sub2
took 2.003 sec for total.
windows
nginx10
[root@rip10 nginx-1.16.1]# vim /apps/nginx/conf/nginx.conf
···
location /echo {
default_type text/plain;
echo hello;
echo $remote_addr;
echo $args; <--变量中存放了URL中的指令
}
···
[root@rip10 nginx-1.16.1]# nginx -s reload
windows
示例3:
nginx10
[root@rip10 nginx-1.16.1]# vim /apps/nginx/conf/nginx.conf
...
location /echo {
default_type text/plain;
echo hello;
echo $remote_addr;
echo $args;
echo $document_root; <--保存了针对当前资源的请求的系统根目录
}
...
[root@rip10 nginx-1.16.1]# nginx -s reload
centos6
[root@centos6 ~]$ curl www.test.com/echo
hello
192.168.37.6
/apps/nginx/html
windows
nginx 变量使用
- $document_uri;#保存了当前请求中不包含指令的URI,注意是不包含请求的指令,如www.magedu.net/main/index.…
- $host;#存放了请求的host名称
- $http_user_agent;#客户端浏览器的详细信息
- $http_cookie;#客户端的cookie信息
- limit_rate 040;echo $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
ngx_http_log_module
- 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
nginx
[root@nginx ~]# vim /etc/nginx/nginx.conf
#添加并写到http中
...
http {
log_format testlog '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" ';
...
[root@nginx ~]# vim /etc/nginx/conf.d/test.conf
charset utf-8;
server_tokens off;
server {
access_log /var/log/nginx/a_net.access.log testlog; <--引用
server_name www.a.net;
root /data/site1;
limit_rate 100k;
location / {
}
location /nginx_status {
stub_status;
allow 127.0.0.1;
allow 192.168.37.0/24;
deny all;
}
location /admin {
root /data;
auth_basic "Admin Area";
auth_basic_user_file /etc/nginx/conf.d/.nginx_passwd;
}
}
server {
server_name *.a.tech;
root /data/site2/;
}
#重新加载
[root@nginx ~]# nginx -s reload
如果重新加载报错nginx: [error] invalid PID number "" in "/run/nginx.pid" ,参考juejin.cn/post/712956…
nginx
打开监控日志
[root@nginx ~]# tail -f /var/log/nginx/a_net.access.log
centos6
[root@centos6 ~]$ curl www.a.net/index.html
/data/site1/index.html
[root@centos6 ~]$ curl 192.168.37.7/index.html
/data/site1/index.html
nginx
监控日志信息
[root@nginx ~]# tail -f /var/log/nginx/a_net.access.log
192.168.37.6 - - [08/Aug/2022:14:29:21 -0400] "GET /index.html HTTP/1.1" 200 23 "-"
192.168.37.6 - - [08/Aug/2022:14:29:25 -0400] "GET /index.html HTTP/1.1" 200 23 "-"
自定义json日志格式
nginx 的默认访问日志记录内容相对比较单一,默认的格式也不方便后期做日志统计分析,生产环境中通常将nginx日志转换为json日志,然后配合使用ELK做日志收集-统计-分析
nginx
[root@nginx ~]# vim /etc/nginx/nginx.conf
http {
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"}';
...
[root@nginx ~]# vim /etc/nginx/conf.d/test.conf
charset utf-8;
server_tokens off;
server {
access_log /var/log/nginx/a_net.access.log access_json; <--
server_name www.a.net;
root /data/site1;
limit_rate 100k;
location / {
}
location /nginx_status {
stub_status;
allow 127.0.0.1;
allow 192.168.37.0/24;
deny all;
}
location /admin {
root /data;
auth_basic "Admin Area";
auth_basic_user_file /etc/nginx/conf.d/.nginx_passwd;
}
}
server {
server_name *.a.tech;
root /data/site2/;
}
#重新加载
[root@nginx ~]# nginx -s reload
#打开监控日志
[root@nginx ~]# tail -f /var/log/nginx/a_net.access.log
192.168.37.6 - - [08/Aug/2022:14:58:35 -0400] "GET /index.html HTTP/1.1" 200 23 "-"
{"@timestamp":"2022-08-08T14:59:09-04:00","host":"192.168.37.7","clientip":"192.168.37.6","size":23,"responsetime":0.000,"upstreamtime":"-","upstreamhost":"-","http_host":"www.a.net","uri":"/index.html","domain":"www.a.net","xff":"-","referer":"-","tcp_xff":"-","http_user_agent":"curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.27.1 zlib/1.2.3 libidn/1.18 libssh2/1.4.2","status":"200"}
{"@timestamp":"2022-08-08T14:59:10-04:00","host":"192.168.37.7","clientip":"192.168.37.6","size":23,"responsetime":0.000,"upstreamtime":"-","upstreamhost":"-","http_host":"192.168.37.7","uri":"/index.html","domain":"192.168.37.7","xff":"-","referer":"-","tcp_xff":"-","http_user_agent":"curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.27.1 zlib/1.2.3 libidn/1.18 libssh2/1.4.2","status":"200"}
centos6
[root@centos6 ~]$ curl www.a.net/index.html
/data/site1/index.html
[root@centos6 ~]$ curl 192.168.37.7/index.html
/data/site1/index.html
nginx
如清理日志、可用此方法丢到垃圾箱中
[root@nginx ~]# ll /var/log/nginx/a_net.access.log
-rw-r--r-- 1 root root 1802 Aug 8 14:59 /var/log/nginx/a_net.access.log
[root@nginx ~]# cat /dev/null > /var/log/nginx/a_net.access.log
[root@nginx ~]# ll /var/log/nginx/a_net.access.log
-rw-r--r-- 1 root root 0 Aug 8 15:06 /var/log/nginx/a_net.access.log
关于favicon.ico
- favicon.ico 文件是浏览器收藏网址时显示的图标,当使用浏览器访问页面时,浏览器会自己主动发起请求获取页面的favicon.ico文件,但是当浏览器请求的favicon.ico文件不存在时,服务器会记录404日志,而且浏览器也会显示404报错
- 解决方案:
服务器不记录访问日志: location = /favicon.ico { log_not_found off; access_log off; }将图标保存到指定目录访问: #location ~ ^/favicon\.ico$ { location = /favicon.ico { root /data/nginx/html/pc/images; }
nginx
#进到网页存放目录
[root@nginx data]# cd /data/site1/
#上传图片并改名
[root@nginx site1]# rz favicon.ico
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; limit_rate 100k; root /data/nginx/html/pc; Index index.html; } mkdir/data/nginx/html/pc/download/
示例:
创建下载页面
nginx
[root@nginx ~]# cd /data/site1/
[root@nginx site1]# mkdir download
[root@nginx site1]# echo /data/site1/download/index.html > download/index.html
#挂载
[root@nginx site1]# mount /dev/sr0 download/
[root@nginx site1]# vim /etc/nginx/conf.d/test.conf
charset utf-8;
server_tokens off;
server {
access_log /var/log/nginx/a_net.access.log access_json;
server_name www.a.net;
root /data/site1;
limit_rate 100k;
location / {
}
location /download { <--
autoindex on; <--
autoindex_exact_size off; <--
autoindex_localtime on; <--
limit_rate 100k; <--
index index.html; <--
} <--
location /nginx_status {
stub_status;
allow 127.0.0.1;
allow 192.168.37.0/24;
deny all;
}
location /admin {
root /data;
auth_basic "Admin Area";
auth_basic_user_file /etc/nginx/conf.d/.nginx_passwd;
}
}
server {
server_name *.a.tech;
root /data/site2/;
}
[root@nginx site1]# nginx -s reload
windows
显示下载页面信息