nginx(四)

Nginx日志切割

如果任由访问日志写下去,日志文件会变得越来越大,甚至是写满磁盘。
所以,我们需要想办法把日志做切割,比如每天生成一个新的日志,旧的日志按规定时间删除即可。

实现日志切割可以通过写shell脚本或者系统的日志切割机制实现。

shell脚本切割Nginx日志

切割脚本内容:

#!/bin/bash
logdir=/var/log/nginx  //定义日志路径
prefix=`date -d "-1 day" +%y%m%d`  //定义切割后的日志前缀
cd $logdir  
for f in `ls *.log`
do
   mv $f $f-$prefix  //把日志改名
done
/bin/kill -USR1 $(cat /usr/local/nginx/logs/nginx.pid 2>/dev/null) 2>/dev/null  //生成新的日志
bzip2 *$prefix  //压缩日志
find . -type f -mtime +180 |xargs /bin/rm -f  //删除超过180天的老日志

系统日志切割机制

在/etc/logrotate.d/下创建nginx文件,内容为:

/data/logs/*log {
    daily
    rotate 30
    missingok
    notifempty
    compress
    sharedscripts
    postrotate
        /bin/kill -USR1 $(cat /usr/local/nginx/logs/nginx.pid 2>/dev/null) 2>/dev/null || :
    endscript
}

说明:

  1. nginx日志在/data/logs/目录下面,日志名字以log结尾
  2. daily表示每天切割
  3. rotate 30表示日志保留30天
  4. missingok表示忽略错误
  5. notifempty表示如果日志为空,不切割
  6. compress表示压缩
  7. sharedscripts和endscript中间可以引用系统的命令
  8. postrotate表示当切割之后要执行的命令

nginx监控

配置Nginx状态

Nginx有内置一个状态页,需要在编译的时候指定参数–with-http_stub_status_module参数方可打开。
也就是说,该功能是由http_stub_status_module模块提供,默认没有加载。
Nginx配置文件示例

server{
	listen 80;
	server_name www.aminglinux.com;
	
	location /status/ {
	    stub_status on;
	    access_log off;
	    allow 127.0.0.1;
	    allow 192.168.10.0/24;
	    deny all;
	}
}

配置说明
location /status/这样当访问/status/时即可访问到状态页内容。
stub_status on即打开了状态页。
access_log off不记录日志
allow和deny只允许指定IP和IP段访问,因为这个页面需要保护起来,并不公开,当然也可以做用户认证。
测试和结果说明
测试命令:curl -x127.0.0.1:80 www.aminglinux.com/status/

结果如下:

Active connections: 1 
server accepts handled requests
 11 11 11 
Reading: 0 Writing: 1 Waiting: 0 

说明:
active connections – 活跃的连接数量
server accepts handled requests — 总共处理的连接数、成功创建的握手次数、总共处理的请求次数
需要注意,一个连接可以有多次请求。
reading — 读取客户端的连接数.
writing — 响应数据到客户端的数量
waiting — 开启 keep-alive 的情况下,这个值等于 active – (reading+writing), 意思就是 Nginx 已经处理完正在等候下一次请求指令的驻留连接.

配置Nginx和php

配置如下(在server部分添加):

    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_pass unix:/tmp/php-fcgi.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

配置说明:

  1. fastcgi_params文件在/usr/local/nginx/conf/下面,其内容为fastcgi相关的变量
  2. fastcgi_pass后面跟的是php-fpm服务监听地址,可以是IP:PORT,也可以是unix socket地址,也支持upstream的地址
  3. fastcgi_index定义索引页,如果在server内其他部分有定义index参数,该配置可以忽略
  4. fastcgi_param这行其实可以在fastcgi_params文件里面定义SCRIPT_FILENAME变量,这个变量如果不定义,php的请求是没办法访问的。

Nginx+Tomcat架构

配置文件示例

server
{
    listen 80;
    server_name www.aminglinux.com;
    
    location ~* "\.(jpg|png|jepg|js|css|xml|bmp|swf|gif|html)$"
    {
        root /data/wwwroot/aminglinux/;
        access_log off;
        expire 7d;
    }
    
    location /
    {
        proxy_pass http://127.0.0.1:8080/;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP      $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

说明:

  1. 首先,把各种静态文件的请求分离出来,单独由nginx处理。
  2. 其他请求直接代理8080端口,即tomcat服务。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章