写一个小例子--输出随机字符串

编写nginx配置文件

        location /random {
content_by_lua_file /usr/local/openresty/nginx/conf/lua/random.lua;
}

编写random.lua文件

local args = ngx.req.get_uri_args()
local salt = args.salt
if not salt then
ngx.exit(ngx.HTTP_BAD_REQUEST)
end local string = ngx.md5(ngx.time() .. salt)
ngx.say(string)

检查语法

./nginx -t -c /usr/local/openresty/nginx/conf/nginx.conf

重启nginx

./nginx -s reload -c /usr/local/openresty/nginx/conf/nginx.conf

测试一下

curl 127.0.0.1/random?salt=123                              

成功输出了随机字符串

ea14f7a36e29925f3e9e318128f989f7

ngx lua API--post请求获取并输出userAgent

1. 编写userAgent.lua文件

local json = require "cjson"

ngx.req.read_body()
local args = ngx.req.get_post_args() if not args or not args.info then
ngx.exit(ngx_HTTP_BAD_REQUEST)
end local client_ip = ngx.var.remote_addr
local user_agent = ngx.req.get_headers()['user-agent'] or ''
local info = ngx.decode_base64(args.info) local response = {}
response.info = info
response.ip = client_ip
response.user_agent = user_agent ngx.say(json.encode(response))

2. 修改nginx配置文件

location /agent {
content_by_lua_file /usr/local/openresty/nginx/conf/lua/userAgent.lua;
}

3. 检查配置语法并重启nginx

[root@VM_0_10_centos sbin]# ./nginx -t -c /usr/local/openresty/nginx/conf/nginx.conf
nginx: the configuration file /usr/local/openresty/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/openresty/nginx/conf/nginx.conf test is successful
[root@VM_0_10_centos sbin]# ./nginx -s reload -c /usr/local/openresty/nginx/conf/nginx.conf

4. 使用curl进行测试

[root@VM_0_10_centos sbin]# curl -i --data 'info=addgfdgjkljdfgjkl65632dsgds' 127.0.0.1/agent
HTTP/1.1 OK
Server: openresty/1.13.6.1
Date: Sun, Sep :: GMT
Content-Type: application/octet-stream
Transfer-Encoding: chunked
Connection: keep-alive {"info":"i#\b䗮zip":"127.0.0.1","user_agent":"curl\/7.61.0"}

连接数据库--操作MySQL

操作Redis

OpenResty入门的更多相关文章

  1. openresty入门文章(笔者自用)

    推荐好的openresty入门介绍文章:https://www.cnblogs.com/digdeep/p/4859575.html  

  2. openresty入门12 openresty php 整合

    利用 openresty 的 drizzle-nginx-module模块 读取数据 传递到  php后端 利用到  openresty 的并发,无阻塞,mysql连接池,memcache|redis ...

  3. OpenResty入门之使用Lua扩展Nginx

    记住一点:nginx配置文件很多坑来源自你的空格少了或多了. 1.Centos下载安装 如果你的系统是 Centos 或 RedHat 可以使用以下命令: yum install readline-d ...

  4. OpenResty入门之使用Lua开发Nginx插件

    记住一点:nginx配置文件很多坑来源自你的空格少了或多了. OpenResty OpenResty 是一个基于 Nginx 与 Lua 的高性能 Web 平台,其内部集成了大量精良的 Lua 库.第 ...

  5. 《用OpenResty搭建高性能服务端》笔记

    概要 <用OpenResty搭建高性能服务端>是OpenResty系列课程中的入门课程,主讲人:温铭老师.课程分为10个章节,侧重于OpenResty的基本概念和主要特点的介绍,包括它的指 ...

  6. Lua脚本语言快速入门手册

    学了两天Lua语言,感叹其短小精悍,上手极快,语法还很舒服,不错!整理下学习过程中经常用到的基础知识,共勉! Lua用法简述 Lua语言是在1993年由巴西一个大学研究小组发明,其设计目标是作为嵌入式 ...

  7. OpenResty 社区王院生:APISIX 的高性能实践

    2019 年 7 月 6 日,OpenResty 社区联合又拍云,举办 OpenResty × Open Talk 全国巡回沙龙·上海站,OpenResty 软件基金会联合创始人王院生在活动上做了&l ...

  8. OpenResty学习指南(一)

    我的博客: https://www.luozhiyun.com/archives/217 想要学好 OpenResty,你必须理解下面 8 个重点: 同步非阻塞的编程模式: 不同阶段的作用: LuaJ ...

  9. OpenResty搭建高性能服务端

    OpenResty搭建高性能服务端   Socket编程 Linux Socket编程领域为了处理大量连接请求场景,需要使用非阻塞I/O和复用,select.poll.epoll是Linux API提 ...

随机推荐

  1. Windows8 64位运行Silverlight程序不能访问WCF的解决方案

    公司的项目是Silverlight+WCF,而我的本本是Win8 64位系统,一直无法正常运行Silverlight程序,一个同事找到了方案,现分享出来 一种情况是,Vs2010运行程序时,报无法加载 ...

  2. 创见VR-上海,会后总结

    第一次,参加这种VR会,感觉不错.上午突然发现自己之前的一款AR Demo下载量在10-50了,真没想到,虽然这款Demo有一处bug至今未修复 ^^.不过,看来现在AR/VR确实恨火. ZSpace ...

  3. 报错:Program "sh" not found in PATH

    参考原文:http://vin-mail.blog.163.com/blog/static/37895280201211932919513/ 如果你按照我的方法 先配置了cygwin的环境变量,在打开 ...

  4. tf warning等级

    from:http://blog.csdn.net/tsinghuahui/article/details/72938764 tf讨厌的warning 2017-08-03 10:02:52.0990 ...

  5. c++ STL deque容器成员函数

    deque是双向队列,即可以在头部插入删除,也可以在尾部插入删除.内部并不连续,这一点和vector并不一样.可能第1个元素和第2个元素的地址是不连在一起的.在使用时用it迭代器会安全一点. 这是c+ ...

  6. byte[] 中需要除去的特定 byte

    /// <summary> /// 去掉byte[]中特定的byte /// </summary> /// <param name="SourceByteArr ...

  7. getline()读入一整行

    string line; getline(cin, line); cin不能读入空行,用getline可以读入空行.

  8. 2018.10.26 NOIP2018模拟赛 解题报告

    得分: \(0+10+10=20\)(\(T1\)死于假题面,\(T3\)死于细节... ...) \(P.S.\)由于原题是图片,所以我没有上传题目描述,只有数据. \(T1\):颜料大乱斗(点此看 ...

  9. 2018.6.16 PHP小实验

    PHP实验 实验一 <?php /** * Created by PhpStorm. * User: qichunlin * Date: 2018/5/17 * Time: 下午5:35 */ ...

  10. PHP开发框架流行度排名:Laravel居首

    摘要:在PHP开发中,选择合适的框架有助于加快软件开发,节约宝贵的项目时间,让开发者专注于功能的实现上.Sitepoint网站做了一个小的调查,结果显示最流行的PHP框架前三甲为:Laravel.Ph ...