1.下载镜像
docker pull openresty/openresty
2.运行容器
docker run -it -d -p 8080:80 \
-v D:/openresty/conf/:/etc/nginx/conf.d/ \
--name openresty openresty/openrest
D:/openresty/conf/default.conf 文件如下
# nginx.vh.default.conf -- docker-openresty
#
# This file is installed to:
# `/etc/nginx/conf.d/default.conf`
#
# It tracks the `server` section of the upstream OpenResty's `nginx.conf`.
#
# This config (and any other configs in `etc/nginx/conf.d/`) is loaded by
# default by the `include` directive in `/usr/local/openresty/nginx/conf/nginx.conf`.
#
# See https://github.com/openresty/docker-openresty/blob/master/README.md#nginx-config-files
#lua_shared_dict dis_cache 512m;
server {
listen 80;
server_name localhost;#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;location / {
# root /usr/local/openresty/nginx/html;
# index index.html index.htm;
content_by_lua_file /etc/nginx/conf.d/short_chain.lua;
}#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 /usr/local/openresty/nginx/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 /usr/local/openresty/nginx/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;
#}
}
D:/openresty/conf/short_chain.lua 文件如下
local redis = require "resty.redis"
local redConn = redis:new()
local ok, err = redConn:connect("127.0.0.1", "6379")
redConn:auth("123456")
if not ok then
ngx.log(ngx.ERR, "redis failed to connect: ", err)
return ngx.exit(500)
end
local mysql = require "resty.mysql"
local db = mysql:new()
db:set_timeout(2000)
local props = {
host = 127.0.0.1,
port = 3306,
database = "short_chain",
user = "root",
password = "123456"
}
local ok, err, errcode, sqlstate = db:connect(props)
if not ok then
ngx.log(ngx.ERR, "mysql failed to connect: ", err, ": ", errcode, " ", sqlstate)
return ngx.exit(500)
end
local code = string.match(ngx.var.request_uri, ".*", 2)local cache_ngx = ngx.shared.dis_cache
local ngx_custom_cache = cache_ngx:get('custom_cache_' .. code)
if ngx_custom_cache == nil or ngx_custom_cache == "" then
local res, err = redConn:get("custom_cache:" .. code);
if res ~= ngx.null then
ngx.log(ngx.ERR, "命中redis缓存")
cache_ngx:set('custom_cache_' .. code, res, 10 * 60);
redConn:set_keepalive(2000, 20)
return ngx.redirect(res, 302)
else
ngx.log(ngx.ERR, "命中mysql")
local select_sql = "select url from `short_chain` where id='" .. code .. "' and deleted=0"
res, err, errcode, sqlstate = db:query(select_sql)
db:set_keepalive(2000, 20)
local url = res[1]['url']
if url == nil or url ~= ngx.null then
redConn:set("custom_cache:" .. code, url)
redConn:expire("custom_cache:" .. code,3600)
redConn:set_keepalive(2000, 20)
return ngx.redirect(url, 302)
end
redConn:set_keepalive(2000, 20)
ngx.exit(ngx.HTTP_FORBIDDEN)
return
end
else
ngx.log(ngx.ERR, "命中代理缓存")
return ngx.redirect(ngx_custom_cache, 302)
end
http:127.0.0.1:8080/1ydcqy8L2uG 被重定向到目标地址