🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
[TOC] ## 概述 OpenResty®是一个基于[Nginx](https://openresty.org/cn/nginx.html "Nginx")与 Lua 的高性能 Web 平台,其内部集成了大量精良的 Lua 库、第三方模块以及大多数的依赖项。用于方便地搭建能够处理超高并发、扩展性极高的动态 Web 应用、Web 服务和动态网关。 OpenResty®通过汇聚各种设计精良的[Nginx](https://openresty.org/cn/nginx.html "Nginx")模块(主要由 OpenResty 团队自主开发),从而将[Nginx](https://openresty.org/cn/nginx.html "Nginx")有效地变成一个强大的通用 Web 应用平台。这样,Web 开发人员和系统工程师可以使用 Lua 脚本语言调动[Nginx](https://openresty.org/cn/nginx.html "Nginx")支持的各种 C 以及 Lua 模块,快速构造出足以胜任 10K 乃至 1000K 以上单机并发连接的高性能 Web 应用系统。 OpenResty®的目标是让你的Web服务直接跑在[Nginx](https://openresty.org/cn/nginx.html "Nginx")服务内部,充分利用[Nginx](https://openresty.org/cn/nginx.html "Nginx")的非阻塞 I/O 模型,不仅仅对 HTTP 客户端请求,甚至于对远程后端诸如 MySQL、PostgreSQL、Memcached 以及 Redis 等都进行一致的高性能响应。 ## 我的目的 项目中有个用到http请求,我用shell脚本+curl简单实现了一下.但是我想用一个我不熟悉的语言来做该功能, 在网上搜索到可以通过lua来实现我的需求.. 于是我找到了OpenResty框架 . lua发送http请求一般有两种方式, 使用 socket.http 或者 resty.http ## 环境&安装 OpenResty 参考:https://openresty.org/cn/linux-packages.html 环境:Ubuntu 安装 ``` $ sudo apt-get -y install --no-install-recommends wget gnupg ca-certificates $ echo "deb http://openresty.org/package/ubuntu $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/openresty.list $ sudo apt-get update $ sudo apt-get -y install openresty ``` 设置环境变量 ``` $ sudo vim ~/.bashrc /usr/local/openresty/nginx/sbin/ $ source ~/.bashrc ``` ## 开始第一个程序 创建项目目录 ``` mkdir ~/openrestry cd ~/openrestry mkdir logs/ conf/ ``` 写配置文件 ``` worker_processes 1; error_log logs/error.log; events { worker_connections 1024; } http { server { listen 8080; location / { default_type text/html; content_by_lua_block { ngx.say("<p>hello, world</p>") } } } } ``` 启动 ``` nginx -p `pwd`/ -c conf/nginx.conf ``` 验证是否成功 ``` curl http://localhost:8080/ ``` 返回结果` <p>hello, world </p>` ## OpenResty使用lua脚本写http 请求 lua脚本可以写在 conf/nginx.conf配置文件的 localtion 中.也可以写在单独的lua脚本文件中.需加上配置参数 `content_by_lua_file` ``` worker_processes 1; error_log logs/error.log; events { worker_connections 1024; } http { server { listen 8080; location / { default_type text/html; content_by_lua_file hello.lua; } } } ``` ### Get请求 hello.lua脚本 ``` //hello.lua local http = require('resty.http') local httpc = http.new() local res, err = httpc:request_uri('http://localhost/hello', { keepalive_timeout = 2000 -- 毫秒 }) if not res or res.status then ngx.log(ngx.ERR, "request error#", err) return end ngx.log(ngx.ERR, "request status#", res.status) ngx.say(res.body) ``` ### Post请求 hello.lua脚本 ``` //hello.lua local http = require "resty.http" local httpc = http.new() local res, err = httpc:request_uri("http://localhost/hello", { method = "POST", body = "a=1&b=2", headers = { ["Content-Type"] = "application/x-www-form-urlencoded", }, keepalive_timeout = 60, keepalive_pool = 10 }) if not res or res.status then ngx.log(ngx.ERR, "request error#", err) return end ngx.log(ngx.ERR, "request status#", res.status) ngx.say(res.body) ``` ### 重启nginx ``` nginx -p `pwd`/ -s reload ``` ### 验证 ``` curl http://localhost:8080 ``` ## 常用方法 创建`HTTP`请求对象,失败返回`nil`和错误信息。 ``` httpc = http.new() ``` 发送请求,填入请求链接和参数。 ``` res, err = httpc:request_uri(uri, params) ``` 设置请求超时时间,单位:毫秒。 ``` httpc:set_timeout(time) ``` ## 遇到的问题 ### OpenResty返回中文乱码问题 #### 描述 接口中使用`ngx.say(json.encode)`编码`json`字符串,返回中文乱码。 ``` {"msg":"请求成功","code":0}` ``` #### 解决 设置`Content-Type`时加上`charset=utf8`。 ``` server { listen 8080; server_name localhost; default_type 'applicaiton/json;charset=utf8'; charset utf-8; ... } ``` 也可以在`header_filter`阶段赋值`header`。 ``` ngx.header.content_type="application/json;charset=utf8" ```