nginx日志访问及其控制

2020-03-03  本文已影响0人  linux_python

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控制并发的方法有两种:

这两个功能分别由上方叙述的两个模块来实现;

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 $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:程序处理完已有的任务后退出.默认动作为忽略该信号
上一篇下一篇

猜你喜欢

热点阅读