短链访问服务之openresty

这篇具有很好参考价值的文章主要介绍了短链访问服务之openresty。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

一、openresty 安装(docker)

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

二、短链服务lua脚本编写如下

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 被重定向到目标地址文章来源地址https://www.toymoban.com/news/detail-529973.html

到了这里,关于短链访问服务之openresty的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Windows 环境下nginx 静态资源服务器(图片,文件)权限控制(nginx/openresty/lua)

    1 同nginx配置server以后,我们可以很方便的直接访问到文件服务器上的文件资源,但是某些情况下,文件资源可能是隐私图片,比如客户注册时上传的身份证照片等等,这时候我们需要对图片访问进行控制,必须登录后才能查看到这些隐私图片。 2 一般来说,我们都是通过后端

    2024年02月09日
    浏览(39)
  • 微信短链跳转到小程序指定页面调试

    首先说下背景:后端给了短链地址,但是无法跳转到指定页面。总是在小程序首页。指定的页面我们是h5页面。排查步骤如下: 1、通过快速URL Scheme 编译。上部普通编译 下拉找到此选项。 、 2、按照小程序的要求的URL Scheme输入。另外后端给的短链打开之后,拷贝尾缀作为t

    2024年02月10日
    浏览(38)
  • 『SuperShortLink』.NET开源的超级短链系统——快速实现长短链的转换及监控

    📣读完这篇文章里你能收获到 了解博主的开源短链项目SuperShortLink 学习长链转短链、短链跳转长链、短链访问统计的原理及方法 掌握内部其他项目的多种接入方式 这是博主开源的一个基于.NET开源的短链生成及监控系统,它包含了在线生成短链、短链跳转长链、支持短链访

    2023年04月22日
    浏览(25)
  • openresty 安装, nginx与 openresty

    openresty VS nginx Nginx 是一款高性能的 Web 服务器和反向代理服务器,具备基础的功能如HTTP服务、负载均衡、反向代理以及动静分离等。它是许多互联网应用的核心组件,因其模块化和可扩展的设计而受到欢迎。1 OpenResty 是基于 Nginx 的 Web 平台,它通过Lua脚本语言扩展了Nginx的功

    2024年01月25日
    浏览(22)
  • 使用openresty替换线上nginx网关之openresty安装细节

    线上跑了多年的一个网关业务,随着部门的拆分,逐渐有了一个痛点。该网关业务主要处理app端请求,app端发起的请求,采用http协议,post方法,content-type采用 application/x-www-form-urlencoded ,表单中有一个固定的字段,叫功能号,即funcNo=1000100这样,然后表单中其他业务字段就根

    2024年02月07日
    浏览(31)
  • 项目部署之OpenResty

    OpenResty® 是一个基于 Nginx 的高性能 Web 平台,用于方便地搭建能够处理超高并发、扩展性极高的动态 Web 应用、 Web 服务和动态网关。具备下列特点: 具备 Nginx 的完整功能 基于 Lua 语言进行扩展,集成了大量精良的 Lua 库、第三方模块 允许使用 Lua 自定义业务逻辑 、 自定义库

    2024年02月07日
    浏览(24)
  • openresty安装与网站发布

    OpenResty® 是一个基于 Nginx 与 Lua 的高性能 Web 平台,其内部集成了大量精良的 Lua 库、第三方模块以及大多数的依赖项。用于方便地搭建能够处理超高并发、扩展性极高的动态 Web 应用、Web 服务和动态网关。 OpenResty® 通过汇聚各种设计精良的 Nginx 模块(主要由 OpenResty 团队自

    2024年02月11日
    浏览(28)
  • OpenResty shared dict

    shared dict 支持数据的存放和读取,还支持原子计数和队列操作,可用于 worker 间的通信。 Nginx 中的变量 可以在 Nginx C 模块之间共享数据,也可以在 C 模块和 lua-nginx-module 之间共享数据。 性能较差,只能存储字符串,不支持其他 Lua 类型。 ngx.ctx 在同一个请求的不同阶段共享数

    2024年02月12日
    浏览(24)
  • 浅尝OpenResty

    当一个域名中衍生出多个服务的时候,如果想要保持对外服务始终是一个域名,则需要通过nginx反向代理来实现。如果在转发的时候需要对具体的规则进行一些逻辑运算的话,则需要通过嵌入lua脚本来实现,而nginx本身是不支持lua功能的,目前可以通过: nginx + lua module来实现

    2024年02月11日
    浏览(29)
  • 1.OpenResty系列之入门简介

    OpenResty(也称为ngx_openresty)是一个基于Nginx的全功能Web应用服务器,它将Nginx与一组附加模块和Lua脚本语言集成在一起,以提供高性能的Web应用程序开发和扩展性。 Nginx是一个轻量级的、高性能的HTTP服务器和反向代理服务器,广泛用于构建高流量网站和应用程序。OpenResty利用

    2024年02月05日
    浏览(31)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包