使用openresty + lua 搭建api 网关(一)安装openresty ,并添加lua模块
openresty 有点不多说,网上各种介绍,先安装吧。
官方操作在此,http://openresty.org/cn/installation.html,
tar -xzvf openresty-VERSION.tar.gz
cd openresty-VERSION/
./configure
make
sudo make install
./configure
然后在进入 openresty-VERSION/
目录, 然后输入以下命令配置:
./configure
默认, --prefix=/usr/local/openresty
程序会被安装到/usr/local/openresty目录。
您可以指定各种选项,比如
./configure --prefix=/opt/openresty \
--with-luajit \
--without-http_redis2_module \
--with-http_iconv_module \
--with-http_postgres_module
试着使用 ./configure --help
查看更多的选项。
HelloWorld
Prepare directory layout
We first create a separate directory for our experiments. You can use an arbitrary directory. Here for simplicity, we just use ~/work
:
mkdir ~/work
cd ~/work
mkdir logs/ conf/
Note that we've also created the logs/
directory for logging files and conf/
for our config files.
Prepare the nginx.conf config file
Create a simple plain text file named conf/nginx.conf
with the following contents in it:
worker_processes 1;
error_log logs/error.log;
events {
worker_connections 1024;
}
http {
server {
listen 8080;
location / {
default_type text/html;
content_by_lua '
ngx.say("<p>hello, world</p>")
';
}
}
}
If you're familiar with Nginx configuration, it should look very familiar to you. OpenResty is just an enhanced version of Nginx by means of addon modules anyway. You can take advantage of all the exisitng goodies in the Nginx world.
Start the Nginx server
Assuming you have installed OpenResty into /usr/local/openresty
(this is the default), we make our nginx
executable of our OpenResty installation available in our PATH
environment:
PATH=/usr/local/openresty/nginx/sbin:$PATH
export PATH
Then we start the nginx server with our config file this way:
nginx -p `pwd`/ -c conf/nginx.conf
Error messages will go to the stderr device or the default error log files logs/error.log
in the current working directory.
Access our HelloWorld web service
We can use curl to access our new web service that says HelloWorld:
curl http://localhost:8080/
If everything is okay, we should get the output
<p>hello, world</p>
You can surely point your favorite web browser to the location http://localhost:8080/
.
Test performance
See Benchmark for details.
Where to go from here
View the documentation of each component at the Components page and find Nginx related stuff on the Nginx Wiki site.
1、创建目录/usr/servers,以后我们把所有软件安装在此目录
- mkdir -p /usr/servers
- cd /usr/servers/
2、安装依赖(我的环境是ubuntu,可以使用如下命令安装,其他的可以参考openresty安装步骤)
- apt-get install libreadline-dev libncurses5-dev libpcre3-dev libssl-dev perl
3、下载ngx_openresty-1.7.7.2.tar.gz并解压
- wget http://openresty.org/download/ngx_openresty-1.7.7.2.tar.gz
- tar -xzvf ngx_openresty-1.7.7.2.tar.gz
ngx_openresty-1.7.7.2/bundle目录里存放着nginx核心和很多第三方模块,比如有我们需要的Lua和LuaJIT。
3、安装LuaJIT
- cd bundle/LuaJIT-2.1-20150120/
- make clean && make && make install
- ln -sf luajit-2.1.0-alpha /usr/local/bin/luajit
4、下载ngx_cache_purge模块,该模块用于清理nginx缓存
- cd /usr/servers/ngx_openresty-1.7.7.2/bundle
- wget https://github.com/FRiCKLE/ngx_cache_purge/archive/2.3.tar.gz
- tar -xvf 2.3.tar.gz
5、下载nginx_upstream_check_module模块,该模块用于ustream健康检查
- cd /usr/servers/ngx_openresty-1.7.7.2/bundle
- wget https://github.com/yaoweibin/nginx_upstream_check_module/archive/v0.3.0.tar.gz
- tar -xvf v0.3.0.tar.gz
6、安装ngx_openresty
- cd /usr/servers/ngx_openresty-1.7.7.2
- ./configure --prefix=/usr/servers --with-http_realip_module --with-pcre --with-luajit --add-module=./bundle/ngx_cache_purge-2.3/ --add-module=./bundle/nginx_upstream_check_module-0.3.0/ -j2
- make && make install
--with*** 安装一些内置/集成的模块
--with-http_realip_module 取用户真实ip模块
-with-pcre Perl兼容的达式模块
--with-luajit 集成luajit模块
--add-module 添加自定义的第三方模块,如此次的ngx_che_purge
8、到/usr/servers目录下
- cd /usr/servers/
- ll
会发现多出来了如下目录,说明安装成功
/usr/servers/luajit :luajit环境,luajit类似于java的jit,即即时编译,lua是一种解释语言,通过luajit可以即时编译lua代码到机器代码,得到很好的性能;
/usr/servers/lualib:要使用的lua库,里边提供了一些默认的lua库,如redis,json库等,也可以把一些自己开发的或第三方的放在这;
/usr/servers/nginx :安装的nginx;
通过/usr/servers/nginx/sbin/nginx -V 查看nginx版本和安装的模块
7、启动nginx
/usr/servers/nginx/sbin/nginx
接下来该配置nginx+lua开发环境了
配置环境
配置及Nginx HttpLuaModule文档在可以查看http://wiki.nginx.org/HttpLuaModule。
1、编辑nginx.conf配置文件
- vim /usr/servers/nginx/conf/nginx.conf
2、在http部分添加如下配置
- #lua模块路径,多个之间”;”分隔,其中”;;”表示默认搜索路径,默认到/usr/servers/nginx下找
- lua_package_path "/usr/servers/lualib/?.lua;;"; #lua 模块
- lua_package_cpath "/usr/servers/lualib/?.so;;"; #c模块
3、为了方便开发我们在/usr/servers/nginx/conf目录下创建一个lua.conf
- #lua.conf
- server {
- listen 80;
- server_name _;
- }
4、在nginx.conf中的http部分添加include lua.conf包含此文件片段
- include lua.conf;
5、测试是否正常
- /usr/servers/nginx/sbin/nginx -t
如果显示如下内容说明配置成功
nginx: the configuration file /usr/servers/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/servers/nginx/conf/nginx.conf test is successful
HelloWorld
1、在lua.conf中server部分添加如下配置
- location /lua {
- default_type 'text/html';
- content_by_lua 'ngx.say("hello world")';
- }
2、测试配置是否正确
- /usr/servers/nginx/sbin/nginx -t
3、重启nginx
- /usr/servers/nginx/sbin/nginx -s reload
4、访问如http://192.168.1.6/lua(自己的机器根据实际情况换ip),可以看到如下内容
hello world
5、lua代码文件
我们把lua代码放在nginx配置中会随着lua的代码的增加导致配置文件太长不好维护,因此我们应该把lua代码移到外部文件中存储。
- vim /usr/servers/nginx/conf/lua/test.lua
- #添加如下内容
- ngx.say("hello world");
然后lua.conf修改为
- location /lua {
- default_type 'text/html';
- content_by_lua_file conf/lua/test.lua; #相对于nginx安装目录
- }
此处conf/lua/test.lua也可以使用绝对路径/usr/servers/nginx/conf/lua/test.lua。
6、lua_code_cache
默认情况下lua_code_cache 是开启的,即缓存lua代码,即每次lua代码变更必须reload nginx才生效,如果在开发阶段可以通过lua_code_cache off;关闭缓存,这样调试时每次修改lua代码不需要reload nginx;但是正式环境一定记得开启缓存。
- location /lua {
- default_type 'text/html';
- lua_code_cache off;
- content_by_lua_file conf/lua/test.lua;
- }
开启后reload nginx会看到如下报警
nginx: [alert] lua_code_cache is off; this will hurt performance in /usr/servers/nginx/conf/lua.conf:8
7、错误日志
如果运行过程中出现错误,请不要忘记查看错误日志。
- tail -f /usr/servers/nginx/logs/error.log
到此我们的基本环境搭建完毕。
nginx+lua项目构建
以后我们的nginx lua开发文件会越来越多,我们应该把其项目化,已方便开发。项目目录结构如下所示:
example
example.conf ---该项目的nginx 配置文件
lua ---我们自己的lua代码
test.lua
lualib ---lua依赖库/第三方依赖
*.lua
*.so
其中我们把lualib也放到项目中的好处就是以后部署的时候可以一起部署,防止有的服务器忘记复制依赖而造成缺少依赖的情况。
我们将项目放到到/usr/example目录下。
/usr/servers/nginx/conf/nginx.conf配置文件如下(此处我们最小化了配置文件)
- #user nobody;
- worker_processes 2;
- error_log logs/error.log;
- events {
- worker_connections 1024;
- }
- http {
- include mime.types;
- default_type text/html;
- #lua模块路径,其中”;;”表示默认搜索路径,默认到/usr/servers/nginx下找
- lua_package_path "/usr/example/lualib/?.lua;;"; #lua 模块
- lua_package_cpath "/usr/example/lualib/?.so;;"; #c模块
- include /usr/example/example.conf;
- }
通过绝对路径包含我们的lua依赖库和nginx项目配置文件。
/usr/example/example.conf配置文件如下
- server {
- listen 80;
- server_name _;
- location /lua {
- default_type 'text/html';
- lua_code_cache off;
- content_by_lua_file /usr/example/lua/test.lua;
- }
- }
lua文件我们使用绝对路径/usr/example/lua/test.lua。
使用openresty + lua 搭建api 网关(一)安装openresty ,并添加lua模块的更多相关文章
- 使用 Node.js 搭建API 网关
外部客户端访问微服务架构中的服务时,服务端会对认证和传输有一些常见的要求.API 网关提供共享层来处理服务协议之间的差异,并满足特定客户端(如桌面浏览器.移动设备和老系统)的要求. 微服务和消费者 微 ...
- yum安装的Nginx添加第三方模块支持tcp
需求:生产有个接口是通过socket通信.nginx1.9开始支持tcp层的转发,通过stream实现的,而socket也是基于tcp通信. 实现方法:Centos7.2下yum直接安装的nginx, ...
- 已安装的nginx添加其他模块
总体操作就是添加新模块并重新编译源码,然后把编译后的nginx可执行文件覆盖原来的那个即可.1 查看已安装的参数nginx -V拷贝那些巴拉巴拉的参数,后面编译的时候使用 2 下载相同版本号的源码,解 ...
- 微服务网关从零搭建——(二)搭建api网关(不带验证)
环境准备 创建空的core2.1 api项目 演示使用名称APIGateWay 过程参考上一篇 完成后在appsettings.json 添加节点 "Setting": { & ...
- OpenResty api网关设计
本文讲述 OpenResty api网关设计,主要涉及api网关介绍.openresty api网关 请求路由(路由判断.路由重写.服务判断.限流).授权验证(统一认证).动态Upstream 以及这 ...
- SIA-GateWay之API网关安装部署指南
SIA-GATEWAY是基于SpringCloud微服务生态体系下开发的一个分布式微服务网关系统.具备简单易用.可视化.高可扩展.高可用性等特征,提供云原生.完整及成熟的接入服务解决方案.本文介绍AP ...
- API网关【gateway 】- 1
最近在公司进行API网关重写,公司内采用serverMesh进行服务注册,调用,这里结合之前学习对API网关服务进行简单的总结与分析. 网关的单节点场景: 网关的多节点场景: 这里的多节点是根据模块进 ...
- Spring Cloud第十四篇 | Api网关Zuul
本文是Spring Cloud专栏的第十四篇文章,了解前十三篇文章内容有助于更好的理解本文: Spring Cloud第一篇 | Spring Cloud前言及其常用组件介绍概览 Spring C ...
- Asp.Net Core API网关Ocelot
首先,让我们简单了解下什么是API网关? API网关是一个服务器,是系统的唯一入口.从面向对象设计的角度看,它与外观模式类似.API网关封装了系统内部架构,为每个客户端提供一个定制的API.它可能还具 ...
随机推荐
- php私有成员private的程序题目
class base { private $member; function __construct() { echo __METHOD__ . "(begin)\n"; $thi ...
- Ionic学习笔记1_基本布局
<body> <!-- 头部 --> bar里嵌入子元素:title,button,button-bar和 inpu ...
- Android JNI和NDK学习(05)--JNI真机调试(转)
本文转自: http://www.cnblogs.com/skywang12345/archive/2013/05/23/3094250.html 本文主要介绍如何将JNI导入到真机进行调试.下面以M ...
- 2017 Wuhan University Programming Contest (Online Round) C. Divide by Six 分析+模拟
/** 题目:C. Divide by Six 链接:https://oj.ejq.me/problem/24 题意:给定一个数,这个数位数达到1e5,可能存在前导0.问为了使这个数是6的倍数,且没有 ...
- Discuz! X 插件开发手册
文件命名规范 Discuz! 按照如下的规范对程序和模板进行命名,请在设计插件时尽量遵循此命名规范: 可以直接通过浏览器访问的普通程序文件,以 .php 后缀命名. 被普通程序文件引用的程序文件, ...
- python 爬虫2 Urllib库的高级用法
1.设置Headers 有些网站不会同意程序直接用上面的方式进行访问,如果识别有问题,那么站点根本不会响应,所以为了完全模拟浏览器的工作,我们需要设置一些Headers 的属性. import url ...
- HTML5学习笔记简明版(10):废弃的元素和属性
废弃的元素(Element) 这个小节里列出的元素在HTML5里将不再使用.现有文档升级到 HTML5的话能够使用一些替代方案. 比如parser section 能够处理isindex 元素的功能. ...
- gridgroup行内编辑删除
Ext.define('Task', { extend: 'Ext.data.Model', idProperty: 'taskId', fields: [ { name: 'projectId', ...
- openssl 升级 操作 -1
好多公司都会用绿盟扫描系统漏洞,里边就会涉及到ssl 漏洞,原因是openssl 版本低导致,会让你升级到指定版本.下面就介绍一下openssl 版本升级的操作方案. 一. 查看系统版本 [root@ ...
- POJ1182食物链(并查集经典好题)
题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=66964#problem/E 题目思路:主要有两种思路:1.带权并查集2.挑战程 ...