[toc]
nginx + lua + redis 实现短链接转发长链接
重新编译安装 nginx 及相关依赖
安装相关依赖包
# yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel lua-devel luajit-devel pcre pcre-devel libxml2 libxml2-dev libxslt-devel gd-devel perl-devel perl-ExtUtils-Embed GeoIP GeoIP-devel GeoIP-data gperftools
下载nginx_devel_kit lua-nginx-module
# wget https://github.com/simplresty/ngx_devel_kit/archive/master.zip # wget https://github.com/openresty/lua-nginx-module/archive/v0.10.13.tar.gz # unzip master.zip && mv ngx_devel_kit-master/ /usr/local/ # tar -xvf v0.10.13.tar.gz -C /usr/local/
重新安装 nginx
# cd /usr/local/src/nginx-1.12.2 # ./configure --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx --with-file-aio --with-ipv6 --with-http_auth_request_module --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module=dynamic --with-http_image_filter_module=dynamic --with-http_geoip_module=dynamic --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_slice_module --with-http_stub_status_module --with-http_perl_module=dynamic --with-mail=dynamic --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module --with-google_perftools_module --with-debug --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -m64 -mtune=generic' --with-ld-opt='-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E' --add-module=/usr/local/lua-nginx-module-0.10.13 --add-module=/usr/local/ngx_devel_kit-master # make && make install
安装lua-resty-redis
# cd /usr/local/src # wget https://github.com/openresty/lua-resty-redis/archive/master.zip # unzip master.zip # cd lua-resty-redis-master/ # make && make install make: Nothing to be done for `all'. install -d /usr/local/lib/lua//resty install lib/resty/*.lua /usr/local/lib/lua//resty
修改nginx http 段内配置文件
http内添加 lua_package_path "/usr/local/lib/lua/?.lua;;";
;;
代表的是LuaJIT安装时的原始搜索路径
# nginx -t && nginx -s reload
修改nginx server或location 字段
location /lua { default_type text/plain; content_by_lua_file /usr/local/lua/request.lua; }
request.lua
# vim /usr/local/lua/request.lua local redis = require "resty.redis" local cache = redis:new() -- redis连接 local ok,err = cache.connect(cache, '127.0.0.1', '25612') if not ok then ngx.say("failed to connect redis cache:", err); return end -- redis认证 local count count, err = cache:get_reused_times() if 0 == count then ok,err = cache:auth("xxxxx") if not ok then ngx.say("failed to auth redis: " , err) return end elseif err then ngx.say("failed to get reused times: ", err) return end -- 选择使用的redis数据库 cache:select(7) -- 当前访问地址 local url = ngx.var.uri -- 主机名 local host = ngx.var.host -- 链接参数 local args = ngx.var.args -- 获取方法 local sch =ngx.var.scheme -- 获取端口 --local pot =ngx.var.server_port -- 定义通用完整 url 路径 local url_full_path = sch .. "://" .. host .. url -- 获取redis 中的值 local res, err = cache:get(url_full_path) -- ngx.req.set_uri(url) if res then --ngx.say(sch .. "://" .. host .. url .. " " .. res) ngx.redirect(res,302) end # nginx -s reload
在redis 写入key 并测试
# redis-cli -h 127.0.0.1 -p 25612 127.0.0.1:25612> AUTH xxxxxxxxxx OK 127.0.0.1:25612> SELECT 7 OK 127.0.0.1:25612[7]> set https://www.ssjinyao.com/lua/test https://www.ssjinyao.com/2019/03/16/%E5%85%B3%E4%BA%8E%E6%9F%90%E7%B3%BB%E7%BB%9F%E9%98%B5%E5%8F%91%E6%80%7%E7%BD%91%E7%BB%9C%E5%A4%84%E7%90%86/ OK
可以看到访问时的中转
补充nginx rewrite转发正则匹配
rewrite ^/([0-9][0-9][0-9][0-9])/([0-1][0-9][0-9][0-9])/([0-9]+\.html) http://test.ssjinyao.com/$1/$2/$3 permanent;