Skip to content

Commit

Permalink
重构:lua 配置文件独立到lua 目录;将lua 目录通过volumes绑定到宿主主机上。
Browse files Browse the repository at this point in the history
  • Loading branch information
supermy committed Jun 10, 2015
1 parent de89d79 commit f4471b3
Show file tree
Hide file tree
Showing 17 changed files with 70 additions and 38 deletions.
15 changes: 14 additions & 1 deletion web+app/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,17 @@ nginx(tengine)+tomcat+mysql+memcached 集群
> nginx 整合HttpRedis2Module/lua-resty-redis完成,http方式访问redis;
> mysql2redis.sh 将mysql 的数据同步到redis,通过linux 的crontab 进行配置;
>
> ## end
>
>开发调试脚本
>
>tail -f docker-share/logs/web/error.log
>
>telnet 192.168.59.103 6379/monitor/keys */set key value/get key/hget rowkey fieldkey
>
>curl -v -b "ChannelCode=test;ChannelSecretkey=a8152b13f4ef9daca84cf981eb5a7907" http://192.168.59.103/api
>
>curl -v -b "ctoken=testf97a93b6e5e08843a7c825a53bdae246" http://192.168.59.103/api
>
>mysql -h 192.168.59.103 -ujava -pjava -Djavatest --skip-column-names --raw < mysql2redis.sql |redis-cli -h 192.168.59.103 --pipe
>
> ## end
4 changes: 3 additions & 1 deletion web+app/fig.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ data:
volumes:
- /Users/moyong/docker-share/www/:/data/www/
- /Users/moyong/docker-share/logs/web/:/usr/local/nginx/logs/
- /Users/moyong/docker-share/conf/nginx/lua/:/usr/local/nginx/conf/nginx.d/lua/ #配置文件,fix.me


data4app:
image: busybox
Expand Down Expand Up @@ -98,6 +100,6 @@ dbinit:
entrypoint: /bin/bash
volumes:
- .:/host
command: -c "sleep 8; mysql -u java --password=java -h mysql javatest < /host/init.sql; exit 0"
command: -c "sleep 8; mysql -u java --password=java -h mysql javatest < /host/mysql/init.sql; exit 0"
links:
- db:mysql
2 changes: 1 addition & 1 deletion web+app/mynginx/fig.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ data:
volumes:
- /Users/moyong/docker-share/www/:/data/www/
- /Users/moyong/docker-share/logs/web/:/usr/local/nginx/logs/
#- /Users/moyong/docker-share/conf/nginx/:/usr/local/nginx/conf/nginx.d/ #配置文件,fix.me
- /Users/moyong/docker-share/conf/nginx/lua/:/usr/local/nginx/conf/nginx.d/lua/ #配置文件,fix.me

myredis:
image: redis:latest
Expand Down
32 changes: 17 additions & 15 deletions web+app/mynginx/nginx.d/lua.conf
Original file line number Diff line number Diff line change
Expand Up @@ -7,74 +7,76 @@ location /hello {
#/lua_set_1?i=1&j=10
location /lua_set_1 {
default_type "text/html";
set_by_lua_file $num /usr/local/nginx/conf/nginx.d/test_set_1.lua;
set_by_lua_file $num /usr/local/nginx/conf/nginx.d/lua/test_set_1.lua;
echo $num;
}

#/lua_access?token=234将得到403 Forbidden的响应。根据如cookie/用户token来决定是否有访问权限。
location /lua_access {
default_type "text/html";
access_by_lua_file /usr/local/nginx/conf/nginx.d/test_access.lua;
access_by_lua_file /usr/local/nginx/conf/nginx.d/lua/test_access.lua;
echo "access";
}

#对于请求我们需要获取如请求参数、请求头、Body体等信息;而对于处理就是调用相应的Lua代码即可;输出响应需要进行响应状态码、响应头和响应内容体的输出
#wget --post-data 'a=1&b=2' 'http://127.0.0.1/lua_request/1/2?a=3&b=4' -O -
#wget --post-data 'a=1&b=2' 'http://192.168.59.103/lua_request/1/2?a=3&b=4' -O -
location ~ /lua_request/(\d+)/(\d+) {
#设置nginx变量
set $a $1;
set $b $host;
default_type "text/html";
#nginx内容处理
content_by_lua_file /usr/local/nginx/conf/nginx.d/test_request.lua;
content_by_lua_file /usr/local/nginx/conf/nginx.d/lua/test_request.lua;
#内容体处理完成后调用
echo_after_body "ngx.var.b $b";
}


location /lua_response_1 {
default_type "text/html";
content_by_lua_file /usr/local/nginx/conf/nginx.d/test_response_1.lua;
content_by_lua_file /usr/local/nginx/conf/nginx.d/lua/test_response_1.lua;
}

location /lua_response_2 {
default_type "text/html";
content_by_lua_file /usr/local/nginx/conf/nginx.d/test_response_2.lua;
content_by_lua_file /usr/local/nginx/conf/nginx.d/lua/test_response_2.lua;
}

location /lua_other {
default_type "text/html";
content_by_lua_file /usr/local/nginx/conf/nginx.d/test_other.lua;
content_by_lua_file /usr/local/nginx/conf/nginx.d/lua/test_other.lua;
}

location /lua_shared_dict {
default_type "text/html";
content_by_lua_file /usr/local/nginx/conf/nginx.d/test_lua_shared_dict.lua;
content_by_lua_file /usr/local/nginx/conf/nginx.d/lua/test_lua_shared_dict.lua;
}


#灰度发布,A/B选择
#我们想把商品编号为为8位(比如品类为图书的)没有改版完成,需要按照相应规则跳转到老版,但是其他的到新版;
#http://192.168.59.103/apiversion?skuId=123
#http://192.168.59.103/apiversion?skuId=123456789
#http://192.168.59.103/apiversion?skuId=12345678
location = /apiversion {
############ 测试时使用的动态请求


set_by_lua $skuId '
return local skuId = ngx.req.get_uri_args()["skuId"]
return ngx.req.get_uri_args()["skuId"]
';
set_by_lua $to_book '
local ngx_match = ngx.re.match
local var = ngx.var
--local skuId = var.skuId
--local skuId = ngx.req.get_uri_args()["skuId"]
local skuId="2222"
local skuId = var.skuId
local r = var.item_dynamic ~= "1" and ngx.re.match(skuId, "^[0-9]{8}$")
if r then return "1" else return "0" end;
';

set_by_lua $to_mvd '
local ngx_match = ngx.re.match
local var = ngx.var
--local skuId = var.skuId
--local skuId = ngx.req.get_uri_args()["skuId"]
local skuId="1111"
local skuId = var.skuId
local r = var.item_dynamic ~= "1" and ngx.re.match(skuId, "^[0-9]{9}$")
if r then return "1" else return "0" end;
';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,17 +214,6 @@ end
if not isempty(channel_code) then
--渠道带宽控制

--[[
local channel_ip_bind_time , err = cache:get(channel_code.."ip_bind_time")
local channel_ip_time_out , err = cache:get(channel_code.."ip_time_out")
local channel_connect_count ,err = cache:get(channel_code.."connect_count")
local channel_pwd ,err = cache:get(channel_code.."pwd") --秘钥盐渍
local channel_iplist ,err = cache:get(channel_code.."iplist") --渠道ip 地址
local channel_token ,err = cache:get(channel_code.."token") --渠道令牌
local channel_token_expire ,err = cache:get(channel_code.."token_expire") --渠道有效期
--]]

local channel_ip_bind_time , err = cache:hget("hash_"..channel_code,"ip_bind_time")
local channel_ip_time_out , err = cache:hget("hash_"..channel_code,"ip_time_out")
local channel_connect_count ,err = cache:hget("hash_"..channel_code,"connect_count")
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
44 changes: 35 additions & 9 deletions web+app/mynginx/nginx.d/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ error_log logs/error.log;

#pid logs/nginx.pid;


worker_processes auto;
worker_cpu_affinity auto;
worker_rlimit_nofile 65535;
Expand Down Expand Up @@ -43,7 +42,15 @@ http {
#keepalive_timeout 0;
keepalive_timeout 65;

gzip on;
gzip on;
gzip_min_length 4k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_proxied any; #前端是squid的情况下要加此参数,否则squid上不缓存gzip文件
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;


# upstream backendjava {
# server mytomcat1:8080 weight=10;
Expand All @@ -52,17 +59,36 @@ http {
# }


map $http_host $item_dynamic {
hostnames;

default 0;

example.com 1;
*.example.com 1;
example.org 2;
*.example.org 2;
.example.net 3;
wap.* 4;
}

map $http_user_agent $mobile {
default 0;
"~Opera Mini" 1;
}


# set search paths for pure Lua external libraries (';;' is the default path):
lua_package_path '/usr/local/nginx/conf/nginx.d/?.lua;/usr/local/share/lua/5.1/?.lua;/usr/local/share/luajit-2.0.3/jit/?.lua;/usr/local/openresty/lualib/?.lua;';
lua_package_path '/usr/local/nginx/conf/nginx.d/lua/?.lua;/usr/local/share/lua/5.1/?.lua;/usr/local/share/luajit-2.0.3/jit/?.lua;/usr/local/openresty/lualib/?.lua;';
# set search paths for Lua external libraries written in C (can also use ';;'):
lua_package_cpath '/usr/local/lib/lua/5.1/?.so;/usr/local/openresty/lualib/?.so;';


#lua 进行初始化参数
#生产环境下千万别关闭Lua代码缓存,只能用在开发模式下,因为对性能有十分大的影响(
#每次IO读取和编译Lua代码消耗很大, 简单的hello world都会慢一个数量级)。
#默认开启
lua_code_cache on;
#默认开启 生产环境todo
lua_code_cache off;

lua_shared_dict myconfig 1m;
#此指令中加载的全局变量会进行Copy-OnWrite,即会复制到所有全局变量到Worker进程。
Expand All @@ -78,7 +104,7 @@ http {
myconfig:set("mysql-port", "3306")
--加载配置文件
local file = io.open("/usr/local/nginx/conf/nginx.d/config.json", "r");
local file = io.open("/usr/local/nginx/conf/nginx.d/lua/config.json", "r");
local content = cjson.decode(file:read("*all"));
file:close();
Expand All @@ -92,7 +118,7 @@ http {
';

#用于启动一些定时任务,比如心跳检查,定时拉取服务器配置
init_worker_by_lua_file /usr/local/nginx/conf/nginx.d/init_worker.lua;
init_worker_by_lua_file /usr/local/nginx/conf/nginx.d/lua/init_worker.lua;


server {
Expand Down Expand Up @@ -129,8 +155,8 @@ http {
location = /api {
default_type 'text/plain';

#access_by_lua_file '/usr/local/nginx/conf/nginx.d/token.lua';
access_by_lua_file '/usr/local/nginx/conf/nginx.d/channel-auth.lua';
#access_by_lua_file '/usr/local/nginx/conf/nginx.d/lua/token.lua';
access_by_lua_file '/usr/local/nginx/conf/nginx.d/lua/channel-auth.lua';


content_by_lua '
Expand Down

0 comments on commit f4471b3

Please sign in to comment.