diff --git a/application/controller/LiveRedisCacheController.lua b/application/controller/LiveRedisCacheController.lua index 802fd67..f388e34 100644 --- a/application/controller/LiveRedisCacheController.lua +++ b/application/controller/LiveRedisCacheController.lua @@ -9,6 +9,7 @@ * |------------------------------------------------------------------------ --]] local template = require "resty.template" +local helper = require "vendor.helper" local redis = require "resty.redis" local cjson = require("cjson") local resty_lock = require "resty.lock" @@ -25,7 +26,7 @@ local live_room = ngx.shared.live_room; local redis_host = "121.41.88.209" local redis_port = 63789 -local redis_auth = "tinywanredisamaistream" +local redis_auth = "tinywanredisamaistream123456789" local redis_timeout = 1000 ----------------- set ngx.cache @@ -211,9 +212,9 @@ end -------------- read_http 大并发采用 resty.http ,对于:ngx.location.capture 慎用 local function read_http(id) local httpc = http.new() - local resp, err = httpc:request_uri("http://sewise.amai8.com", { + local resp, err = httpc:request_uri("http://testwww.baidu.com", { method = "GET", - path = "/openapi/luaJson?id=" .. id, + path = "/api/Api?liveId=" .. id, headers = { ["User-Agent"] = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.111 Safari/537.36" } @@ -236,19 +237,27 @@ local function read_http(id) return end - -- 缓存到 Redis 数据缓存 -- 这里要做优化 - local live_info_key = "LIVE_TABLE:" .. id - local live_value = cjson_decode(resp.body) -- 解析的Lua自己的然后存储到Redis 数据库中去 - local live_live_str = write_redis(redis_host,redis_port,redis_auth, { live_info_key }, cjson_encode(live_value)) - if not live_live_str then - log(ERR, "redis set info error: ") + -- backend not data 判断api返回的状态码 + local status_code = helper.cjson_decode(resp.body)['code'] + if tonumber(status_code) ~= 404 then + -- 正常数据缓存到 Redis 数据缓存 + local live_info_key = "LIVE_TABLE:" .. id + local live_value = helper.cjson_decode(resp.body) -- 解析的Lua自己的然后存储到Redis 数据库中去 + local live_live_str = write_redis(redis_host, redis_port, redis_auth, { live_info_key }, helper.cjson_encode(live_value)) + if not live_live_str then + log(ERR, "redis set info error: ") + end + log(ERR, " [read_http] content from backend API id : " .. id) -- tag data origin + return helper.cjson_encode(live_value) + else + -- 后端没有数据直接返回 nil + log(ERR, " [read_http] backend API is not content return error_msg : " ..helper.cjson_decode(resp.body)['msg']) -- tag data origin + return end - log(ERR, " [read_http] content from backend API id : " .. id) -- tag data origin - return cjson_encode(live_value) end -----------------------------------------------------------------------业务逻辑处理 ---get var id +---------------------------------------------------------------------- 业务逻辑处理 +-- get var id local id = ngx_var.id local live_info_key = "LIVE_TABLE:" .. id @@ -262,14 +271,15 @@ if not content then log(ERR, "redis not found content, back to backend API , id : ", id) content = read_http(id) end + -------------------------------- 主要就是这里要根据后端服务没有数据的情况处理,最好不要出现500错误就可以啦 --- if backend API not exit 404 +-- if backend API not success data , page show 404 if not content then log(ERR, "backend API not found content, id : ", id) return ngx.exit(ngx.HTTP_NOT_FOUND) end --- if backend API response result is false 403 +-- if backend API response result is false page show 403 if tostring(content) == "false" then log(ERR, "backend API content is false ", id) return ngx.exit(ngx.HTTP_FORBIDDEN) diff --git a/application/controller/WebSocketRedisController.lua b/application/controller/WebSocketRedisController.lua index 16cc7b4..7214bf0 100644 --- a/application/controller/WebSocketRedisController.lua +++ b/application/controller/WebSocketRedisController.lua @@ -32,6 +32,7 @@ if not wb then return ngx.exit(444) end + local push = function() -- --create redis local red = redis:new() @@ -45,7 +46,7 @@ local push = function() ok, err = red:auth(redis_auth) if not ok then - log(ERR, "failed to auth: ", err) + ngx.log(ngx.ERR, "failed to auth: ", err) wb:send_close() end --sub @@ -61,7 +62,7 @@ local push = function() local res, err = red:read_reply() if res then local item = res[3] --- local bytes, err = wb:send_text(tostring(msg_id).." "..item) + --local bytes, err = wb:send_text(tostring(msg_id).." "..item) local bytes, err = wb:send_text('[ '..tostring(msg_id).." ] "..ngx.localtime().." "..item) if not bytes then -- better error handling @@ -73,6 +74,7 @@ local push = function() end end + local co = ngx.thread.spawn(push) -- create thread 1 --main loop diff --git a/application/demo/company_branch/cookie_test.lua b/application/demo/company_branch/cookie_test.lua new file mode 100644 index 0000000..2817f9e --- /dev/null +++ b/application/demo/company_branch/cookie_test.lua @@ -0,0 +1,33 @@ +-- +-- Created by IntelliJ IDEA. +-- User: Administrator +-- Date: 2017/6/19 +-- Time: 10:05 +-- To change this template use File | Settings | File Templates. +-- +local redis = require "resty.redis" +local log = ngx.log +local redis_host = "121.41.88.209" +local redis_port = "63789" +local redis_auth = "tinywanredisamaistream" +local redis_timeout = 1000 + +local red = redis:new() +red:set_timeout(5000) -- 1 sec +local ok, err = red:connect(redis_host, redis_port) +if not ok then + ngx.log(ngx.ERR, "failed to connect redis: ", err) + wb:send_close() + return +end + +ok, err = red:auth(redis_auth) +if not ok then + log(ERR, "failed to auth: ", err) +end + +resp, err = red:set("LATEST_COMMENT::12345", "LATEST_COMMENT VALUES") +if not resp then + ngx.log(ngx.ERR, "set redis live error : ", err) +end + diff --git a/application/demo/company_branch/header_filter.lua b/application/demo/company_branch/header_filter.lua new file mode 100644 index 0000000..dd48438 --- /dev/null +++ b/application/demo/company_branch/header_filter.lua @@ -0,0 +1,35 @@ +-- +-- Created by IntelliJ IDEA. +-- User: Administrator +-- Date: 2017/6/19 +-- Time: 10:14 +-- To change this template use File | Settings | File Templates. +-- +local match = string.match + +local function get_cookies() + -- local cookies = ngx.header["Set-Cookie"] or {} + local cookies = ngx.header.set_cookie or {} + if type(cookies) == "string" then + cookies = {cookies} + end + return cookies +end + +local function remove_cookie(cookie_name) + local cookies = get_cookies() + + ngx.log(ngx.ERR, "source cookies ", table.concat(cookies, " ")) + for key, value in ipairs(cookies) do + local name = match(value, "(.-)=") + ngx.log(ngx.ERR, key.."<=>", value) + if name == cookie_name then + table.remove(cookies, key) + end + end + + ngx.header['Set-Cookie'] = cookies or {} + ngx.log(ngx.ERR, "new cookies ", table.concat(cookies, " ")) +end + +remove_cookie("Foo") \ No newline at end of file diff --git a/application/demo/wechat_test.lua b/application/demo/wechat_test.lua new file mode 100644 index 0000000..f6912d0 --- /dev/null +++ b/application/demo/wechat_test.lua @@ -0,0 +1,11 @@ +-- +-- Created by IntelliJ IDEA. +-- User: Administrator +-- Date: 2017/7/11 +-- Time: 17:03 +-- To change this template use File | Settings | File Templates. +-- +local wechat = require "vendor.wechat" +ngx.say("_VERSION == "..wechat._VERSION) +ngx.say("_VERSION == "..wechat.index()) + diff --git a/conf/domains/api.conf b/conf/domains/api.conf deleted file mode 100644 index 66325a9..0000000 --- a/conf/domains/api.conf +++ /dev/null @@ -1,36 +0,0 @@ -# server live -server { - listen 8686; - server_name 127.0.0.1; - charset utf8; - default_type application/json; - #access_log /mnt/hgfs/Linux-Share/Lua/lua_project_v0.01/logs/api_access.log; - #error_log /mnt/hgfs/Linux-Share/Lua/lua_project_v0.01/logs/api_error.log error; - set $project_path /mnt/hgfs/Linux-Share/Lua/; # 定义项目根目录,如项目放在/home/www/ - lua_code_cache off; - # 这样子只需要写对应的 Lua 文件,再也不用去每次修改 Nginx 主配置了 - # 用户 - location ~ ^/0.1/([-_a-zA-Z0-9/]+)$ { - set $id $1; - content_by_lua_file "${project_path}lua_project_v0.01/application/api/${id}.lua"; - } - - # 直播接口数据 - location ~ ^/0.1/live/([-_a-zA-Z0-9/]+) { - content_by_lua_file "${project_path}lua_project_v0.01/application/api/$1.lua"; - } - - # Redis 没有缓存数据去后端Mysql查询数据 - location /sub { - internal; - #proxy_pass http://backend/$1$is_args$args; - proxy_pass http://127.0.0.1:8686/backend/mysql; - } - - # Backend Mysql Data - location /backend/mysql { - content_by_lua_block { - ngx.say("Backend source mysql data") - } - } -} diff --git a/conf/domains/nginx_demo.conf b/conf/domains/nginx_demo.conf deleted file mode 100644 index 6dc38ca..0000000 --- a/conf/domains/nginx_demo.conf +++ /dev/null @@ -1,234 +0,0 @@ -#upstream -upstream item_http_upstream { - server 192.168.1.1 max_fails=2 fail_timeout=30s weight=5; - server 192.168.1.2 max_fails=2 fail_timeout=30s weight=5; -} -proxy_cache_path /mnt/hgfs/Linux-Share/Lua/lua_project_v0.01/cache/proxy_cache levels=1:2 keys_zone=cache_item:200m inactive=1m max_size=30g; -#缓存 -lua_shared_dict item_local_shop_cache 600m; -# the size depends on the number of servers in upstream {}: -lua_shared_dict healthcheck 1m; -lua_shared_dict ip_blacklist 1m; -#lua_socket_log_errors off; -server { - listen 8080; - server_name localhost; - index index.php index.html index.htm; - set $project_path /mnt/hgfs/Linux-Share/Lua/; # 定义项目根目录,如项目放在/home/www/ - set $web_root "${project_path}lua_project_v0.01/template/websocket"; - - location / { - root $web_root; - } - - # cjson decode 失败,500 错误直接退 - location /cjson_decode_pcall { - default_type 'text/html'; - content_by_lua_file "${project_path}lua_project_v0.01/application/demo/cjson.lua"; - #content_by_lua_file "/home/www/lua_project_v0.01/application/demo/cjson.lua"; - } - - #test_ip_location - location /test_ip_location { - resolver 8.8.8.8; # dns - default_type "text/plain"; - content_by_lua_file "${project_path}lua_project_v0.01/application/demo/ip_location.lua"; - #content_by_lua_file "/mnt/hgfs/Linux-Share/Lua/lua_project_v0.01/application/demo/ip_location.lua"; - } - - # redis option - location /get_redis_iresty { - default_type 'text/html'; - content_by_lua_file "${project_path}lua_project_v0.01/application/get_redis_iresty.lua"; - } - - # test_redis_long - location /test_redis_short { - default_type 'text/html'; - content_by_lua_file "${project_path}lua_project_v0.01/application/redis/test_short.lua"; - } - - # test_redis_long - location /test_redis_long { - default_type 'text/html'; - content_by_lua_file "${project_path}lua_project_v0.01/application/redis/test_long.lua"; - } - - # redis_iresty_short.lua - location /redis_iresty_short { - default_type 'text/html'; - content_by_lua_file "${project_path}lua_project_v0.01/application/redis/redis_iresty_short.lua"; - } - - # test_redis_long - location /redis_iresty_long { - default_type 'text/html'; - content_by_lua_file "${project_path}lua_project_v0.01/application/redis/redis_iresty_long.lua"; - } - - # shell - location /shell_test { - content_by_lua_block { - local shell = require("resty.shell") - local args = { - socket = "unix:/tmp/shell.sock", - } - -- local status, out, err = shell.execute("cat /proc/cpuinfo", args) - ngx.header.content_type = "text/plain" - ngx.say("Hello from:\n" .. out) - } - } - - # shell - location /shell_websocket { - content_by_lua_block { - local shell = require("resty.shell") - local args = { - socket = "unix:/tmp/shell.sock", - } - local status, out, err = shell.execute("nohup websocketd --port=8082 /home/tinywan/Go/count.sh 2>&1 &", args) - ngx.header.content_type = "text/plain" - ngx.say("Hello from:\n") - } - } - - #ngx_timer_at - location /ngx_timer_at { - default_type 'text/html'; - content_by_lua_file /home/tinywan/Openresty_Protect/First_Protect/lua/get_timer_at.lua; - } - - # websocket系统负载统计 - location =/vmstats { - default_type text/html; - content_by_lua_file /mnt/hgfs/Linux-Share/Lua/lua_project_v0.01/application/websocket_server.lua; - } - - # lua-resty-websocket 官方测试案例 - location /lua_websocket_server { - default_type text/html; - content_by_lua_file "${project_path}lua_project_v0.01/application/lua_websocket_server.lua"; - } - - # mysql 官方测试案例 - location /mysql_server { - default_type text/html; - content_by_lua_file "${project_path}lua_project_v0.01/application/mysql/mysql.lua"; - } - - location /mysql_fun { - default_type text/html; - content_by_lua_file "${project_path}lua_project_v0.01/application/mysql/mysql_fun.lua"; - } - - location /mysql_fun_test { - default_type text/html; - content_by_lua_file "${project_path}lua_project_v0.01/application/mysql/mysql_fun_test.lua"; - } - - # ngx_re_match 官方测试案例 - location /ngx_re_match { - default_type text/html; - content_by_lua_block { - local m, err = ngx.re.match("/live/4001493201083111111.m3u8", "[0-9]+") - if m then - -- m[0] == "1234" - ngx.say("makcj::",m[0]) - else - if err then - ngx.log(ngx.ERR, "error: ", err) - return - end - ngx.say("match not found") - end - } - } - - #test_mysql - location /test_mysql { - resolver 8.8.8.8; # dns; - default_type "text/plain"; - content_by_lua_file "/mnt/hgfs/Linux-Share/Lua/lua_project_v0.01/application/mysql/mysql.lua"; - } - - # os.execute 测试 标准os库 - location /os_execute { - content_by_lua_block { - --文件重命名 - local ret = os.rename("/home/tinywan/shell/123TEST", "/home/tinywan/shell/123TEST_rename") - ngx.say(ret) - -- 删除一个文件 - local ret = os.remove("/home/tinywan/shell/tinywan123.sh") - ngx.say(ret) - local ret = os.execute("mkdir /home/tinywan/shell/123TEST_execute") - ngx.say(ret) - } - } - - # 产品列表 nginx proxy cache 使用 - location /2017TinywanInfo { - # set $skuId $1; - expires 3m; # 设置响应缓存头信息,此处是3分钟;将会得到Cache-Control:max-age=180和类似Expires:Sat, 28 Feb 2015 10:01:10 GMT的响应头 - proxy_cache cache_item; # 使用名为cache_item的对应缓存配置 - proxy_cache_key $uri; # 定义缓存唯一key,通过唯一key来进行hash存取,缓存key为uri,不包括host和参数,这样不管用户怎么通过在url上加随机数都是走缓存的 - #proxy_cache_bypass $item_dynamic; # nginx不从缓存取响应的条件,可以写多个 - #proxy_no_cache $item_dynamic; # nginx不将响应内容写入缓存的条件,可以写多个 - proxy_cache_valid 200 301 3m; # 为不同的响应状态码设置不同的缓存时间,此处我们对200、301缓存3分钟 - proxy_cache_use_stale updating error timeout invalid_header http_500 http_502 http_503 http_504; # 什么情况下使用不新鲜(过期)的缓存内容 - proxy_pass_request_headers off; # 我们不需要请求头,所以不传递 - proxy_set_header Host $host; # 自定义http header头,用于发送给后端真实服务器 - #支持keep-alive - proxy_http_version 1.1; # 1.1和proxy_set_header Connection "":支持keepalive - proxy_set_header Connection ""; - proxy_pass http://127.0.0.1:8083; - add_header X-Cache '$upstream_cache_status'; #添加是否缓存命中的响应头;比如命中HIT、不命中MISS、不走缓存BYPASS;比如命中会看到X-Cache:HIT响应头 - } - - # 清理缓存配置proxy_cache - location /purge { - allow 127.0.0.1; - #allow 192.168.1.0/16; - deny all; - #proxy_cache_purge cache_item $arg_url; - } - - location /cjson { - content_by_lua_file "/mnt/hgfs/Linux-Share/Lua/lua_project_v0.01/application/demo/cjson.lua"; - } - - # lua-resty-template test - location /template_test { - default_type 'text/html'; - content_by_lua ' - local template = require "resty.template" - template.render("index2.html", { message = "Hello, World!" }) - '; - } - - # 模板渲染 带有控制器数据的模板渲染 - location /template_test111 { - default_type 'text/html'; - content_by_lua ' - local template = require "resty.template" - template.render("index.html", { message = "Hello, World!" }) - '; - } - - # lua 的 capture 和 capture_multi(子查询) - location /capture { - content_by_lua_file "${project_path}lua_project_v0.01/application/ngx_lua_API/capture.lua"; - } - - location ~ \.php$ { - fastcgi_pass unix:/var/run/php7.0.9-fpm.sock; - fastcgi_index index.php; - fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; - include fastcgi_params; - } - - #-------------------------------------------------cookie 的使用 - location /cookie { - - } -} - diff --git a/conf/domains/nginx_index.conf b/conf/domains/nginx_index.conf deleted file mode 100644 index e314aa4..0000000 --- a/conf/domains/nginx_index.conf +++ /dev/null @@ -1,89 +0,0 @@ -# 设置Web缓存区名称为cache_one,内存缓存空间大小为200MB,1分钟没有被访问的内容自动清除,硬盘缓存空间大小为30GB -server { - listen 8083; - server_name 127.0.0.1; - charset utf8; - default_type text/html; - index chatroom.html index.htm; - - #加载模板文件 - set $template_root "/mnt/hgfs/Linux-Share/Lua/lua_project_v0.01/template"; - lua_code_cache off; - #--------------------------------------------个人简历(重要)----------------------------------------------------- - location /resume { - content_by_lua_file "/mnt/hgfs/Linux-Share/Lua/lua_project_v0.01/application/controller/ResumeController.lua"; - } - #--------------------------------------------个人简历(重要)----------------------------------------------------- - - location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ - { - root /mnt/hgfs/Linux-Share/Lua/lua_project_v0.01/public; - #expires 7d; - } - - # 2017TinywanInfo - location /2017TinywanInfo { - content_by_lua_file "/mnt/hgfs/Linux-Share/Lua/lua_project_v0.01/application/controller/InfoController.lua"; - } - location /index { - content_by_lua_block{ - local helper = require 'vendor.helper' - local array = helper.split('a,b,v,b',',') - for key,value in ipairs(array) - do - ngx.say(key, value) - end - ngx.say('==' .. array[1]) - } - } - - - - # dkjson 库使用 - location /dkjson_encode { - content_by_lua_block{ - local dkjson = require 'vendor.dkjson' - --lua对象到字符串 - local obj = { - id = 110, - name = "Tinywan", - age = 24, - is_male = false, - hobby = {"film", "music", "read"} - } - local str = dkjson.encode(obj, {indent = true}) - ngx.say(str, "
") - } - } - - # dkjson redis share 使用 - location /dkjson_decode_redis { - content_by_lua_file "/mnt/hgfs/Linux-Share/Lua/lua_project_v0.01/application/controller/RedisController.lua"; - } - - # dkjson redis share 使用 - location /dkjson_decode_redis2 { - content_by_lua_file "/mnt/hgfs/Linux-Share/Lua/lua_project_v0.01/application/controller/ProductController.lua"; - } - - # dkjson 库使用 - location /redis { - content_by_lua_block{ - local redis = require "resty.redis_iresty" - local red = redis:new() - - local res, err = red:auth("tinywan123456") - if not res then - ngx.say("failed to authenticate: ", err) - return - end - - local ok, err = red:set("tinywan123456", "NGINX-based OPenresty_short") - if not ok then - ngx.say("failed to set: ", err) - return - end - ngx.say("set result: ", ok) - } - } -} diff --git a/conf/domains/nginx_live.conf b/conf/domains/nginx_live.conf index 8f55416..577844e 100644 --- a/conf/domains/nginx_live.conf +++ b/conf/domains/nginx_live.conf @@ -11,8 +11,8 @@ server { index chatroom.html index.htm; #加载直播模板 - set $project_path /mnt/hgfs/Linux-Share/Lua/; # 定义项目根目录,如项目放在/home/www/ - set $template_root "/mnt/hgfs/Linux-Share/Lua/lua_project_v0.01/template/live"; + set $project_path /home/www/; # 定义项目根目录,如项目放在/home/www/ + set $template_root "/home/www/lua_project_v0.01/template/live"; #url映射 location ~* "^/live/(\d+)\.html$" { @@ -90,11 +90,9 @@ server { } # 聊天室配置 - location /live_chat_room { + location /live_redis { lua_code_cache off; - content_by_lua_file "${project_path}lua_project_v0.01/application/controller/ChatRoomController.lua"; + content_by_lua_file "${project_path}lua_project_v0.01/application/controller/test_redis.lua"; } - - } diff --git a/conf/domains/nginx_product.conf b/conf/domains/nginx_product.conf deleted file mode 100644 index 7a094bc..0000000 --- a/conf/domains/nginx_product.conf +++ /dev/null @@ -1,37 +0,0 @@ - - -# server product -server { - listen 8087; - server_name localhost; - charset gbk; - index chatroom.html index.htm index.phtm; - set $project_path /mnt/hgfs/Linux-Share/Lua/; - - #加载模板文件 - set $project_path /mnt/hgfs/Linux-Share/Lua/; # 定义项目根目录,如项目放在/home/www/ - set $template_root "${project_path}lua_project_v0.01/template/product"; - - #url映射 - location ~* "^/product/(\d+)\.html$" { - rewrite /product/(.*) http://127.0.0.1:8082/$1 permanent; - } - - # 项目的入口文件的调用 - location ~* "^/live/(\d{6,12})\.html$" { - default_type text/html; - set $skuId $1; - lua_code_cache off; - content_by_lua_file "${project_path}lua_project_v0.01/application/controller/ProductController.lua"; - } - - # item 显示列表 - location ~^/item/(\d+)$ { - #default_type application/x-javascript; - default_type text/html; - charset utf-8; - lua_code_cache on; - set $skuId $1; - content_by_lua_file "${project_path}lua_project_v0.01/application/controller/ItemController.lua"; - } -} diff --git a/conf/domains/upstream.conf b/conf/domains/upstream.conf deleted file mode 100644 index 82481b6..0000000 --- a/conf/domains/upstream.conf +++ /dev/null @@ -1,31 +0,0 @@ -# server live -server { - listen 8687; - server_name 127.0.0.1; - charset utf8; - default_type application/json; - - proxy_set_header Host $http_host; - proxy_set_header X-Forwarded-Port $server_port; - proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; - proxy_set_header X-Forwarded-Proto $scheme; - proxy_set_header X-Forwarded-Protocol $scheme; - proxy_set_header X-Real-IP $remote_addr; - proxy_read_timeout 30; - proxy_connect_timeout 10; - - lua_code_cache off; - - location ~ ^/0.1/([-_a-zA-Z0-9/]+)$ { - set $id $1; - content_by_lua_file /mnt/hgfs/Linux-Share/Lua/lua_project_v0.01/application/api/${id}.lua; - } - - location /UpstreamBackend { - lua_code_cache off; - resolver 8.8.8.8; # 不加resolver的话可能会报错, 无法解析,加一个8.8.8.8就可以搞定了 - set $backend ''; - rewrite_by_lua_file /mnt/hgfs/Linux-Share/Lua/lua_project_v0.01/application/controller/UpstreamBackendController.lua; - proxy_pass http://$backend; - } -} diff --git a/conf/domains/waf.conf b/conf/domains/waf.conf deleted file mode 100644 index 7084d29..0000000 --- a/conf/domains/waf.conf +++ /dev/null @@ -1,22 +0,0 @@ -#lua_shared_dict limit 10m; -#init_by_lua_file "/mnt/hgfs/Linux-Share/Lua/lua_project_v0.01/application/waf/init.lua"; -#access_by_lua_file "/mnt/hgfs/Linux-Share/Lua/lua_project_v0.01/application/waf/waf.lua"; -server { - listen 8082; - server_name localhost; - index index.php chatroom.html index.htm; - - set $web_root /mnt/hgfs/Linux-Share/Lua/lua_project_v0.01/template/waf; - root $web_root; - - location / { - try_files $uri $uri/ /index.php?$args; - } - - location ~ \.php$ { - fastcgi_pass unix:/var/run/php7.0.9-fpm.sock; - fastcgi_index index.php; - fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; - include fastcgi_params; - } -} diff --git a/conf/nginx.conf b/conf/nginx.conf index e5f21f6..14e7ab7 100644 --- a/conf/nginx.conf +++ b/conf/nginx.conf @@ -1,7 +1,7 @@ -user tinywan; +user www; worker_processes 8; -pid logs/nginx.pid; -error_log log/error.log error; +pid /home/www/lua_project_v0.01/logs/nginx.pid; +error_log /home/www/Linux-Share/Lua/lua_project_v0.01/logs/error.log error; events { use epoll; worker_connections 1024; @@ -10,12 +10,9 @@ events { http { include /opt/openresty/nginx/conf/mime.types; default_type text/html; - #lua_package_path "/home/tinywan/Openresty_Protect/First_Protect/lualib/?.lua;;"; #lua 模块 - #lua_package_cpath "/home/tinywan/Openresty_Protect/First_Protect/lualib/?.so;;"; #c模块 - #lua_package_path "/mnt/hgfs/Linux-Share/Lua/lua_project_v0.01/lualib/?.lua;;"; # lua 模块 - lua_package_path "/mnt/hgfs/Linux-Share/Lua/lua_project_v0.01/lualib/?.lua;/mnt/hgfs/Linux-Share/Lua/lua_project_v0.01/application/controller/?.lua"; # lua 模块 - lua_package_cpath "/mnt/hgfs/Linux-Share/Lua/lua_project_v0.01/lualib/?.so;;"; # c模块 + lua_package_path "/home/www/lua_project_v0.01/lualib/?.lua;/home/www/lua_project_v0.01/application/controller/?.lua"; + lua_package_cpath "/home/www/lua_project_v0.01/lualib/?.so;;"; - include "/mnt/hgfs/Linux-Share/Lua/lua_project_v0.01/conf/domains/*"; + include "/home/www/lua_project_v0.01/conf/domains/*"; } diff --git a/conf/nginx.conf.default b/conf/nginx.conf.default deleted file mode 100644 index e1001e7..0000000 --- a/conf/nginx.conf.default +++ /dev/null @@ -1,62 +0,0 @@ -# the size depends on the number of servers in upstream {}: -lua_shared_dict healthcheck 1m; -lua_shared_dict ip_blacklist 1m; -lua_socket_log_errors off; - -error_log /mnt/hgfs/Linux-Share/Lua/lua_project_v0.01/logs/error.log error; - -server { - listen 80; - server_name localhost; - - location / { - - } - - location /test_ip_location { - resolver 8.8.8.8; # dns - default_type "text/plain"; - content_by_lua_file "/mnt/hgfs/Linux-Share/Lua/lua_project_v0.01/application/ip_location.lua"; - } - - # redis option - location /get_redis_iresty { - default_type 'text/html'; - lua_code_cache off; - content_by_lua_file "/mnt/hgfs/Linux-Share/Lua/lua_project_v0.01/application/get_redis_iresty.lua"; - } - - location /get_main { - default_type 'text/html'; - lua_code_cache off; - content_by_lua_file /home/tinywan/Openresty_Protect/First_Protect/lua/main.lua; - } - - location /ip_blacklist { - default_type 'text/html'; - lua_code_cache off; - content_by_lua_file /home/tinywan/Openresty_Protect/First_Protect/lua/ip_blacklist.lua; - } - - - # shell - location /shell_test { - content_by_lua_block { - local shell = require("resty.shell") - local args = { - socket = "unix:/tmp/shell.sock", - } - local status, out, err = shell.execute("cat /proc/cpuinfo", args) - ngx.header.content_type = "text/plain" - ngx.say("Hello from:\n" .. out) - } - } - - location /ngx_timer_at { - default_type 'text/html'; - lua_code_cache off; - content_by_lua_file /home/tinywan/Openresty_Protect/First_Protect/lua/get_timer_at.lua; - } -} - - diff --git a/conf/nginx_lua_upstream.conf b/conf/nginx_lua_upstream.conf deleted file mode 100644 index 680949b..0000000 --- a/conf/nginx_lua_upstream.conf +++ /dev/null @@ -1,78 +0,0 @@ -error_log /mnt/hgfs/Linux-Share/Lua/lua_project_v0.01/logs/error.log error; - -# Nginx -upstream backend01 { # 配置后端服务器组 - #max_fails默认值为1,fail_timeout默认值为10s,max_fails=0表示不做检查 - server 127.0.0.1:8082 weight=1 max_fails=1 fail_timeout=10s; - server 127.0.0.1:8083 weight=1 max_fails=1 fail_timeout=10s; - server 127.0.0.1:8084 weight=1 max_fails=1 fail_timeout=10s; - keepalive 32; - #hash $request_uri consistent; -} - -# ngx.balancer 利用 openresty balancer_by_lua 实现一个简单的 ip_hash upstream 调度 -upstream backend{ - server 0.0.0.0; - balancer_by_lua_block { - local balancer = require "ngx.balancer" - local host = {"192.168.1.111", "192.168.1.112"} - local backend = "" - local port = ngx.var.server_port - local remote_ip = ngx.var.remote_addr - local key = remote_ip..port - local hash = ngx.crc32_long(key); - hash = (hash % 2) + 1 - backend = host[hash] - ngx.log(ngx.DEBUG, "ip_hash=", ngx.var.remote_addr, " hash=", hash, " up=", backend, ":", port) - local ok, err = balancer.set_current_peer(backend, port) - if not ok then - ngx.log(ngx.ERR, "failed to set the current peer: ", err) - return ngx.exit(500) - end - ngx.log(ngx.DEBUG, "current peer ", backend, ":", port) - } -} - -server { - listen 8081; - listen 8082; - listen 8083; - listen 8084; - server_name localhost; - location / { - proxy_pass http://backend; # 注意:proxy_pass后面的路径不带uri时,其会将location的uri传递给后端主机 - } -} - -# 配置虚拟服务器8082 -server { - listen 8082; - server_name localhost; - index chatroom.html index.htm; - set $web_root /opt/openresty/nginx; - location / { - root $web_root/html8082; - } -} - -# 配置虚拟服务器8083 -server { - listen 8083; - server_name localhost; - index chatroom.html index.htm; - set $web_root /opt/openresty/nginx; - location / { - root $web_root/html8083; - } -} - -# 配置虚拟服务器8084 -server { - listen 8084; - server_name localhost; - index chatroom.html index.htm; - set $web_root /opt/openresty/nginx; - location / { - root $web_root/html8084; - } -} diff --git a/lualib/vendor/wechat.lua b/lualib/vendor/wechat.lua new file mode 100644 index 0000000..7a41636 --- /dev/null +++ b/lualib/vendor/wechat.lua @@ -0,0 +1,51 @@ +-- Copyright (C) Shaobo Wan (Tinywan) + +local cjson = require("cjson") +local http = require "resty.http" +local log = ngx.log +local err = ngx.ERR +local exit = ngx.exit +local ngx_var = ngx.var +local print = ngx.print +local redirect = ngx.redirect + +local ok, new_tab = pcall(require, "table.new") +if not ok or type(new_tab) ~= "function" then + new_tab = function(narr, nrec) return {} end +end + + +local _M = new_tab(0, 54) + +_M._VERSION = '0.26' + +local scope = "snsapi_userinfo" +local appid = 'wx94c43716d8a91f3f' + +local function urlEncode(s) + s = string.gsub(s, "([^%w%.%- ])", function(c) return string.format("%%%02X", string.byte(c)) end) + return string.gsub(s, " ", "+") +end + +local function urlDecode(s) + s = string.gsub(s, '%%(%x%x)', function(h) return string.char(tonumber(h, 16)) end) + return s +end + +-- 获取access_token +function _M.get_access_token() +end + +-- 拉取用户信息 +function _M.get_user_info() + return print("拉去用户信息") +end + +-- 入口文件 +function _M.index() + local redirect_uri = urlEncode('http://wanwecaht.amai8.com/wechat/Index/getUserInfo'); + local url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" ..appid .."&redirect_uri="..redirect_uri .."&response_type=code&scope="..scope.."&state=1234#wechat_redirect"; + return redirect('/getUserInfo') +end + +return _M \ No newline at end of file