安装
yum -y install yum-utils
# 添加openresty yum 源
yum-config-manager --add-repo https://openresty.org/package/centos/openresty.repo
# 安装openresty
yum -y install openresty
帮助
与nginx基本是一样的
nginx version: openresty/1.17.8.2
Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]
Options:
-?,-h : this help
-v : show version and exit
-V : show version and configure options then exit
-t : test configuration and exit
-T : test configuration, dump it and exit
-q : suppress non-error messages during configuration testing
-s signal : send signal to a master process: stop, quit, reopen, reload
-p prefix : set prefix path (default: /usr/local/openresty/nginx/)
-c filename : set configuration file (default: conf/nginx.conf)
-g directives : set global directives out of configuration file
目录结构
bin COPYRIGHT luajit lualib nginx openssl111 pcre site zlib
为方便配置,我们可以在openresty下面建立自己的配置目录myconfig 以及logs存储目录.
启动openresty
需要注意的是-c指定的是你自己的配置文件的目录. 这个是一个相对路径.这个相对路径不是针对当前路径的,而是针对-p所定义的路径的.
# openresty -c ../myconf/nginx.conf
-p参数的默认值为 /usr/local/openresty/nginx/可以通过-h帮助查看.因为-c的相对路径是基于-p的,所以 …/myconf/nginx.conf就是先返回上一级
启动后没有任何提示,可以查看是否有相关进程
# ps -ef |grep nginx
root 2207 1 0 09:19 ? 00:00:00 nginx: master process openresty -c ../myconf/nginx.conf
nobody 2208 2207 0 09:19 ? 00:00:00 nginx: worker process
root 2245 1626 0 09:23 pts/0 00:00:00 grep --color=auto nginx
ps -ef是查看所有进程,这个ps用法可以参考帮助
To see every process on the system using standard syntax:
ps -e
ps -ef
ps -eF
ps -ely
To see every process on the system using BSD syntax:
ps ax
ps axu
有nginx进程说明openresty已经启动了.
或者可以用netstat命令查看相关端口号
[root openresty]# netstat -nltp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1207/master
tcp 0 0 0.0.0.0:50010 0.0.0.0:* LISTEN 1883/java
tcp 0 0 0.0.0.0:50075 0.0.0.0:* LISTEN 1883/java
tcp 0 0 0.0.0.0:50020 0.0.0.0:* LISTEN 1883/java
tcp 0 0 127.0.0.1:40777 0.0.0.0:* LISTEN 1883/java
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 2391/nginx: master
tcp 0 0 192.168.10.101:8020 0.0.0.0:* LISTEN 1778/java
tcp 0 0 192.168.10.101:50070 0.0.0.0:* LISTEN 1778/java
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 956/sshd
tcp6 0 0 ::1:25 :::* LISTEN 1207/master
tcp6 0 0 :::3306 :::* LISTEN 1027/mysqld
tcp6 0 0 :::22 :::* LISTEN 956/sshd
关闭
openresty -s stop
再次查看,没有相关进程,说明已关闭
]# ps -ef |grep nginx
root 2365 1626 0 09:32 pts/0 00:00:00 grep --color=auto nginx
nginx嵌入lua脚本
默认的location块内容为
location / {
root html;
index index.html index.htm;
}
修改为
location /lua {
set $test "hello, world.";
content_by_lua '
ngx.header.content_type = "text/plain";
ngx.say(ngx.var.test);
';
}
重新启动opensty
# openresty -c ../myconf/nginx.conf -s reload
然后再浏览器中访问
localhost/lua
结果如下
其中set是命令, $test是变量名 “hello world” 是变量值.
content_by_lua(_file)
语法:content_by_lua <lua-script-str>
适用上下文:location、location if
其中content_by_lua是指令, 后面单引号中的内容是lua脚本.通过这个指令,可以由lua直接确定nginx响应页面的正文。
ngx.header.content_type = “text/plain”; 是什么鬼,看下帮助
设置文件相应头的格式,就是返回网页的格式content_type为"text/plain"
ngx.header.HEADER
syntax: ngx.header.HEADER = VALUE
syntax: value = ngx.header.HEADER
context: rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*
Set, add to, or clear the current request's HEADER response header that is to be sent.
Underscores (_) in the header names will be replaced by hyphens (-) by default. This transformation can be turned off via the lua_transform_underscores_in_response_headers directive.
The header names are matched case-insensitively.
-- equivalent to ngx.header["Content-Type"] = 'text/plain'
ngx.header.content_type = 'text/plain';
ngx.header["X-My-Header"] = 'blah blah';
ngx.say(ngx.var.test); 其中ngx.say代表打印.
ngx.var.test 代表获取变量test的值
ngx.print
location /lua {
content_by_lua '
local a=5
ngx.header.content_type = "text/plain";
ngx.print(a);
';
}
结果会在网页上输出5
ngx.say
location /lua {
content_by_lua '
local a=6
ngx.header.content_type = "text/plain";
ngx.say(a);
';
}
结果会在网页上打印6
ngx.now
location /lua {
content_by_lua '
ngx.header.content_type = "text/plain";
ngx.print(ngx.now());
';
}
结果在网页中输出
ngx.arg
语法
语法:set_by_lua $res <lua-script-str> [$arg1 $arg2 ...]
example 1
location /lua {
set $a 32;
set $b 56;
set_by_lua $sum 'return ngx.arg[1] +ngx.arg[2]' $a $b;
echo "sum=${sum}";
}
输出内容 sum=88
解释
set_by_lua
代表一个指令$re
变量re,用于保存lua script的执行结果,或者说返回值'return ngx.arg[1] +ngx.arg[2]'
为lua script$a $b
向lua script传递变量echo "sum=${sum}"
代表向w网页输出内容
example2
如下$re
是个变量.
通过在set_by_lua
指令外面加一个参数$a
我们把变量a传递给了lua,让lua进行处理.lua把变量a和和lua内部变量s合并后返回.返回给变量$re
. set_by_lua
指令 的作用就是有一些东西需要经过lua处理,所以要给lua传一些参数,然后最后lua再把结果返回.就可以用在其他地方了.
location /lua {
set $a 32;
set_by_lua $re '
local s="zhangsan"
return ngx.arg[1]..s;
'
$a;
echo $re;
}
输出结果32zhangsan
其他方法
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() #在不阻塞nginx其他事件的情况下同步读取客户端的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.clear_header() #请求某个请求头
ngx.exec(uri, args) #执行内部跳转,根据uri和请求参数
ngx.redirect(uri, args) #执行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的uriencode会编码)
ngx.unescape_uri() #将uri解码
ngx.encode_args(table) #将table解析成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.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(src) #返回可用于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