-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlua_memcached_mysql_www.conf
81 lines (71 loc) · 3 KB
/
lua_memcached_mysql_www.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
location /video {
index index.html index.htm index.php
add_header Content-Type "text/plain text/html charset=utf-8" ;
set $oldurl "";
set $newurl "";
rewrite_by_lua '
local aurl = "http://"..ngx.var.server_name..ngx.var.request_uri
local access_url = "http://"..ngx.var.server_addr..ngx.var.request_uri
local memcached = require "resty.memcached"
local memc, err = memcached:new()
if not memc then
ngx.say("failed to instantiate memc")
return
end
memc:set_timeout(1000) -- 1 sec
local ok, err = memc:connect("192.168.xxx.xxx", 11211)
if not ok then
ngx.say("failed to connect")
-- return
end
local un_access_url = ngx.unescape_uri(access_url)
local res, flags, err = memc:get(un_access_url)
if err then
ngx.say("failed to get url")
end
if not res then
local mysql = require "resty.mysql"
local db, err = mysql:new()
if not db then
return ngx.exit(404)
end
db:set_timeout(1000) -- 1 sec
local ok, err, errno, sqlstate = db:connect{
host = "192.168.xxx.xxx",
port = 3306,
database = "xxxx",
user = "xxxx",
password = "xxxx",
max_packet_size = 1024 * 1024
}
if not ok then
return ngx.exit(404)
end
local un_aurl = ngx.unescape_uri(aurl)
local sql_name = ngx.quote_sql_str(un_aurl)
local sql = "select old_url,new_url from group_plugin_fansclub_old_article_log where old_url = " .. sql_name
res, err, errno, sqlstate = db:query(sql)
if not res then
return ngx.exit(404)
end
for key,value in pairs(res)do
newurl = value.new_url
oldurl = value.old_url
end
if oldurl then
local ok, err = memc:set(oldurl,newurl)
return ngx.redirect(newurl,301)
else
ngx.exit(404)
end
end
local ok, err = memc:set_keepalive(10000, 100)
if not ok then
ngx.say("cannot set keepalive: ", err)
return
end
-- ngx.say("memcached url")
ngx.redirect(res,301)
return
';
}