nginx lua 笔记
来源:互联网 发布:vava黑历史知乎 编辑:程序博客网 时间:2024/06/14 04:09
nginx
配置中使用“$”
- geo http模块
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
}
nginx.vim
- 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
- 用途:一般用于内存、数据对齐。\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; #默认off GMT时间。on为服务器时间 autoindex_exact_size off; #默认on 详细大小,off单位k或者M 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 api 详细api
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.crc32_short(str) #字符串的crs32_short哈希 ngx.crc32_long(str) #字符串的crs32_long哈希 ngx.hmac_sha1(str) #字符串的hmac_sha1哈希 ngx.md5(str) #返回16进制MD5 ngx.md5_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_uringx.unescape_uringx.req.read_body()local body = ngx.req.get_body_data()--local header = ngx.req.get_headers()if not body then local file = ngx.req.get_body_file() body = common_lib:read_file_data(file)end
执行系统命令
- os.execute(“mkdir dir”)
时间
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 = nilngx.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 10s; 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; } }}
阅读全文
0 0
- nginx lua 笔记
- NGINX+Lua 笔记 (陆续补充,待整理)
- 配置nginx+lua开发环境笔记(2)
- nginx + lua
- nginx lua
- Nginx Lua
- nginx lua
- Nginx+Lua
- nginx lua
- nginx+lua
- nginx lua
- nginx lua
- nginx lua
- Nginx模块-lua-nginx-module
- 【nginx】nginx介绍-集成lua
- [lua]Lua笔记
- [译]NGINX和LUA
- Nginx与Lua
- apicloud 日历弹框滑动时body顶部滑动问题 及mui dtpicker的使用方法
- 剑指offer面试题24 二叉搜索树的后序遍历序列
- MyEclipse如何连接SQL SERVER 2012?
- 动画(重要)
- sqoop从关系型数据库导数据到hbase
- nginx lua 笔记
- CSS样式
- 常用命令
- AVMetadataObject类解析二维码
- 4、表达式
- WOJ1213-Area
- Detour工具包使用
- TC358743XBG/TC358749XBG(\w Scaler):HDMI转MIPI CSI东芝转换芯片
- HDU 6058 Kanade's sum(思维)