3月 21

Nginx服务使用的脚本

[编写人:张子萌 2010]

Nginx服务器时使用的脚本.

1.在nginx日志出现错误后发送告警邮件,使用该脚本会在nginx的sbin目录下产生last_error_log,错误文件格式

为nginx默认

#!/bin/sh
##############################################
# AUTHOR: simon-zzm
# back log
# Ver : 1.0 For Production
# description:
# 2010-03-12 simon-zzm@163.com
##############################################
######set ip Email base path
IP=172.2.2.2
admin_mail=simon-zzm@163.com
base_path=/usr/local/nginx

###run
cut_error_log=`/bin/cat ${base_path}/sbin/last_error_log`
last_error_log=`/usr/bin/tail -1 ${base_path}/logs/error.log|/bin/cut -c 1-19`
if [ “$cut_error_log” != “$last_error_log” ];then
/usr/bin/tail -5 ${base_path}/logs/error.log>$base_path/sbin/mail1.txt
/bin/mail -s $IP_nginx_error_log $admin_mail<$base_path/sbin/mail1.txt
/bin/rm -rf $base_path/sbin/mail1.txt
echo $last_error_log>${base_path}/sbin/last_error_log
fi

2.nginx日志切割,每个小时切割一个日志,

#!/bin/sh

##############################################

# AUTHOR: simon-zzm

# back log

# Ver : 1.0 For Production

# description:

# 2010-03-12 simon-zzm@163.com

##############################################

###set base path、use every an hour to cut log
log_dir=”/usr/local/nginx/logs”
date_dir=`date +%Y%m%d%H`

###run
/bin/mv ${log_dir}/access.log ${log_dir}/access_${date_dir}.log
/bin/kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`

3.日志备份部分参照通用性日志备份

http://simon-zzm.blog.163.com/blog/static/88809522201021523828112/

3月 15

日志 压缩 备份 定时删除脚本

[编写整理:simonzhang 2010-03-15 2012-04-08修改]

  在linux下有大量日志需要压缩备份,并定期清理长期保存的备份日志。对于多处日志存放使用数组进行循环处理。脚本修改好后,设置定时任务即可。

#!/bin/sh
##############################################
# AUTHOR: simonzhang
# back log
# Ver : 1.1 For Production
# description: 
# 2010-03-12  
##############################################
####### set log patch
log_path=("/usr/local/nginx/logs/" "/usr/local/tomcat6/logs/" )
####### set backup log patch
bak_log_path=("/usr/local/nginx/logs/back" "/usr/local/tomcat6/logs/back")

####### set backup  3 day ago log
backupdays=3

#######clear 180 day ago compress log
deletedays=180

#######  start
for (( i=0 ; i<${#log_path[@]} ; i++ ))
do
        cd ${log_path[i]}
    if [ ! -f ${bak_log_path[i]} ] ; then
            /bin/mkdir -p ${bak_log_path[i]}
    fi
        /usr/bin/find  * -ctime +$backupdays -maxdepth 0 -not -name *.pid -not -name error* -exec zip -m {}.zip  {} \;
        /bin/mv *.zip ${bak_log_path[$i]}
        cd ${bak_log_path[$i]}
        echo /usr/bin/find  * -ctime +$deletedays -maxdepth 0 -exec rm {} \;
done
############  end
3月 12

nginx 0.8 安装 配置

【整理人:张子萌 最近一次修改2010-12】

nginx的官方网站:http://nginx.org/

1.下载nginx源码
nginx-0.8.34.tar.gz

如果希望在Nginx中使用正则表达式使配置更灵活,需要安装PCRE。
查看是否安装pcre
# rpm -qa pcre*

如果希望在是用ssl加密部分请安装openssl。

如果希望压缩传送需要安装gzip

如果使用nginx使用的缓存,需要清理指定url需要下载并同时编译。
http://labs.frickle.com/files/ngx_cache_purge-1.0.tar.gz

推荐使用升级工具安装,升级工具使用可以参照:
http://simon-zzm.blog.163.com/blog/static/88809522201025102237958/

2. 编译安装nginx
首先创建账户,指定用户与组的id均为700
# groupadd -g 700 nginx
# useradd -g700 -u700 nginx
解压、安装nginx,对于不需要的模块可以不编译。
最后一行为清理指定url模块,可以选择性编译
# tar zxvf ngx_cache_purge-1.0.tar.gz
# tar zxvf nginx-0.8.34.tar.gz
# cd nginx-0.8.34
# ./configure –prefix=/usr/local/nginx
–user=nginx
–group=nginx
–with-pcre
–with-select_module
–with-poll_module
–with-http_stub_status_module
–with-http_ssl_module
–with-http_realip_module
–with-http_gzip_static_module
–add-module=/usr/local/src/ngx_cache_purge-1.0
# make && make install

3. nginx配置文件

主要配置文件:/usr/local/nginx/conf/nginx.conf
# 程序运行用户
user nginx nginx;
# 启动进程数
worker_processes 8;

#给每个进程分配cpu,例:将8个进程分配到8个cpu,也可以写多个,或将一个进程分配到多个cpu

worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;

# 全局错误日志和程序PID文件
error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

pid logs/nginx.pid;

# 工作模式及连接数上限
# 工作模式有:select(标准模式),poll(标准模式),kqueue(高效模式,适用FreeBSD 4.1+, OpenBSD 2.9+, NetBSD 2.0 and MacOS X),
# poll(高效模式。适用Linux 2.6+,SuSE 8.2,Solaris 7 11/99+, HP/UX 11.22+ (eventport), IRIX 6.5.15+ 和 Tru64 UNIX 5.1A+)
events {
use poll;
worker_connections 1024;
}

# 设定http服务器,反向代理功能提供负载均衡支持
http {
#设定mime类型
include mime.types;
default_type application/octet-stream;

# 隐藏nginx版本,防止攻击
server_tokens off;
# 设定日志格式
#log_format main ‘$remote_addr – $remote_user [$time_local] “$request” ‘
# ‘$status $body_bytes_sent “$http_referer” ‘
# ‘”$http_user_agent” “$http_x_forwarded_for”‘;
#设定access log
#access_log logs/access.log main;
#linux下强大的静态文件发送功能,一定要开启
sendfile on;
tcp_nopush on;

keepalive_timeout 60;
tcp_nodelay on;

# 定义一个叫“mysvr”的记录区,总容量为 10M
# 和下面location中的limit_conn一起限制单个IP的并发连接数为10
# limit_zone只能是用在http中,limit_conn可以使用在http、sever、local中
limit_zone mysvr $binary_remote_addr 10m;

#设定负载均衡的服务器列表
upstream mysvr {
ip_hash #如果不需要可以删除直接采用轮训方法负载
#weigth参数表示权值,权值越高被分配到的几率越大
server 192.168.0.1:80 weight=5;
server 192.168.0.2:80 weight=1;
server 192.168.0.3:80 weight=6;
server 192.168.0.5:80;
server 192.168.0.6:80;

#down 表示单前的server暂时不参与负载
#weight 默认为1.weight越大,负载的权重就越大。
#max_fails :允许请求失败的次数默认为1.当超过最大次数时,返回proxy_next_upstream 模块定义的错误
#fail_timeout:max_fails次失败后,暂停的时间。
#backup: 其它所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻。
#ip_hash:每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题
}
# 设定缓存临时目录,临时目录要与proxy_cache_path中目录一样
proxy_temp_path /usr/local/nginx_test/proxy_temp;
# proxy_cache_path中参数如下
# levels指定该缓存空间有两层hash目录,第一层目录是1个字母,第二层为2个字母
# keys_zone为这个空间起个名字,比如 cache_one,在server模块中proxy_cache使用
# 200m 内存缓存空间大小为200MB
# inactive 1天没有被访问的内容自动清除;
# max_size使用硬盘缓存大小30G
# clean_time指定一分钟清理一次缓存。
proxy_cache_path /usr/local/nginx_test/proxy_temp/ levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g;

#设定虚拟主机,默认为监听80端口
server {
listen 80;
server_name 192.168.0.7 mysvr.c
om www.mysvr.com;
#charset gb2312;
#charset utf8;
#charset koi8-r;
#设定本虚拟主机的访问日志
#access_log logs/host.access.log main;

#对 “/” 启用负载均衡
location / {
proxy_pass http://mysvr;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 10m; #允许客户端请求的最大单文件字节数
client_body_buffer_size 128k; #缓冲区代理缓冲用户端请求的最大字节数
proxy_connect_timeout 90; #与后端连接超时时间
proxy_send_timeout 90; #后端服务器回传超时时间
proxy_read_timeout 90; #连成功后,后端服务器响应超时时间。
proxy_buffer_size 4k; #代理服务器保存用户头信息的缓存大小
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k; #高负荷缓村大小。大小计算方法proxy_buffers*2
proxy_temp_file_write_size 64k; #设定缓存文件大小,大于这个值经直接从upstream获得。
limit_conn mysvr 10; #每个IP只能有10个并发
limit_rate 100k; #每个并发只能有100K的速度
index index.html index.htm; #选则文件后缀解析都优先顺序
expires 30d; #客户端缓存时间 30天

}

#设定查看Nginx状态的地址
location /NginxStatus {
stub_status on;
access_log on;
auth_basic “NginxStatus