一、高级配置
1 .1网页的状态页
基于nginx 模块 ngx_http_stub_status_module 实现,在编译安装nginx的时候需要添加编译参数 --with-http_stub_status_module,否则配置完成之后监测会是提示语法错误注意: 状态页显示的是整个服务器的状态,而非虚拟主机的状态
参考上一篇文章
#配置示例:这样能更安全
location /nginx_status {
#stub_status;
auth_basic "auth login";
auth_basic_user_file /apps/nginx/conf/.htpasswd;
allow 192.168.0.0/16;
allow 127.0.0.1;
deny all;
}
1.2 Nginx 第三方模块
ehco 模块
开源的echo模块 GitHub - openresty/echo-nginx-module: An Nginx module for bringing the power of "echo", "sleep", "time" and more to Nginx's config file
① 修改子配置文件
[root@zzzcentos1 conf.d]#vim pc.conf
server{
listen 80;
server_name www.lucky.com;
root /data/;
location /ip {
echo "welcome,your ip addr: ";
echo $remote_addr;
}
}
② 依赖nginx编译echo模块
[root@zzzcentos1 conf.d]#cd /opt/
[root@zzzcentos1 opt]#unzip echo-nginx-module-master.zip
[root@zzzcentos1 opt]#cd nginx-1.18.0/
[root@zzzcentos1 nginx-1.18.0]#systemctl stop nginx
[root@zzzcentos1 nginx-1.18.0]#./configure --help | grep add
[root@zzzcentos1 nginx-1.18.0]#nginx -V
[root@zzzcentos1 nginx-1.18.0]#./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=/opt/echo-nginx-module-master
[root@zzzcentos1 nginx-1.18.0]#make && make install
③ 添加echo模块后检查语法不会报错
[root@zzzcentos1 conf.d]#nginx -t
nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /apps/nginx/conf/nginx.conf test is successful
[root@zzzcentos1 conf.d]#nginx -s reload
nginx: [error] invalid PID number "" in "/apps/nginx/logs/nginx.pid"
[root@zzzcentos1 conf.d]#systemctl start nginx
[root@zzzcentos1 conf.d]#nginx -s reload
④ 访问页面
结合看下
1.3变量
官方文档 http://nginx.org/en/docs/varindex.html
1.3.1常用内置变量
$remote_addr;
#存放了客户端的地址,注意是客户端的公网IP
$proxy_add_x_forwarded_for
#此变量表示将客户端IP追加请求报文中X-Forwarded-For首部字段,多个IP之间用逗号分隔,如果请求中没有X-Forwarded-For,就使用$remote_addrthe “X-Forwarded-For” client request header field with the $remote_addr variable appended to it, separated by a comma. If the “X-Forwarded-For” field is not present in the client request header, the $proxy_add_x_forwarded_for variable is equal to the $remote_addr variable.
客户机 代理1 代理2 nginx服务器
$proxy_add_x_forwarded_for: 在代理1 上存的是 客户机的ip
$proxy_add_x_forwarded_for: 在代理2 上存的是 客户机的ip,代理1的ip 用逗号隔开
$proxy_add_x_forwarded_for: nginx 上存的是 客户机的ip,代理1的ip,代理2的ip
$args;
#变量中存放了URL中的参数,例如:http://www.kgc.org/main/index.do?id=20190221&partner=search
#返回结果为: id=20190221&partner=search 存放的就是这个
select * from table where id=20190221
$document_root;
#保存了针对当前资源的请求的系统根目录,例如:/apps/nginx/html
$document_uri;
#保存了当前请求中不包含参数的URI,注意是不包含请求的指令,比
如:http://www.kgc.org/main/index.do?id=20190221&partner=search会被定义为/main/index.do
#返回结果为:/main/index.do
$host;
#存放了请求的host名称
limit_rate 10240;
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; https:// www.baidu.com/main/index.do?id=20190221&partner=search
#包含请求参数的原始URI,不包含主机名,相当于:$document_uri?$args,例如:/main/index.do?id=20190221&partner=search
$scheme;
#请求的协议,例如:http,https,ftp等
$server_protocol;
#保存了客户端请求资源使用的协议的版本,例如:HTTP/1.0,HTTP/1.1,HTTP/2.0等
$server_addr;
#保存了服务器的IP地址
$server_name;
#请求的服务器的主机名
$server_port; 443 https
#请求的服务器的端口号
$http_<name>
#name为任意请求报文首部字段,表示记录请求报文的首部字段
arbitrary request header field; the last part of a variable name is the field name converted to lower case with dashes replaced by underscores
#用下划线代替横线
#示例: echo $http_User_Agent;
$http_user_agent;
#客户端浏览器的详细信息
$http_cookie;
#客户端的cookie信息
$cookie_<name>
#name为任意请求报文首部字部cookie的key名
$http_<name>
#name为任意请求报文首部字段,表示记录请求报文的首部字段,ame的对应的首部字段名需要为小写,如果有
横线需要替换为下划线
arbitrary request header field; the last part of a variable name is the field
name converted to lower case with dashes replaced by underscores #用下划线代替横线
#示例:
echo $http_user_agent;
echo $http_host;
$sent_http_<name>
#name为响应报文的首部字段,name的对应的首部字段名需要为小写,如果有横线需要替换为下划线,此变量有问题
echo $sent_http_server;
$arg_<name>
#此变量存放了URL中的指定参数,name为请求url中指定的参数
#对比 变量 $arg 是全部, 如果 要id 如下
echo $arg_id;
实验:变量
server{
listen 80;
server_name www.lucky.com;
root /data/;
location /ip {
echo "welcome,your ip addr: ";
echo $remote_addr;
}
location /main {
index index.html;
default_type text/html;
echo "hello world,main-->";
echo $remote_addr;
echo $args;
echo $arg_user;
echo $document_root;
echo $document_uri;
echo $host;
echo $http_user_agent;
echo $http_cookie;
echo $request_filename;
echo $scheme;
echo $scheme://$host$document_uri?$args;
}
}
① 修改配置文件
②测试验证:
实验结束:
补充下 正向代理 反向代理
正向代理:代理的是客户端
反向代理:代理的是服务端
反向代理:reverse proxy,指的是代理外网用户的请求到内部的指定的服务器,并将数据返回给用户的一种方式,这是用的比较多的一种方式。
总结
$proxy_add_x_forwarded_for 实现ip 透传,记录每一个地址
#此变量表示将客户端IP追加请求报文中X-Forwarded-For首部字段,多个IP之间用逗号分隔,如果请求中没有X-Forwarded-For,就使用$remote_addrthe “X-Forwarded-For” client request header field with the $remote_addr variable appended to it, separated by a comma. If the “X-Forwarded-For” field is not present in the client request header, the $proxy_add_x_forwarded_for variable is equal to the $remote_addr variable.
客户机 代理1 代理2 nginx服务器
$proxy_add_x_forwarded_for: 在代理1 上存的是 客户机的ip
$proxy_add_x_forwarded_for: 在代理2 上存的是 客户机的ip,代理1的ip 用逗号隔开
$proxy_add_x_forwarded_for: nginx 上存的是 客户机的ip,代理1的ip,代理2的ip
$http_user_agent; 客户端浏览器的详细信息
$server_addr; 服务器的IP地址
$scheme; 请求的协议 例如:http,https,ftp等
$http_cookie; 客户端的缓存信息 缓存:cookie和session
$server_protocol; 你使用的协议的版本
$document_root;指明了主站点目录的位置
#保存了针对当前资源的请求的系统根目录,例如:/apps/nginx/html
1.3.2 自定义变量
假如需要自定义变量名称和值,使用指令set $variable value;
语法格式:
Syntax: set $variable value;
Default: —
Context: server, location, if #可以放置的位置
代码:
location /test {
set $name kgc;
echo $name;
set $my_port $server_port;
echo $my_port;
}
1.3.3自定义图标
favicon.ico 文件是浏览器收藏网址时显示的图标,当客户端使用浏览器问页面时,浏览器会自己主动发起请求获取页面的favicon.ico文件,但是当浏览器请求的favicon.ico文件不存在时,服务器会记录404日志,而且浏览器也会显示404报错
【自定义小图标】
wget www.baidu.com/favicon.ico
放到主目录就可以了不生效可以重新打开浏览器
实验:定义百度图标
实验前:
查看主目录:
下载图标
[root@zzzcentos1 ~]#cd /apps/nginx/conf.d/
[root@zzzcentos1 conf.d]#wget www.baidu.com/favicon.ico
[root@zzzcentos1 conf.d]#cp favicon.ico /data/
检测
实验:定义京东图标
[root@zzzcentos1 data]#wget www.jd.com/favicon.ico
先下载图标,把favicon.ico复制到主目录下,就可以了
1.4 自定义访问日志
如果访问出错---404,可以去看日志信息cat /apps/nginx/logs/error.log
①日志的格式 可自由指定
访问日志是记录客户端即用户的具体请求内容信息,而在全局配置模块中的error_log是记录nginx服务器运行时的日志保存路径和记录日志的level,因此两者是不同的,而且Nginx的错误日志一般只有一个,但是访问日志可以在不同server中定义多个,定义一个日志需要使用access_log指定日志的保存路径,使用log_format指定日志的格式,格式中定义要保存的具体日志内容
Syntax: access_log path [format [buffer=size] [gzip[=level]] (flush=time] [if=condition]];access_log off;
Defau1t:
access_log 1ogs/access.1og combined;
Context: http,server, location,if in location,limit_except
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"'
'$server_name:$server_port';
log_format test '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"'
'$server_name:$server_port';
格式可以定义多个
###注意如果开启 include 注意定义自配置文件与 日志格式的上下关系 , 日志格式一定要在 include 之前 否则会不生效。
解释下:
注意如果开启 include 注意定义自配置文件与 日志格式的上下关系 ,日志格式一定要在 include 之前 否则会不生效。
实验:
去7-2访问:
再去7-1查看日志
②自定义 json 格式日志
方便ELK收集日志
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",'
'"xff":"$http_x_forwarded_for",'
'"referer":"$http_referer",'
'"tcp_xff":"$proxy_protocol_addr",'
'"http_user_agent":"$http_user_agent",'
'"status":"$status"}';
①配置
②去访问
③看日志 tail -f /apps/nginx/logs/access.log
轻松一刻:提取下状态码 200
实验1:
实验2:
③日志分割
nginx的日志是生成在安装目录下/usr/local/nginx/logs中,大量的日志如果不做良好的管理,长此以往会造成日志文件过大且日志分析困难
----------------日志切割-------------------
vim /opt/fenge.sh
#!/bin/bash
# Filename: fenge.sh
day=$(date -d "-1 day" "+%Y%m%d") #显示前一天的时间
logs_path="/var/log/nginx"
pid_path="/usr/local/nginx/logs/nginx.pid"
[ -d $logs_path ] || mkdir -p $logs_path #创建日志文件目录
mv /usr/local/nginx/logs/access.log ${logs_path}/kgc.com-access.log-$day #移动并重命名日志文件
kill -USR1 $(cat $pid_path) #重建新日志文件
find $logs_path -mtime +30 -exec rm -rf {} \; #删除30天之前的日志文件
#find $logs_path -mtime +30 | xargs rm -rf
chmod +x /opt/fenge.sh
/opt/fenge.sh
ls /var/log/nginx
ls /usr/local/nginx/logs/access.log
#设置周期计划任务
crontab -e
0 1 * * * /opt/fenge.sh
编写日志分割的脚本
#!/bin/bash
#this is used for cutting nginx logs
##定义日志收集的目录
newlogpath=/var/log/nginx
##定义区分日志的标识,采用时间标记,计划每天一次日志分割
lastday=`date -d "-1 day" +%Y%m%d`
##找到应用程序生成日志的源路径
oldlogpath=/usr/local/nginx/logs
##定义应用程序的pid号 也就是nginx的master进程号
pid=`cat ${oldlogpath}/nginx.pid`
##先判断收集日志的目录是否存在,没有则创建,有则进行下一步
[ -d $newlogpath ]||mkdir $newlogpath
##使用mv命令进行日志分割,将生成的访问日志和错误日志都移动到收集日志的目录下,并添加时间标记
mv $oldlogpath/access.log $newlogpath/access.log.$lastday
mv $oldlogpath/error.log $newlogpath/error.log.$lastday
##重新生成新的日志,便于nginx记录后续访问事务
kill -USR1 $pid
##删除大于30天的日志,释放磁盘空间
find $newlogpath -mtime +30 -exec rm -rf {} \;
chmod +x ~/nginx_log.sh
crontab -e
0 0 * * * ~/nginx_log.sh
面试:你写过那些脚本?
一键安装及脚本
日志分割
系统调优
自动检测系统性能
1.5 Nginx压缩功能
支持对指定类型的文件进行压缩然后再传输给客户端,而且压缩还可以设置压缩比例,压缩后的文件大小将比源文件显著变小,这样有助于降低出口带宽的利用率,降低企业的IT支出,不过会占用相应的CPU资源。
Nginx对文件的压缩功能 是依赖于模块 ngx_http_gzip_module
允许Nginx服务器将输出内容在发送客户端之前进行压缩,以节约网站带宽,提升用户的访问体验,默认已经安装,可在配置文件中加入相应的压缩功能参数对压缩性能进行优化
官方文档: Module ngx_http_gzip_module
#启用或禁用gzip压缩,默认关闭
gzip on | off;
#压缩比由低到高从1到9,默认为1
gzip_comp_level level;
#禁用IE6 gzip功能
gzip_disable "MSIE [1-6]\.";
#gzip压缩的最小文件,小于设置值的文件将不会压缩
gzip_min_length 1k;
#启用压缩功能时,协议的最小版本,默认HTTP/1.1
gzip_http_version 1.0 | 1.1;
#指定Nginx服务需要向服务器申请的缓存空间的个数和大小,平台不同,默认:32 4k或者16 8k;
gzip_buffers number size;
#指明仅对哪些类型的资源执行压缩操作;默认为gzip_types text/html,不用显示指定,否则出错
gzip_types mime-type ...;
#如果启用压缩,是否在响应报文首部插入“Vary: Accept-Encoding”,一般建议打开
gzip_vary on | off;
#预压缩,先压缩好,不用临时压缩,消耗cpu
gzip_static on | off;
实验: