nginx里使用openresty-lua-redis等

安装
version: "3"
services:
  nginx_lua:
    image: openresty/openresty:alpine
    #image: openresty/openresty:latest #没有安装opm

以下命令可以在Dockerfile编写, 前缀以RUN补充,使其创建新的镜像
或者在运行后,进容器后直接运行

安装opm

curl -L -o /usr/local/bin/opm https://raw.githubusercontent.com/openresty/opm/master/opm  &&  chmod +x /usr/local/bin/opm

安装基础工具,以避免以下面的报错

apk add --no-cache bash perl  curl
#openresty:alpine版本的包管理工具apk,如使用latest,包管理工具是apt

报错1:
$ opm -h
can’t execute ‘perl’: No such file or directory
报错2:
ERROR: failed to find package: server error: sh: curl: not found

通过opm去查找依赖库

7793bfb21d5b:/# opm search base64
openresty/lua-resty-core                          New FFI-based Lua API for the ngx_lua module
bungle/lua-resty-nettle                           Nettle (a low-level cryptographic library) Bindings for LuaJIT FFI
bungle/lua-resty-session                          Session Library for OpenResty - Flexible and Secure
jprjr/luasodium                                   An FFI binding to libsodium.
GUI/lua-resty-txid                                Generate sortable, unique transaction or request IDs.
xiaocang/lua-resty-openssl                        FFI-based OpenSSL binding for LuaJIT
fffonion/lua-resty-openssl                        FFI-based OpenSSL binding for LuaJIT
fffonion/lua-resty-acme                           Automatic Let's Encrypt certificate serving and Lua implementation of ACME procotol
hamishforbes/lua-resty-consul                     Library to interface with the consul HTTP API
lilien1010/lua-resty-s3uploader                   an http s3 client for openresty
duhoobo/lua-resty-smtp                            A http to smtp bridge library for the ngx_lua module

安装时,需要加上account name

7793bfb21d5b:/# opm install lua-resty-random
ERROR: package name lua-resty-random is not prefixed by an account name.
nginx里使用lua
  1. 正常的响应: ngx.say(“Hello, OpenResty!”)
  2. 重定向: ngx.redirect(long_url, ngx.HTTP_MOVED_PERMANENTLY)
  3. 生成随机数:
	local random_bytes = {}
	for i = 1, 6 do
	  table.insert(random_bytes, string.char(math.random(0, 255)))
	end
	random_bytes = table.concat(random_bytes)
  1. Base64编码: ngx.encode_base64(random_bytes)
  2. 截取前5个字符: string.sub(encoded_bytes, 1, 5)
  3. table将k-v加入res对象: table.insert(result, key … ", " … value)
  4. table合并为字符串: table.concat(result, “\n”)
redis相关操作
  1. 引入redis
	local redis = require("resty.redis")
	local red = redis:new()
  1. 连接redis
red:connect("192.168.0.xx", 6379)
  1. redis写入,并设置key7天过期
	local saved, err = red:setex(key, 604800, long_url)
  1. redis读取
	local long_url, err = red:get("code:" .. code)
  1. 关闭Redis连接
	local ok, err = red:set_keepalive(10000, 100)
server {
    listen 80;
    server_name 0.0.0.0;
    location / {
	  default_type  text/html;
	  content_by_lua_block {
	      ngx.say("Hello, OpenResty!")
	  }
    }
	location /s/ {
      # 使用Lua模块
      content_by_lua_block {
        -- 执行重定向
		return ngx.redirect("http://example.com", ngx.HTTP_MOVED_PERMANENTLY)
	  }
	}
    location /s/create {
      # 使用Lua模块
      content_by_lua_block {
		local redis = require("resty.redis")
		local red = redis:new()
		
		-- 连接到Redis服务器
		local ok, err = red:connect("192.168.0.xx", 6379)
		if not ok then
			ngx.log(ngx.ERR, "Failed to connect to Redis: ", err)
			return ngx.exit(500)
		end
		# ok = 1
		ngx.log(ngx.INFO, "connect to Redis: ", ok)
		ngx.say("Hello, OpenResty!")
		}
	}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值