其实有点类似WampServer一站式安装包

wget http://openresty.org/download/ngx_openresty-1.7.10.1.tar.gz
tar -zxvf ngx_openresty-1.7.10.1.tar.gz
cd ngx_openresty-1.7.10.1
./configure
make && make install

configure之前需要安装的相关组件

yum -y install pcre-devel
yum -y install gcc zlib zlib-devel openssl openssl-devel

安装目录默认会在

cd /usr/local/openresty/

测试lua环境是否正常

lua
print("hello")

测试nginx+lua

cd /usr/local/openresty/nginx/conf

在nginx.conf里添加测试内容

location /hello {
       default_type 'text/plain';
       content_by_lua 'ngx.say("ok")';
       }

location /say {
default_type text/html;
content_by_lua '
ngx.say("<p>hello, world</p>")
ngx.say(tostring(ngx.var.remote_addr),"<br/>")
ngx.say(tostring(ngx.var.arg_name),"<br/>")
';
}

 

启动nginx

sbin/nginx

测试:

[root@k2 nginx]# curl 'http://localhost/hello'
ok

浏览器访问:

测试3:

    location ~ /lua_request/(\d+)/(\d+) {
        #设置nginx变量
        ;
        set $b $host;
        default_type "text/html";
        #nginx内容处理
        content_by_lua_file /test/test_request.lua;
        #内容体处理完成后调用
        echo_after_body "ngx.var.b $b";
    }

test_request.lua

--nginx变量
local var = ngx.var
ngx.say("ngx.var.a : ", var.a, "<br/>")
ngx.say("ngx.var.b : ", var.b, "<br/>")
ngx.say(], "<br/>")
ngx.;

ngx.say("<br/>")

--请求头
local headers = ngx.req.get_headers()
ngx.say("headers begin", "<br/>")
ngx.say("Host : ", headers["Host"], "<br/>")
ngx.say("user-agent : ", headers["user-agent"], "<br/>")
ngx.say("user-agent : ", headers.user_agent, "<br/>")
for k,v in pairs(headers) do
    if type(v) == "table" then
        ngx.say(k, " : ", table.concat(v, ","), "<br/>")
    else
        ngx.say(k, " : ", v, "<br/>")
    end
end
ngx.say("headers end", "<br/>")
ngx.say("<br/>")

--get请求uri参数
ngx.say("uri args begin", "<br/>")
local uri_args = ngx.req.get_uri_args()
for k, v in pairs(uri_args) do
    if type(v) == "table" then
        ngx.say(k, " : ", table.concat(v, ", "), "<br/>")
    else
        ngx.say(k, ": ", v, "<br/>")
    end
end
ngx.say("uri args end", "<br/>")
ngx.say("<br/>")

--post请求参数
ngx.req.read_body()
ngx.say("post args begin", "<br/>")
local post_args = ngx.req.get_post_args()
for k, v in pairs(post_args) do
    if type(v) == "table" then
        ngx.say(k, " : ", table.concat(v, ", "), "<br/>")
    else
        ngx.say(k, ": ", v, "<br/>")
    end
end
ngx.say("post args end", "<br/>")
ngx.say("<br/>")

--请求的http协议版本
ngx.say("ngx.req.http_version : ", ngx.req.http_version(), "<br/>")
--请求方法
ngx.say("ngx.req.get_method : ", ngx.req.get_method(), "<br/>")
--原始的请求头内容
ngx.say("ngx.req.raw_header : ",  ngx.req.raw_header(), "<br/>")
--请求的body内容体
ngx.say("ngx.req.get_body_data() : ", ngx.req.get_body_data(), "<br/>")
ngx.say("<br/>")

脚本测试

wget --post-data 'a=1&b=2' 'http://127.0.0.1/lua_request/1/2?a=3&b=4' -O -&nbsp;

访问:

http://192.168.1.120/lua_request/1/2?a=3&b=4

参考:

http://openresty.org/

https://m.oschina.net/blog/344540

http://jinnianshilongnian.iteye.com/blog/2186448

最新文章

  1. 基于TCP的网络编程
  2. hdu 1503, LCS variants, find a LCS, not just the length, backtrack to find LCS, no extra markup 分类: hdoj 2015-07-18 16:24 139人阅读 评论(0) 收藏
  3. 【LeetCode OJ】Construct Binary Tree from Preorder and Inorder Traversal
  4. 性能测试工具 转自https://yq.aliyun.com/articles/35149?spm=5176.100239.blogcont35147.8.rsow6k
  5. 背水一战 Windows 10 (1) - C# 6.0 新特性
  6. 转载 linux内核 asmlinkage宏
  7. [CareerCup] 8.8 Othello Game 黑白棋游戏
  8. Android+OpenCV 摄像头实时识别模板图像并跟踪
  9. 关于T-SQL重编译那点事,WITH RECOMPILE和OPTION(RECOMPILE)区别仅仅是存储过程级重编译和SQL语句级重编译吗
  10. css 兼容 position:fixed
  11. Chatwin商务通
  12. javascript 中 &quot;!function(){}() &quot; 是什么意思?
  13. iOS 圆角那些事(转)
  14. 创建简单的Python列表
  15. UnicodeEncodeError: &#39;gbk&#39; codec can&#39;t encode character &#39;\xa0&#39; in position 1987: illegal multibyte sequence
  16. Java新知识系列 四
  17. excel 删除重复项
  18. Elasticsearch 性能监控基础【转】
  19. Python教程:进击机器学习(五)--Scipy《转》
  20. winhex十六进制常用快捷键

热门文章

  1. PHP获取IP地址
  2. rest api设计的一般原则
  3. ZJOIDay2T1 BB题解
  4. 插入备份数据 报 IDENTITY_INSERT 为 ON 时解决方法
  5. iOS开源App整理
  6. IDEA Error:java: 未结束的字符串文字
  7. javax.imageio.IIOException: Can&#39;t create cache file!
  8. centos7精简版(minimal)killall: command not found
  9. Qt字符转换
  10. ABAP 内表的行列转换-NEW