nginx
配置中使用“$”
geo $KK {default"$" ; }
set $ss $kk ;
平滑升级
kill -USR2 8936 (master) 创建新的master worker kill -WINCH 8936 (master) 停止旧的worker kill -QUIT 8936 (master) 停止旧的master createnewworker () { masternum1=`ps -ef |grep nginx |grep master |wc -l` pidnum2=`ps -ef |grep nginx |grep master |awk '{print $2}'` if [ $masternum1 -eq 1 ];then echo "one master, update master..." kill -USR2 $pidnum2 sleep 1 kill -WINCH $pidnum2 sleep 1 kill -QUIT $pidnum2 sleep 1 echo "New worker create success" ps -ef |grep 'nginx: ' |grep -v grep elif [ $masternum1 -eq 0 ];then echo "no master, start master..." /usr/local/sms/sbin/nginx -c /usr/local/sms/conf/nginx.conf sleep 3 ps -ef |grep 'nginx: ' |grep -v grep elif [ $masternum1 -eq 2 ];then echo "two master, kill old worker..." pidnum2=`cat /usr/local/sms/logs/nginx.pid.oldbin` kill $pidnum2 sleep 1 pidnum2=`ps -ef |grep nginx |grep master |awk '{print $2}'` kill -USR2 $pidnum2 sleep 1 kill -WINCH $pidnum2 sleep 1 kill -QUIT $pidnum2 sleep 1 echo "New worker create success" ps -ef |grep 'nginx: ' |grep -v grep elif [ $masternum1 -gt 2 ];then echo "Create new worker is Faild ..........." sleep 3 ps -ef |grep 'nginx: ' |grep -v grep fi }
Download nginx.vim to ~/.vim/syntax/ add this line to ~/.vim/filetype.vim:
au BufRead ,BufNewFile /etc/nginx/*,/usr/local/nginx/conf/* if &ft == '' | setfiletype nginx | endif
内存对齐
#define ngx_align(d, a) (((d) + (a - 1)) & ~(a - 1)) //// 将 m 对其到内存对齐地址 align这是一个常见的宏\R\N
功能:把d已a为最小单位对齐。d为要操作的目标,a为对齐单位,2的n次方。\R\N
简介:((d) + (a - 1))的结果就是如果d小于a的低几位(log2 a位)部分如果不是0,就规整为a。用& ~(a - 1)操作,把低位设成0。总的结果就是,把d规整为a的倍数,不足a的1倍的部分则补足为1(类似与近似运算时的1进制)。从而保证align(d,a)的低log2 a位为零。
比如把12和2规整为8的倍数(2进制第三位对齐)。12=8+4,低3位部分4规整为8,algn(12,8)=16。24本身就是8的倍数,规整后还是24。 offsetof
#define offsetof(type,member) (size_t)&(((type *)0)->member)
访问目录文件
client_max_body_size 100m ;
client_body_buffer_size 100000k ;
location /{
autoindex on ;
autoindex_localtime on ;
autoindex_exact_size off ;
root $dir ;
}
打印body
location /test {
content_by_lua '
ngx.req.read_body()
local args = ngx.req.get_post_args()
for key, val in pairs(args) do
ngx.log(ngx.INFO,key.. val )
end
';
}
nginx打印
/*
* supported formats:
* %[0 ][width ][x ][X ]O off_t
* %[0 ][width ]T time_t
* %[0 ][width ][u ][x|X ]z ssize_t/size_ t
* %[0 ][width ][u ][x|X ]d int/u_int
* %[0 ][width ][u ][x|X ]l long
* %[0 ][width|m ][u ][x|X ]i ngx_int_ t/ngx_uint_ t
* %[0 ][width ][u ][x|X ]D int32_t/uint32_ t
* %[0 ][width ][u ][x|X ]L int64_t/uint64_ t
* %[0 ][width|m ][u ][x|X ]A ngx_atomic_ int_t/ngx_ atomic_uint_ t
* %[0 ][width ][.width ]f double, max valid number fits to %18.15f
* %P ngx_pid_ t
* %M ngx_msec_ t
* %r rlim_t
* %p void *
* %V ngx_str_t *
* %v ngx_variable_value_t *
* %s null-terminated string
* %* s length and string
* %Z '\0'
* %N '\n'
* %c char
* %% %
*
* reserved:
* %t ptrdiff_t
* %S null-terminated wchar string
* %C wchar
*/
ngx.arg [index] #ngx指令参数,当这个变量在set_by_lua或者set_by_lua_file内使用的时候是只读的,指的是在配置指令输入的参数.
ngx.var .varname #读写NGINX变量的值,最好在lua脚本里缓存变量值,避免在当前请求的生命周期内内存的泄漏
ngx.config .ngx _lua_version #当前ngx_lua模块版本号
ngx.config .nginx _version #nginx版本
ngx.worker .exiting #当前worker进程是否正在关闭
ngx.worker .pid #当前worker进程的PID
ngx.config .nginx _configure #编译时的./configure命令选项
ngx.config .prefix #编译时的prefix选项
core constans: #ngx_lua 核心常量
ngx.OK (0 )
ngx.ERROR (-1 )
ngx.AGAIN (-2 )
ngx.DONE (-4 )
ngx.DECLINED (-5 )
ngx.nil
http method constans: #经常在ngx.location.catpure和ngx.location.capture_multi方法中被调用.
ngx.HTTP _GET
ngx.HTTP _HEAD
ngx.HTTP _PUT
ngx.HTTP _POST
ngx.HTTP _DELETE
ngx.HTTP _OPTIONS
ngx.HTTP _MKCOL
ngx.HTTP _COPY
ngx.HTTP _MOVE
ngx.HTTP _PROPFIND
ngx.HTTP _PROPPATCH
ngx.HTTP _LOCK
ngx.HTTP _UNLOCK
ngx.HTTP _PATCH
ngx.HTTP _TRACE
http status constans: #http请求状态常量
ngx.HTTP _OK (200 )
ngx.HTTP _CREATED (201 )
ngx.HTTP _SPECIAL_RESPONSE (300 )
ngx.HTTP _MOVED_PERMANENTLY (301 )
ngx.HTTP _MOVED_TEMPORARILY (302 )
ngx.HTTP _SEE_OTHER (303 )
ngx.HTTP _NOT_MODIFIED (304 )
ngx.HTTP _BAD_REQUEST (400 )
ngx.HTTP _UNAUTHORIZED (401 )
ngx.HTTP _FORBIDDEN (403 )
ngx.HTTP _NOT_FOUND (404 )
ngx.HTTP _NOT_ALLOWED (405 )
ngx.HTTP _GONE (410 )
ngx.HTTP _INTERNAL_SERVER_ERROR (500 )
ngx.HTTP _METHOD_NOT_IMPLEMENTED (501 )
ngx.HTTP _SERVICE_UNAVAILABLE (503 )
ngx.HTTP _GATEWAY_TIMEOUT (504 )
Nginx log level constants: #错误日志级别常量 ,这些参数经常在ngx.log方法中被使用.
ngx.STDERR
ngx.EMERG
ngx.ALERT
ngx.CRIT
ngx.ERR
ngx.WARN
ngx.NOTICE
ngx.INFO
ngx.DEBUG
##################
#API中的方法:
##################
print() #与 ngx.print()方法有区别,print() 相当于ngx.log()
ngx.ctx #这是一个lua的table,用于保存ngx上下文的变量,在整个请求的生命周期内都有效,
ngx.location .capture () #发出一个子请求,详细用法参考官方文档。
ngx.location .capture _multi() #发出多个子请求,详细用法参考官方文档。
ngx.status #读或者写当前请求的相应状态. 必须在输出相应头之前被调用.
ngx.header .HEADER #访问或设置http header头信息,详细参考官方文档。
ngx.req .set _uri() #设置当前请求的URI,详细参考官方文档
ngx.set _uri_args(args) #根据args参数重新定义当前请求的URI参数.
ngx.req .get _uri_args() #返回一个LUA TABLE,包含当前请求的全部的URL参数
ngx.req .get _post_args() #返回一个LUA TABLE,包括所有当前请求的POST参数
ngx.req .get _headers() #返回一个包含当前请求头信息的lua table.
ngx.req .set _header() #设置当前请求头header某字段值.当前请求的子请求不会受到影响.
ngx.req .read _body() #在不阻塞ngnix其他事件的情况下同步读取客户端的body信息.[详细]
ngx.req .discard _body() #明确丢弃客户端请求的body
ngx.req .get _body_data() #以字符串的形式获得客户端的请求body内容
ngx.req .get _body_file() #当发送文件请求的时候,获得文件的名字
ngx.req .set _body_data() #设置客户端请求的BODY
ngx.req .set _body_file() #通过filename来指定当前请求的file data。
ngx.req .clear _header() #清求某个请求头
ngx.exec (uri,args) #执行内部跳转,根据uri和请求参数
ngx.redirect (uri, status) #执行301或者302的重定向。
ngx.send _headers() #发送指定的响应头
ngx.headers _sent #判断头部是否发送给客户端ngx.headers_sent=true
ngx.print (str) #发送给客户端的响应页面
ngx.say () #作用类似ngx.print,不过say方法输出后会换行
ngx.log (log.level ,...) #写入nginx日志
ngx.flush () #将缓冲区内容输出到页面(刷新响应)
ngx.exit (http-status) #结束请求并输出状态码
ngx.eof () #明确指定关闭结束输出流
ngx.escape _uri() #URI编码(本函数对逗号,不编码,而php的urlencode会编码)
ngx.unescape _uri() #uri解码
ngx.encode _args(table) #将tabel解析成url参数
ngx.decode _args(uri) #将参数字符串编码为一个table
ngx.encode _base64(str) #BASE64编码
ngx.decode _base64(str) #BASE64解码
ngx.crc 32_short(str) #字符串的crs32_short哈希
ngx.crc 32_long(str) #字符串的crs32_long哈希
ngx.hmac _sha1(str) #字符串的hmac_sha1哈希
ngx.md 5(str) #返回16进制MD5
ngx.md 5_bin(str) #返回2进制MD5
ngx.today () #返回当前日期yyyy-mm-dd
ngx.time () #返回当前时间戳
ngx.now () #返回当前时间
ngx.update _time() #刷新后返回
ngx.localtime () #返回 yyyy-mm-dd hh:ii:ss
ngx.utctime () #返回yyyy-mm-dd hh:ii:ss格式的utc时间
ngx.cookie _time(sec ) #返回用于COOKIE使用的时间
ngx.http _time(sec ) #返回可用于http header使用的时间
ngx.parse _http_time(str) #解析HTTP头的时间
ngx.is _subrequest #是否子请求(值为 true or false)
ngx.re .match (subject,regex,options,ctx) #ngx正则表达式匹配,详细参考官网
ngx.re .gmatch (subject,regex,opt) #全局正则匹配
ngx.re .sub (sub ,reg,opt) #匹配和替换(未知)
ngx.re .gsub () #未知
ngx.shared .DICT #ngx.shared.DICT是一个table 里面存储了所有的全局内存共享变量
ngx.shared .DICT .get
ngx.shared .DICT .get _stale
ngx.shared .DICT .set
ngx.shared .DICT .safe _set
ngx.shared .DICT .add
ngx.shared .DICT .safe _add
ngx.shared .DICT .replace
ngx.shared .DICT .delete
ngx.shared .DICT .incr
ngx.shared .DICT .flush _all
ngx.shared .DICT .flush _expired
ngx.shared .DICT .get _keys
ndk.set _var.DIRECTIVE #不懂
lua
url转义
ngx.escape_uri
ngx.unescape_uri
ngx.req.read_body()
local body = ngx.req.get_body_data()
if not body then
local file = ngx.req.get_body_file()
body = common_lib:read_file_data(file )
end
执行系统命令
时间
os.date("%Y -%m -%d_ %H :%M :%S " , os.time ()- 8 *3600
Lua 定义了两个常量: LUA_MININTEGER 和
LUA_MAXINTEGER 来表示这个类型可以表示的最小和最大值。
time_tab = os.date("%Y -%m -%d_ %H :%M :%S " , date + time )
date = os.time {year=year, month=month, day=day, hour=0 , min=0 , sec=0 }
请求参数
local urlargs = {}
local agrstr = ngx.encode_args(urlargs)
req_url = req_url.."?" ..agrstr
local req_args = {}
ngx_var.args = nil
ngx.req .set _uri_args(req_args)
req_url = req_url.."?" ..ngx .unescape _uri(ngx_var.args )
flv直播 回源 302
upstream tx.flv.huya.bak {
server tx.flv.huya.com :80 ;
localfirst;
keepalive 1024 ;
}
server {
listen 8080 ;
server_name tx.flv.huya.com;
proxy_intercept_errors on ;
recursive_error_pages on ;
location / {
proxy_pass http :// tx.flv.huya.bak;
proxy_set_header Host tx.flv.huya.com;
error_page 302 = @error_page_302 ;
}
location ~ /proxyto/ ([^/]+)(.*) {
proxy_pass http :// $1 $2 $is_args$query_string;
error_page 302 = @error_page_302 ;
}
location @error_page_302 {
rewrite_by_lua '
local _, _, upstream_http_location = string.find(ngx.var.upstream_http_location, "^http:/(.*)$")
ngx.header["cnkedao"] = "/proxyto" .. upstream_http_location
ngx.exec("/proxyto" .. upstream_http_location);
' ;
}
}
upstream tx.flv.huya {
server 127.0 .0 .1 :8080 ;
keepalive 1024 ;
}
server {
listen 80 ;
server_name tx.flv.huya.com;
http_billing_withname on ;
location / {
root html;
index index.html index.htm;
proxy_http_version 1.1 ;
proxy_pass http :// tx.flv.huya;
proxy_set_header Host tx.flv.huya.com;
}
location ~* \.flv$ {
http_live on ;
gop_enable on ;
gop_min 250 ;
zero_timestamp on ;
stream_type live_flv;
live_proxy_connect_timeout 10 s;
live_proxy_http_version 1.1 ;
live_proxy_ignore_client_abort off ;
live_proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504 http_403 http_404;
add_header Cache-Control no -cache;
add_header Content-Type video/x-flv;
live_proxy_pass http :// tx.flv.huya;
live_proxy_set_header Host tx.flv.huya.com;
live_proxy_set_header User-Agent SMS;
}
}
}