nginx日志访问及其控制
access_log日志模块的详细用法
access_log指令块是来自于ngx_http_log_module模块的, 默认集成在nginx二进制文件中, 是不可以禁用的; 但可以在配置文件中暂时关闭这个功能; access_log off;
可以关闭access_log日志记录功能;
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 gzip=info;
Syntax: access_log path [format [buffer=size] [gzip[=level]] [flush=time] [if=condition]];
access_log off;
Default: access_log logs/access.log combined;
buffer: 开辟的内存空间, 用来缓存日志, 当到达指定大小后, 出发磁盘的IO进行记录;
gzip: 指定日志的等级,随后进行在该等级以上的日志进行压缩处理;
flush: 日志刷新时间,时限;经常与buffer配合使用;
if: 判断符合条件condition的数据, 才记录到日志文件中;
nginx服务热升级解决方案(平滑升级)
通常情况下, nginx的旧版本不会支持很多新的功能所以为了测试新的功能, 我们都会更新nginx的版本来测试新版本的某些特性; 但是在升级的过程中又不能断掉已有的连接, 所以就有了热升级这么一说; 但是新版本又有很多不确定性存在, 所以还要在升级之后能够快速的回滚到原来的版本;
热升级的流程:
image.png
# 本例子适用于实验性,但足够贴近生产; 生产环境中不会有本实验的更换速度是一定的;
[root@hotdeploy ~]# tar xf nginx-1.14.2.tar.gz -C /opt/
[root@hotdeploy ~]# cd /opt/nginx-1.14.2/
[root@hotdeploy nginx-1.14.2]# groupadd -g 996 nginx
[root@hotdeploy nginx-1.14.2]# useradd -u 998 -g 996 -s /sbin/nologin -M nginx
[root@hotdeploy nginx-1.14.2]# ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx
[root@hotdeploy nginx-1.14.2]# make && make install
# 热部署流程
[root@hotdeploy ~]# ps -ef | grep nginx
root 4250 1 0 20:59 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 4251 4250 0 20:59 ? 00:00:00 nginx: worker process
[root@hotdeploy ~]# tar xf nginx-1.16.1.tar.gz -C /opt/
[root@hotdeploy ~]# cd /opt/nginx-1.16.1
[root@hotdeploy nginx-1.16.1]# ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx
[root@hotdeploy nginx-1.16.1]# make
[root@hotdeploy ~]# cp /usr/local/nginx/sbin/{nginx,nginx.old}
[root@hotdeploy ~]# cp -f /opt/nginx-1.16.1/objs/nginx /usr/local/nginx/sbin/nginx
cp:是否覆盖"/usr/local/nginx/sbin/nginx"? y
[root@hotdeploy ~]# ls -l /usr/local/nginx/sbin
总用量 7396
-rwxr-xr-x 1 root root 3825088 12月 3 21:03 nginx
-rwxr-xr-x 1 root root 3746336 12月 3 21:02 nginx.old
[root@hotdeploy ~]# kill -USR2 4250
[root@hotdeploy ~]# ps -ef | grep nginx
root 4250 1 0 20:59 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 4251 4250 0 20:59 ? 00:00:00 nginx: worker process
root 6794 4250 0 21:05 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 6795 6794 0 21:05 ? 00:00:00 nginx: worker process
[root@hotdeploy ~]# kill -WINCH 4250
[root@hotdeploy ~]# ps -ef | grep nginx
root 4250 1 0 20:59 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
root 6794 4250 0 21:05 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 6795 6794 0 21:05 ? 00:00:00 nginx: worker process
# 快速回滚
[root@hotdeploy ~]# /usr/local/nginx/sbin/nginx -s stop
[root@hotdeploy ~]# ps -ef | grep nginx
nginx 6795 1 0 21:05 ? 00:00:00 nginx: worker process
[root@hotdeploy ~]# kill -9 6795
[root@hotdeploy ~]# /usr/local/nginx/sbin/nginx
[root@hotdeploy ~]# ps -ef | grep nginx
root 24441 1 0 21:14 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 24442 24441 0 21:14 ? 00:00:00 nginx: worker process
nginx错误页面个性化设置
通常情况下工程师都无法保证网页会一直被用户访问到, 所以通常情况下设置404页面的显示成了突破口, 不再让用户看到明显的错误页面也成了各大网站竞争手段; 当然其他错误页面也是可以自行设定的; 通常情况下使用nginx自带的error_page即可定义显示的404, 50x等错误页面; 也有的网站是使用rewrite去重定向到一个其他的页面的; 两种方式都是可以的; 但肯定不是用户想要得到的页面; 但是能够阻挡一阵因网站不稳定带来的投诉;
fusion 01: 自行编辑404页面;
#自行下载写好的404页面;
fusion 02: rewrite重定向到与网站相关的网页中;
location / {
root /usr/local/nginx/html;
index index.html index.htm;
if (!-e $request_filename) {
rewrite ^(.*)$ /40x.html;
}
}
访问控制及流量控制=访问量控制
nginx对于流量的控制可以使用ngx_http_limit_conn_module模块来做;
而访问控制可以使用ngx_http_limit_req_module模块进行控制; 简称流量控制;
对于webserver而言, 当遇到爬虫或恶意大流量攻击的时候, 会造成服务器内存和CPU过载, 同时带宽也会跑满, 所以针对这些场景要进行访问控制; nginx控制并发的方法有两种:
- 通过对ip或者其他参数控制其并发量-并发量
- 通过控制单位时间内总的请求处理量-并行量
这两个功能分别由上方叙述的两个模块来实现;
-
limit_conn_zone
指令配置 limit_conn_zone key zone=name:size
配置的上下文:http
说明:key 是 Nginx 中的变量, 通常为 $binary_remote_addr或$server_name; name 为共享内存的名称, size 为该共享内存的大小; 此配置会申请一块共享内存空间 name, 并且保存 key 的访问情况 -
limit_conn_log_level
语法:limit_conn_log_level info|notice|warn|error
默认值:error
配置上下文:http, server, location
说明:当访问达到最大限制之后, 会将访问情况记录在日志中 -
limit_conn
语法:limit_conn zone_name number
配置上下文:http, server, location
说明:使用 zone_name 进行访问并发控制, 当超过 number 时返回对应的错误码 -
limit_conn_status
语法:limit_conn_status code
默认值:503
配置上下文:http, server, location
说明:当访问超过限制 number 时, 给客户端返回的错误码, 此错误码可以配合 error_page 等参数, 在访问超量时给客户返回友好的错误页面 -
limit_rate
语法:limit_rate rate
默认值:0
配置上下文:http, server, location
说明:对每个链接的速率进行限制, rate 表示每秒的下载速度; -
limit_rate_after
语法:limit_rate_after size
配置上下文:http, server, location
说明:此命令和 limit_rate 配合, 当流量超过 size 之后, limit_rate 才开始生效
limit_conn_zone $binary_remote_addr zone=addr:10m;
server {
listen 80;
server_name www.domain.com;
location /ip {
root /path/;
index index.html index.htm;
limit_rate_after 100;
limit_rate 50; # 带宽限制
limit_conn addr 5; # 控制并发访问
limit_conn_status 503; # 超限制后返回的状态码;
limit_conn_log_level warn; # 日志记录级别
}
# 当超过并发访问限制时, 返回503错误页面
error_page 503 /503.html;
}
-
limit_req_zone
语法: limit_req_zone key zone=name:size rate=rate
配置上下文: http
说明: key 是 Nginx 中的变量, 通常为 $binary_remote_addr或$server_name; name 为共享内存的名称, size 为该共享内存的大小; rate 为访问频率, 单位为 r/s 、r/m. 此配置会申请一块共享内存空间 name, 并且保存 $key 的访问情况; -
limit_req
语法: limit_rate zone=name [burst=number] [nodelay|delay=number]
配置上下文: http, server, location
说明: 开启限制, burst 设置最多容量, nodelay 决定当请求超量是, 是等待处理还是返回错误码;
limit_req_zone $binary_remote_addr zone=req:10m rate=2r/s; 200r/m
server {
listen 80;
server_name www.domain.com;
location /limit {
root /path/;
index index.html index.htm;
limit_req zone=req burst=3 nodelay;
limit_req_status 503;
}
# 当超过并发访问限制时,返回503错误页面
error_page 503 /503.html;
}
当单位时间内请求数超过rate时, 模块会检测burst值, 如果值为0, 则请求会依据delay|nodelay配置返回错误或者进行等待; 如果burst大于0时, 当请求数大于rate但小于burst时,请求进入等待队列进行处理;
附录
1)SIGHUP:当用户退出shell时,由该shell启动的所有进程将收到这个信号,默认动作为终止进程
2)SIGINT:当用户按<Ctrl+C>组合键时,用户终端向正在运行中的由该终端启动的程序发出此信号.默认动作为终止进程
3)SIGQUIT:当用户按<ctrl+\>组合键时产生该信号,终端向正在运行中的由该终端启动的程序发信号.默认动作为终止进程
9)SIGKILL:无条件终止进程.本信号不能被忽略,处理和阻塞.默认动作为终止进程.
12)SIGUSR2:另外一个用户自定义信号,程序员可以在程序中定义并使用该信号.默认动作为终止进程
15)SIGTERM:程序结束信号该信号可以被阻塞和终止.通常用来要示程序正常退出.缺省产生这个信号.默认动作为终止进程.
28)SIGWINCH:程序处理完已有的任务后退出.默认动作为忽略该信号