Nginx、lua、openresty

Lua精简高效的特点加上Nginx的强大,可以包容到lua语言,读取nginx的各种环境变量。
开发nginx 的插件的时候不需要使用c语言了

openResty

基于nginx+lua开发的开源产品。将lua脚本整合到了nginx当中。
通过lua扩展nginx实现可伸缩的web平台。

openResty目录结构

lualib:lua相关源码
luajit:运行环境
nginx : 原有的目录内容

lua热部署配置

只能是外部lua文件才能热加载

  1. server{
  2. lua_code_cache:off;
  3. }

nginx执行lua脚本的方法

直接在location中写lua脚本语句

  1. location /lua{
  2. default_type text/html;
  3. content_by_lua 'ngx.say("<b>hello world!</b><br>hhh")';
  4. }

或者使用content_by_lua_block

  1. location /lua{
  2. default_type text/html;
  3. content_by_lua_block {
  4. ngx.say("<b>hello world!</b><br>hhh")
  5. }
  6. }

加载lua脚本文件执行
conf目录下创建001.lua文件

  1. location /lua{
  2. default_type text/html;
  3. content_by_lua_file conf/001.lua ;
  4. }

将所有lua配置都引用外部文件
新增lua.conf文件,conf目录里面添加content_by_lua_file

  1. location /lua{
  2. default_type text/html;
  3. include lua.conf;
  4. }

或者,

  1. include lua.conf; #在lua.conf中写location

lua获取参数
ngx.var

  1. location /lua{
  2. default_type text/html;
  3. content_by_lua_block {
  4. ngx.say(ngx.var.arg_a) #其中的a就是传输的参数
  5. }
  6. }

取到所有的变量

image.png
获取请求头的信息
image.png