Nginx Rewrite

break

语法:break

作用域:server,location,if
Example:

if ($slow) {
   limit_rate 10k;
   break;
}

if

语法:if ( condition ) { … }

作用域:server,location

条件可以是:

  • 变量,空字符串或”0″为false
  • 使用=或!=运算符的比较变量
  • 使用正则匹配
  • ~ 为区分大小写匹配
  • ~* 为不区分大小写匹配
  • !~和!~*分别为区分大小写不匹配及不区分大小写不匹配
  • -f和!-f 判断是否存在文件
  • -d和!-d 判断是否存在目录
  • -e和!-e 判断是否存在文件或目录
  • -x和!-x 判断文件是否可执行

正则匹配到的值可以在之后使用$1-$9访问

Examples of use:

if ($http_user_agent ~ MSIE) { rewrite ^(.*)$ /msie/$1 break; } if ($http_cookie ~* "id=([^;] +)(?:;|$)" ) { set $id $1; } if ($request_method = POST ) { return 405; } if ($slow) { limit_rate 10k; } if ($invalid_referer) { return 403; } if ($args ~ post=140){ rewrite ^ http://example.com/ permanent; } Read More

PIL – Convert JPG to GIF Frames

#!/usr/bin/env python #coding:utf-8 import ImageGrab # from PIL import time import string from PIL import Image, ImageChops from PIL.GifImagePlugin import getheader, getdata import os import numpy as np def intToBin(i): """ 把整型数转换为双字节 """ # 先分成两部分,高8位和低8位 i1 = i % 256 i2 = int( i/256) # 合成小端对齐的字符串 return chr(i1) + chr(i2) def getheaderAnim(im): """ 生成动画文件头 """ bb = "GIF89a" bb += intToBin(im.size[0]) bb += intToBin(im.size[1]) bb += "x87x00x00" #使用全局颜色表 return bb def getAppExt(loops=0): """ 应用扩展,默认为0,为0是表示动画是永不停止 """ bb = "x21xFFx0B" # application extension bb += "NETSCAPE2.0" bb += "x03x01" if loops == 0: loops = 2**16-1 bb += intToBin(loops) bb += 'x00' # end return bb def getGraphicsControlExt(duration=0.1): """ 设置动画时间间隔 """ bb = 'x21xF9x04' bb += 'x08' # no transparancy bb += intToBin( int(duration*100) ) # in 100th of seconds bb += 'x00' # no transparant color bb += 'x00' # end return bb def _writeGifToFile(fp, images, durations, loops): """ 把一系列图像转换为字节并存入文件流中 """ # 初始化 frames = 0 previous = None for im in images: if not previous: # 第一个图像 # 获取相关数据 palette = getheader(im)[1] #取第一个图像的调色板 data = getdata(im) imdes, data = data[0], data[1:] header = getheaderAnim(im) appext = getAppExt(loops) graphext = getGraphicsControlExt(durations[0]) # 写入全局头 fp.write(header) fp.write(palette) fp.write(appext) # 写入图像 fp.write(graphext) fp.write(imdes) for d in data: fp.write(d) else: # 获取相关数据 data = getdata(im) imdes, data = data[0], data[1:] graphext = getGraphicsControlExt(durations[frames]) # 写入图像 fp.write(graphext) fp.write(imdes) for d in data: fp.write(d) # 准备下一个回合 previous = im.copy() frames = frames + 1 fp.write(";") # 写入完成 return frames def writeGif(filename, images, duration=0.1, loops=0, dither=1): """ writeGif(filename, images, duration=0.1, loops=0, dither=1) 从输入的图像序列中创建GIF动画 images 是一个PIL Image [] 或者 Numpy Array """ images2 = [] # 先把图像转换为PIL格式 for im in images: if isinstance(im,Image.Image): #如果是PIL Image images2.append( im.convert('P',dither=dither) ) elif np and isinstance(im, np.ndarray): #如果是Numpy格式 if im.dtype == np.uint8: pass elif im.dtype in [np.float32, np.float64]: im = (im*255).astype(np.uint8) else: im = im.astype(np.uint8) # 转换 if len(im.shape)==3 and im.shape[2]==3: im = Image.fromarray(im,'RGB').convert('P',dither=dither) elif len(im.shape)==2: im = Image.fromarray(im,'L').convert('P',dither=dither) else: raise ValueError("图像格式不正确") images2.append(im) else: raise ValueError("未知图像格式") # 检查动画播放时间 durations = [duration for im in images2] # 打开文件 fp = open(filename, 'wb') # 写入GIF try: n = _writeGifToFile(fp, images2, durations, loops) finally: fp.close() return n ## 将多帧位图合成为一幅gif图像 def images2gif( images, giffile, durations=0.05, loops = 1): seq = [] for i in range(len(images)): im = Image.open(images[i]) background = Image.new('RGB', im.size, (255,255,255)) background.paste(im, (0,0)) seq.append(background) frames = writeGif( giffile, seq, durations, loops) print frames, 'images has been merged to', giffile ## 将gif图像每一帧拆成独立的位图 def gif2images( filename, distDir = '.', type = 'bmp' ): if not os.path.exists(distDir): os.mkdir(distDir) print 'spliting', filename, im = Image.open( filename ) im.seek(0) # skip to the second frame cnt = 0 type = string.lower(type) mode = 'RGB' # image modea if type == 'bmp' or type == 'png': mode = 'P' # image mode im.convert(mode).save(distDir+'/%d.'%cnt+type ) cnt = cnt+1 try: while 1: im.seek(im.tell()+1) im.convert(mode).save(distDir+'/%d.'%cnt+type) cnt = cnt+1 except EOFError: pass # end of sequence white = (255,255,255) preIm = Image.open(distDir+'/%d.'%0+type).convert('RGB') size = preIm.size prePixs = preIm.load() for k in range (1,cnt): print '.', im = Image.open(distDir+'/%d.'%k+type).convert('RGB') pixs = im.load() for i in range(size[0]): for j in range(size[1]): if pixs[i,j] == white: pixs[i,j] = prePixs[i,j] preIm = im prePixs = preIm.load() im.convert(mode).save(distDir+'/%d.'%k+type,quality=30, optimize=True, progressive=True) print ' ', filename, 'has been splited to directory: [',distDir,']' return cnt # 总帧数 if __name__ == '__main__': frames = gif2images(os.getcwd()+'/1.gif',distDir='tmp',type='png') images = [] for i in range(frames-1,-1,-1): images.append(os.getcwd()+'/tmp/%d.png'%i) images2gif(images,'save.gif', durations = 0.05) Read More

PIL – Convert GIF Frames to JPG

http://stackoverflow.com/questions/10269099/pil-convert-gif-frames-to-jpg

import Image import sys def processImage(infile): try: im = Image.open(infile) except IOError: print "Cant load", infile sys.exit(1) i = 0 mypalette = im.getpalette() try: while 1: im.putpalette(mypalette) new_im = Image.new("RGBA", im.size) new_im.paste(im) new_im.save('foo'+str(i)+'.png') #new_im.save('foo'+str(i)+'.png',quality=30, optimize=True, progressive=True) i += 1 im.seek(im.tell() + 1) except EOFError: pass # end of sequence processImage('gif_example.gif') Read More

Permission denied: make_sock: could not bind to address

(13)Permission denied: make_sock: could not bind to address [::]:8080
(13)Permission denied: make_sock: could not bind to address 0.0.0.0:8080

check SELinux status: semanage port -l|grep http

like this:
http_cache_port_t tcp 3128, 8080, 8118, 10001-10010
http_cache_port_t udp 3130
http_port_t tcp 80, 443, 488, 8008, 8009, 8443
pegasus_http_port_t tcp 5988
pegasus_https_port_t tcp 5989 Read More

Config freeRADIUS Server to authenticate VPN server via CentOS

1.安装ppp,pptp

http://blog.pzy.me/2013/08/24/pptpd-installation-tutorial-via-centos/

2.安装freeradius

yum install mysql* freeradius* -y

3.导入数据

mysql> create database radius; mysql -u root -p radius < /etc/raddb/sql/mysql/admin.sql mysql -u root -p radius < /etc/raddb/sql/mysql/cui.sql mysql -u root -p radius < /etc/raddb/sql/mysql/nas.sql mysql -u root -p radius < /etc/raddb/sql/mysql/schema.sql mysql -u root -p radius < /etc/raddb/sql/mysql/wimax.sql Read More

101 Tips to MySQL Tuning and Optimization

转自:101 Tips to MySQL Tuning and Optimization
英文原文:101 Tips to MySQL Tuning and Optimization

MySQL是一个功能强大的开源数据库。随着越来越多的数据库驱动的应用程序,人们一直在推动MySQL发展到它的极限。这里是101条调节和优化MySQL安装的技巧。一些技巧是针对特定的安装环境的,但这些思路是通用的。我已经把他们分成几类,来帮助你掌握更多MySQL的调节和优化技巧。 Read More

pptpd Installation Tutorial via CentOS