Nginx-openresty+mysql+redis使用篇(一)

来源:互联网 发布:docker swarm 网络 编辑:程序博客网 时间:2024/06/10 18:58

Nginx-openresty+mysql+redis使用篇(一)


资料参考:

https://github.com/openresty/lua-nginx-module

注:这不是一份全面的资料,全面的资料可以访问上面官方的 github 进行查找,本文只是个人在使用中需要用到的特性

几个常用的配置

lua_code_cache 默认为 on,lua 脚本会被加载到内存中,修改 lua 文件,需要重新加载配置,生产环境一般使用该配置。调试阶段可以关闭该选项,系统每次执行 lua 脚本时,都会重新去加载 lua 脚本文件,关闭方式如下:

lua_code_cache off;

content_by_lua 和 content_by_lua_file,在 Nginx 的 NGX_HTTP_CONTENT_PHASE 执行的 lua 脚本

content_by_lua_file html/lua/test.lua;

以上的配置方式,系统会在 ~/nginx/html/lua 目录下搜索 test.lua 文件,与其类似的配置还有:

  • rewrite_by_lua 和 rewrite_by_lua_file
  • access_by_lua 和 access_by_lua_file

header_filter_by_lua 和 header_filter_by_lua_file,在 Nginx filter_header 阶段执行的 lua 脚本

header_filter_by_lua_file  html/lua/filter_header.lua;

与其类似的配置还有 body_filter_by_lua 和 body_filter_by_lua_file

常量

系统常量

ngx.OK (0)ngx.ERROR (-1)ngx.AGAIN (-2)ngx.DONE (-4)ngx.DECLINED (-5)

HTTP方法常量

ngx.HTTP_GETngx.HTTP_HEADngx.HTTP_PUTngx.HTTP_POSTngx.HTTP_DELETEngx.HTTP_OPTIONSngx.HTTP_MKCOLngx.HTTP_COPYngx.HTTP_MOVEngx.HTTP_PROPFINDngx.HTTP_PROPPATCHngx.HTTP_LOCKngx.HTTP_UNLOCKngx.HTTP_PATCHngx.HTTP_TRACE

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)

ngx.log会使用这部分变量

变量

ngx.var.VAR_NAME

此处的 VAR_NAME 可以为NGINX内置变量,也可以为配置文件中的变量,如(这里没有全部列举):

NGINX HTTP核心模块内置变量

schemecontent_lengthcontent_typehostremote_addrremote_port

如配置文件:

location /lua {    default_type "text/plain";    set $my_var "my_var";    content_by_lua_file html/lua/test.lua;    header_filter_by_lua_file  html/lua/filter_header.lua;}

Lua脚本示例:

test.lua

ngx.say("my_var: "..ngx.var.my_var)ngx.say("http_host: "..ngx.var.http_host)ngx.say("uri: "..ngx.var.uri)ngx.say("args: "..ngx.var.args)

filter_header.lua

ngx.status = 500

测试结果:

[www@123 ~]$ curl -i "http://127.0.0.1:8080/lua?a=1&b=2"HTTP/1.1 500 Internal Server ErrorServer: openresty/1.9.3.1Date: Mon, 26 Oct 2015 12:50:44 GMTContent-Type: text/plainTransfer-Encoding: chunkedConnection: keep-alivemy_var: my_varhttp_host: 127.0.0.1:8080uri: /luaargs: a=1&b=2

ngx.ctx

可以将变量作为上下文,跨域NGINX多个阶段的脚本进行使用

配置文件:

location /luatest {    rewrite_by_lua_file html/lua/phase.lua;    access_by_lua_file  html/lua/phase.lua;    content_by_lua_file html/lua/phase.lua;}

Lua脚本示例:

if ('rewrite' == ngx.get_phase()) then    ngx.ctx.foo = {}endtable.insert(ngx.ctx.foo, ngx.get_phase())if ('content' == ngx.get_phase()) then    for k, v in pairs(ngx.ctx.foo) do        ngx.say(k..": "..v)    endend

测试结果

[www@123 ~]$ curl -i "http://127.0.0.1:8080/luatest"HTTP/1.1 200 OKServer: openresty/1.9.3.1Date: Mon, 26 Oct 2015 12:55:31 GMTContent-Type: application/octet-streamTransfer-Encoding: chunkedConnection: keep-alive1: rewrite2: access3: content

一次执行,分别在 rewrite 阶段,access 阶段和 content 阶段,向 ctx 变量中插入相应的阶段值

ngx.shared.DICT

可作为内置共享变量,该变量为一个 table,需要在配置中指定变量为共享变量,注意:lua_shared_dict 只能配置在 http 下

配置文件(省略了无关部分):

http {    lua_shared_dict pv 32m;    server {        listen       80;        server_name  localhost;        location /lua1 {            lua_code_cache off;            content_by_lua_file html/lua/test1.lua;        }        location /lua2 {            lua_code_cache off;            content_by_lua_file html/lua/test2.lua;        }    }}

Lua脚本:

test1.lua

ngx.shared.pv:set("count", 100)ngx.say('set pv count to 100')

test2.lua

ngx.say('get pv count '..ngx.shared.pv:get("count"))

测试结果:

[www@123 conf]$ curl "http://127.0.0.1:8080/lua1"set pv count to 100[www@123 conf]$ curl "http://127.0.0.1:8080/lua2"get pv count 100

访问lua1时,将 pv 的 count 设置为 100,访问 lua2 的时候将该值取出,该方法可以实现变量在系统中的共享

使用共享变量,不能直接使用,需要使用共享变量接口对共享变量中的值进行处理,具体参见:
https://github.com/openresty/lua-nginx-module#ngxshareddict

ngx.status

处理返回的响应,可以直接对 ngx.status 赋值

Lua脚本示例:

ngx.status = ngx.HTTP_INTERNAL_SERVER_ERRORngx.say("Hello World")

测试结果:

[www@123 ~]$ curl -i "http://127.0.0.1:8080/lua"HTTP/1.1 500 Internal Server ErrorServer: openresty/1.9.3.1Date: Mon, 26 Oct 2015 13:00:53 GMTContent-Type: text/plainTransfer-Encoding: chunkedConnection: keep-aliveHello World

ngx.header.HEADER

对头的处理有以下两种方式:

ngx.header.content_type = "text/plain"ngx.header["Content-Type"] = "text/plain"

如果要删除头有以下两种方式:

ngx.header["Content-Type"] = nilngx.header["Content-Type"] = {}

Lua脚本:

ngx.header.content_type = "test"ngx.header.test = "haha"ngx.header.server = nilngx.say("Hello World")

测试结果:

[www@123 ~]$ curl -i "http://127.0.0.1:8080/lua"HTTP/1.1 200 OKDate: Mon, 26 Oct 2015 13:04:09 GMTContent-Type: testTransfer-Encoding: chunkedConnection: keep-alivetest: hahaHello World

从测试结果可以看出,Content-Type 被设置为了 test,新加了一个 test 头值为 haha,Server 头被删除

使用日志

openresty 日志接口为

ngx.log(level, str)

level 选值如下:

  • ngx.STDERR
  • ngx.EMERG
  • ngx.ALERT
  • ngx.CRIT
  • ngx.ERR
  • ngx.WARN
  • ngx.NOTICE
  • ngx.INFO
  • ngx.DEBUG

lua脚本:

ngx.log(ngx.WARN, "this ia openresty test!!!!!")

日志输出结果如下:

2015/10/26 21:52:21 [warn] 6156#0: *87 [lua] test.lua:1: this ia openresty test!!!!!, client: 127.0.0.1, server: localhost, request: "GET /lua HTTP/1.1", host: "127.0.0.1:8080"
0 0