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

编写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. ios下表单disabled样式重置

    在做最近的一个活动项目时,需要用到表单的disabled状态,但是在IOS下那颜色不是一般的浅,就跟没有一样,一开始通过如下样式重置: input:disabled, input[disabled]{ ...

  2. linux 跳过登陆修改用户密码

      2017-02-11 20:41 6人阅读 评论(0) 收藏 编辑 删除  分类: Linux 版权声明:本文为博主原创文章,未经博主允许不得转载. Linux 系统默认的是有0 1 2  3   ...

  3. 学习笔记:MDN的CSS

    HTML用于定义内容的结构和语义,CSS用于设计风格和布局. CSS规则由选择器和声明块组成:声明由属性(properties)和属性值组成. CSS介绍: 盒=框=box,边界=border,内边距 ...

  4. String.format()的用法

    string.format()用法 2011-06-21 14:58:57|  分类: 工作笔记 |  标签:string  format用法   |字号大中小 订阅 1.格式化货币(跟系统的环境有关 ...

  5. mybatis-映射器的CRUD

    设计步骤:model.mapper.dao.service.junit单元测试.log4j日志 项目和之前的一样在此只是创建了test和修改了mapper 1.修改映射 1.1修改接口 package ...

  6. HDU 1712 ACboy needs your help AC男需要你的帮助 (分组的背包)

    分组背包问题:有N件物品和一个容量为V的背包.第i件物品的体积是c[i],价值是w[i].这些物品被划分为若干组,每组中的物品互相冲突,最多选一件.求解将哪些物品装入背包可使这些物品的体积总和不超过背 ...

  7. Python开发第三篇

    函数 一.函数参数传值 形参:函数在定义的时候给定的参数 实参:函数在运行时赋给的参数: def func(i):#i为定义时的参数,为形参 pass func(name)#name为运行时的参数,为 ...

  8. 注册Windows service及其相关

    注册Windows service,.net写的 net stop "xxxxxx""%SYSTEMROOT%\Microsoft.NET\Framework\v2.0. ...

  9. 反转链表[剑指offer]之python实现

    输入一个链表,输出反转后的链表. 非递归实现: # -*- coding:utf-8 -*- # class ListNode: # def __init__(self, x): # self.val ...

  10. springmvc如何获取参数

    请参考这篇文章, 写得比较全面. https://www.cnblogs.com/xiaoxi/p/5695783.html