Zuul for Nginx&Lua
基于Nginx和Lua module。需要安装Nginx Lua模块或者直接下载openresty编译安装。
安装和配置ngx-lua-zuul
下载代码到/path/to/nginx/lua/lib/
git clone http://github.com/tietang/ngx-lua-zuul --depth=1
配置nginx.conf
从nginx.conf.default 复制nginx.conf,也可以copy下面的例子并保存为nginx.conf,以快速运行。
在nginx.conf的http节点中添加:
lua_package_path "/Users/tietang/nginx/nginx/lua/lib/?.lua;;";
lua_shared_dict discovery 10m;
lua_shared_dict apps_count 1m;
lua_shared_dict apps_res_time 10m;
lua_shared_dict api_count 10m;
lua_shared_dict api_res_time 10m;
init_by_lua '
discovery = require "discovery"
json=require "cjson"
balancer=require "robin"
router = require "router"
';
init_worker_by_lua '
discovery:schedule()
';
location 中添加:
location / {
set $bk_host '';
# default_type text/html;
access_by_lua_file lua/lib/app_route.lua;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://$bk_host;
log_by_lua_file lua/lib/stats.lua;
#
}
下面是一个完整的例子:
#user nobody;
worker_processes 2;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
##lua 代码路径
lua_package_path "/Users/tietang/nginx/nginx/lua/lib/?.lua;;";
##一些共享缓存
lua_shared_dict discovery 10m;
lua_shared_dict apps_count 1m;
lua_shared_dict apps_res_time 10m;
lua_shared_dict api_count 10m;
lua_shared_dict api_res_time 10m;
lua_shared_dict metrics 10m;
lua_shared_dict metrics_time 10m;
##初始化
init_by_lua '
discovery = require "discovery"
json=require "cjson"
balancer=require "robin"
router = require "router"
';
## 初始化定时器
init_worker_by_lua '
discovery:schedule()
';
server {
listen 8000;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location = / {
set $dir $document_root;
root $dir/html;
index index.html index.htm;
}
# Dashboard路径
location ^~ /_dashboard {
set $dir "./html";
# root $dir/html/;
alias $dir;
index index.html index.htm;
}
# 静态文件路径
location ^~ /public/ {
set $dir "./html/public/";
alias $dir;
}
# 一些管理功能-例子统计
location = /_admin/stats {
default_type text/html;
content_by_lua_file lua/lib/stats_show.lua;
}
location = /_admin/stats.json {
default_type application/json;
content_by_lua_file lua/lib/stats_show_json.lua;
}
location = /_admin/data.json {
default_type application/json;
content_by_lua '
local v={}
v.date=ngx.now()*1000
-- string.gsub(ngx.localtime()," ",":")
v.close=math.random(2,100)
ngx.say( json.encode(v))
';
}
location = /_admin/data.tsv {
default_type application/json;
content_by_lua '
ngx.say("date close")
ngx.say(string.gsub(ngx.localtime()," ",":") .. " ".. math.random(20,600) )
';
}
## 请求转发处理
location / {
set $bk_host '';
# default_type text/html;
# 路由计算
access_by_lua_file lua/lib/app_route.lua;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://$bk_host;
# 简单统计收集
log_by_lua_file lua/lib/stats.lua;
#
}
#用于调试的例子
location = /haha {
default_type application/json;
# access_by_lua_file demo/demo.lua;
content_by_lua '
ngx.say(ngx.var.host)
ngx.say(ngx.time())
ngx.say(ngx.now())
ngx.say( json.encode(router.routingTable) .."
")
ngx.say( "
")
ngx.say(json.encode(discovery.hosts ) .."
")
';
#
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
例子Eureka 服务
如果没有Eureka环境,也可以编译安装本例子中的EurekaDemo服务,参考:编译和运行eureka-demo服务
运行测试
启动所有的demo服务:discovery,api,zuul;
启动nginx;
打开浏览器:http://127.0.0.1:8000/api/test/0/0
其测试api参考编译和运行eureka-demo服务中的相关内容。