- pull docker 镜像
version: '3'
services:
openresty_server:
image: openresty/openresty:1.25.3.1
command: [openresty, '-g', 'daemon off;']
ports:
- "${SERVER_PORT}:80"
volumes:
- ./nginx/conf.d:/etc/nginx/conf.d
- ./nginx/nginx.conf:/usr/local/openresty/nginx/conf/nginx.conf
- ./logs/nginx:/usr/local/openresty/nginx/logs
- ./lualibs:/usr/local/openresty/nginx/lualibs
- /etc/localtime:/etc/localtime
user: root
network_mode: bridge
#working_dir: /app
links:
- openresty_kvrocks
openresty_kvrocks:
image: apache/kvrocks:2.8.0
hostname: openresty_kvrocks
command:
- "--bind"
- "0.0.0.0"
- "--dir"
- "/data/kvrocks"
- "--workers"
- "16"
ports:
- "${KVROCKS_PORT}:6666"
volumes:
- ./data/kvrocks:/data/kvrocks
#environment:
# - ""
user: root
network_mode: bridge
- 增加 nginx 的 gw.conf
upstream simple_server {
server 172.17.0.1:6930 weight=1;
}
#
#init_by_lua_file lualibs/init.lua;
#
init_by_lua_block {
package.path = package.path .. ";/usr/local/openresty/nginx/lualibs/?.lua"
local resol = require "local-resolver"
-- expose global as we expect
resolver = resol.new("/etc/hosts")
}
#
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location /hello {
default_type text/html;
content_by_lua_block {
ngx.say("<p>hello, world</p>")
}
}
location / {
access_by_lua_block {
--local resolver = require "local-resolver"
--resolver = resolver.new("/etc/hosts")
local redis = require "resty.redis"
local red = redis:new()
local ok, err = red:connect(resolver:resolve("openresty_kvrocks"), 6666)
if not ok then
--ngx.say("HTTP_UNAUTHORIZED: ", "x")
--ngx.say("failed to connect cache: ", err)
ngx.log(ngx.ERR, err)
ngx.exit(ngx.HTTP_FORBIDDEN)
return
end
local token = ngx.req.get_headers()['Token']
if token == nil then
token = "null"
end
local key = "resty:auth:access:token:" .. token
local res, err = red:get(key)
if not res then
ngx.log(ngx.ERR, "get redis key failed: " .. key)
ngx.exit(ngx.HTTP_FORBIDDEN)
return
end
if res == ngx.null then
ngx.log(ngx.ERR, "get redis key empty: " .. key)
ngx.exit(ngx.HTTP_FORBIDDEN)
return
end
--ngx.say(ngx.req.get_headers()['Token'])
}
proxy_pass http://simple_server;
proxy_connect_timeout 18000;
proxy_send_timeout 18000;
proxy_read_timeout 18000;
client_max_body_size 10240m;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/local/openresty/nginx/html;
}
}