海运的博客

Nginx/Lnmp定时分割日志shell脚本

发布时间:May 5, 2012 // 分类:日记分析 // No Comments

如果服务器访问量较大,Nginx日志占用空间也是个很大的问题,也不可能每天都手工清除日志,久而久之硬盘空间。。。
此脚本用于每天定时切割日记,备份30天内的日志数据备查,并删除30前备份的数据。

#/bin/bash
#Create by https://www.haiyun.me
log_path="/home/wwwlogs"
mkdir -p $log_path/$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/$(date -d "yesterday" +"%d")  
for logname in $(ls /home/wwwlogs/*.log |awk -F/ '{print $NF}')
do
         mv $log_path/$logname $log_path/$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/$(date -d "yesterday" +"%d")/$logname
 done
kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`
find $log_path -type d -mtime +30 -exec rm {} \;

将此脚本添加到计划任务:

echo "30 1 * * * sh /path/file.sh" > /etc/cron.d/nginx
#每天凌晨1点30执行任务

Vim添加Nginx配置文件语法高亮

发布时间:May 4, 2012 // 分类:Vim // No Comments

mkdir -p ~/.vim/syntax/
wget -O ~/.vim/syntax/nginx.vim https://www.haiyun.me/usr/uploads/2012/05/2410071208.vim
echo "au BufRead,BufNewFile /usr/local/nginx/conf/* set ft=nginx" >> ~/.vimrc

效果图如下:
nginx配置.png

Nginx反向代理做负载均衡及缓存服务器

发布时间:May 2, 2012 // 分类:高可用 // No Comments

安装编译环境及相关组件:

yum -y install gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 \
libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl \
curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel openldap \
openldap-devel nss_ldap openldap-clients openldap-servers unzip

编译安装PCRE、Nginx及缓存清除模块ngx_cache_purge:

/usr/local/src/
wget http://sourceforge.net/projects/pcre/files/pcre/8.30/pcre-8.30.zip
unzip pcre-8.30.zip 
cd pcre-8.30
./configure 
make && make install  
cd ..
wget http://labs.frickle.com/files/ngx_cache_purge-1.2.tar.gz
tar zxvf ngx_cache_purge-1.2.tar.gz 
wget http://nginx.org/download/nginx-1.0.15.tar.gz
tar zxvf nginx-1.0.15.tar.gz 
cd nginx-1.0.15
./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module  \
--with-http_gzip_static_module --add-module=../ngx_cache_purge-1.2
make && make install  

Nginx配置文件:

user  www www;

worker_processes 4;

error_log  /usr/local/nginx/logs/nginx_error.log  crit;

pid        /usr/local/nginx/logs/nginx.pid;

worker_rlimit_nofile 65535;

events
    {
        use epoll;
        worker_connections 65535;
    }

http
    {
        include       mime.types;
                default_type application/octet-stream; 

                charset utf-8;   

        server_names_hash_bucket_size 128;
        client_header_buffer_size 32k;
        large_client_header_buffers 4 32k;
        client_max_body_size 50m;

        sendfile on;
        tcp_nopush     on;

        keepalive_timeout 60;

        tcp_nodelay on;

                proxy_temp_path /cache_tmp;
                proxy_cache_path /cache levels=1:2 keys_zone=proxy_cache:256m inactive=10d max_size=10g;

                client_body_buffer_size  512k;
                proxy_connect_timeout 5;   
                proxy_read_timeout 60;   
                proxy_send_timeout 15; 
                proxy_buffer_size        16k;
                proxy_buffers            4 64k;
                proxy_busy_buffers_size 128k;
                proxy_temp_file_write_size 128k;

        gzip on;
        gzip_min_length  1k;
        gzip_buffers     4 16k;
        gzip_http_version 1.0;
        gzip_comp_level 2;
        gzip_types       text/plain application/x-javascript text/css application/xml;
        gzip_vary on;

                upstream proxy_server_pool 
                {
                server node1.www.haiyun.me;
                server node2.www.haiyun.me;
                }
                 

                 log_format   proxy
                 $remote_addr-$remote_user-$time_local-$request-$body_bytes_sent-$http_referer-
                 $http_user_agent-$upstream_addr-$upstream_cache_status-$upstream_status ;
server
        {
                 listen       80;
                 server_name cdn.www.haiyun.me;
                 location /
                 {
                 proxy_next_upstream http_502 http_504 error timeout invalid_header;   
                 proxy_cache proxy_cache;
                 proxy_cache_valid  200 304 1d;
                 proxy_cache_key $host$uri$is_args$args; #$http_cookie
                 add_header Nginx-Cache "$upstream_cache_status from $upstream_addr";
                 proxy_set_header Host $host;
                 proxy_set_header X-Forwarded-For $remote_addr;
                 proxy_pass http://proxy_server_pool;
                 }

                 location ~ .*\.(php|jsp|cgi)?$
                 {
                 proxy_set_header Host $host;
                 proxy_set_header X-Forwarded-For $remote_addr;
                 proxy_pass http://proxy_server_pool;
                 }
                 location ~ /purge(/.*)   
                 {    
                 allow 127.0.0.1;   
                 deny all;   
                 proxy_cache_purge proxy_cache $host$uri$is_args$args;   
                 }   
                 access_log  /usr/local/nginx/logs/cdn.www.haiyun.me.log proxy;
         }
}

飞飞影视Nginx伪静态规则

发布时间:April 23, 2012 // 分类:伪静态 // No Comments

2.0版本:

location / {
rewrite ^/vod-(.*)\.html$ /index.php?s=/Home-vod-$1 last;
rewrite ^/news-(.*)$ /index.php?s=/Home-news-$1 last;
rewrite ^special-(.*)$ /index.php?s=/Home-special-$1 last;
rewrite ^/tag-(.*)$ /index.php?s=/Home-tag-$1 last;
rewrite ^/gb-(.*)$ /index.php?s=/Home-gb-$1 last;
rewrite ^/cm-(.*)$ /index.php?s=/Home-cm-$1 last;
rewrite ^/map-(.*)$ /index.php?s=/Home-map-$1 last;
rewrite ^/my-(.*)$ /index.php?s=/Home-my-$1 last; 
rewrite ^/Tpl/(.*)/Home/(.*).html$ /index.php last;
}

1.9版本:

rewrite ^/vod-(.*)\.html$ /index.php?s=/Home-vod-$1;
rewrite ^/news-(.*)$ /index.php?s=/Home-news-$1;
rewrite ^/ajax-(.*)$ /index.php?s=/Home-ajax-$1;
rewrite ^/tag-(.*)$ /index.php?s=/Home-tag-$1;
rewrite ^/gb-(.*)$ /index.php?s=/Home-gb-$1;
rewrite ^/cm-(.*)$ /index.php?s=/Home-cm-$1;

Nginx安装字符替换模块substitutions_filter

发布时间:April 22, 2012 // 分类:Nginx // No Comments

substitutions_filter是Nginx的一个扩展模块,用来搜索并替换应答内容中的文本,配合nginx的反向代理有妙用哦。
添加此模块需重新编译Nginx,LNMP编译升级请参考:LNMP平滑升级
首先获取substitutions_filter:

yum -y install subversion
svn checkout http://substitutions4nginx.googlecode.com/svn/trunk/ substitutions4nginx-read-only

编译Nginx时添加:

./configure --add-module=/path/substitutions4nginx-read-only

使用语法:

subs_filter 源字符 目标字符 [gior] 
#g:替换全部
#o:替换首个
#i:不区分大小写
#r:使用正则替换

使用范例:

location / {
    subs_filter_types text/html text/css text/xml; #替换的文件类型
    subs_filter st(\d*).onovps $1.www.haiyun.me ir; #
    subs_filter www.haiyun.me www.haiyun.me;
}

注意:开启GZIP后不能替换,需关闭:

proxy_set_header Accept-Encoding "";
分类
最新文章
最近回复
  • 海运: 恩山有很多。
  • swsend: 大佬可以分享一下固件吗,谢谢。
  • Jimmy: 方法一 nghtp3步骤需要改成如下才能编译成功: git clone https://git...
  • 海运: 地址格式和udpxy一样,udpxy和msd_lite能用这个就能用。
  • 1: 怎么用 编译后的程序在家里路由器内任意一台设备上运行就可以吗?比如笔记本电脑 m参数是笔记本的...
  • 孤狼: ups_status_set: seems that UPS [BK650M2-CH] is ...
  • 孤狼: 擦。。。。apcupsd会失联 nut在冲到到100的时候会ONBATT进入关机状态,我想想办...
  • 海运: 网络,找到相应的url编辑重发请求,firefox有此功能,其它未知。
  • knetxp: 用浏览器F12网络拦截或监听后编辑重发请求,修改url中的set为set_super,将POS...
  • Albert: 啊啊啊啊啊啊啊啊啊 我太激动了,终于好了英文区搜索了半天,翻遍了 pve 论坛没找到好方法,博...