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

编写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. webapp一些样式记录

    图片外面的div设置宽高自适应width: 100vw; max-width: 640px; display: block; height: 43.75vw; max-height: 280px; f ...

  2. unity中的动画制作方法

    Unity中的动画制作方法 1.DOTween DoTween在5.0版本中已经用到了,到官网下载好插件之后,然后通过在项目中导入头using DG.Tweening;即可. 一些常用的API函数 D ...

  3. unhandled event loop exception解决方案

    今天突然遇到这个问题,打开ADT就报unhandled event loop exception, 原因是ATI显卡的HydraDM.exe HydraDM64.exe进程somehow跟ADT起了冲 ...

  4. 解决Android Studio和Android SDK Manager无法在线更新的问题[转]

    升级时提示 Connection failed. Please check your network connection and try again 修改安装目录下bin\studio.exe.vm ...

  5. 域名设置A记录或CNAME记录,但无法被解析,可能是因为状态为:clientHold

    解决方案: 访问https://whois.aliyun.com/查询域名状态是否为“注册商禁止解析”: 若是,联系注册商根据对方要求进行操作以便解除. https://icann.org/epp#c ...

  6. firefox 提示 ssl_error_unsupported_version 的解决方法

    访问一些HTTPS网站时尤其是国内网站 中文提示: 无法安全地连接 Firefox 无法保证您在 sx.ac.10086.cn 上的数据安全性,因为它使用 SSLv3,一个目前安全性欠佳的安全协议.专 ...

  7. Java分代的思考

    我们都知道,java的gc是基于java的分代前提,不管是CMS还是G1,都是基于分代思想:老年代和新生代 思考一:为什么可以分代? 1.java中对象的生命周期是不一样的,在gc中对应年龄的概念. ...

  8. Handling Exceptions

    https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/Exceptions/Tasks/Handling ...

  9. 从暴力匹配到KMP算法

    前言 现在有两个字符串:\(s1\)和\(s2\),现在要你输出\(s2\)在\(s1\)当中每一次出现的位置,你会怎么做? 暴力匹配算法 基本思路 用两个指针分别指向当前匹配到的位置,并对当前状态进 ...

  10. 使ListView控件中的选择项高亮显示

    实现效果: 知识运用: ListView控件的SelectedItems属性 //获取在ListView控件中被选中数据项的集合 public ListView.SelectedListViewIte ...