openresty+redis+lua构建企业预发布环境

hcwei 2020年07月29日 90次浏览

1. 安装OpenResty

OpenResty是一个基于 Nginx 与 Lua 的高性能 Web 平台,其内部集成了大量精良的 Lua 库、第三方模块以及大多数的依赖项。用于方便地搭建能够处理超高并发、扩展性极高的动态 Web 应用、Web 服务和动态网关。通过汇聚各种设计精良的 Nginx 模块,从而将 Nginx 有效地变成一个强大的通用 Web 应用平台。这样,Web 开发人员和系统工程师可以使用 Lua 脚本语言调动 Nginx 支持的各种 C 以及 Lua 模块,快速构造出足以胜任 10K 乃至 1000K 以上单机并发连接的高性能 Web 应用系统。OpenResty的目标是让你的Web服务直接跑在 Nginx 服务内部,充分利用 Nginx 的非阻塞 I/O 模型,不仅仅对 HTTP 客户端请求,甚至于对远程后端诸如 MySQL、PostgreSQL、Memcached 以及 Redis 等都进行一致的高性能响应。

yum install pcre-devel openssl-devel gcc curl
wget https://openresty.org/download/openresty-1.17.8.2.tar.gz
tar xf openresty-1.17.8.2.tar.gz
cd openresty-1.17.8.2
./configure --prefix=/mnt/installer/openresty
make
make install

image.png

2. 安装redis

这里为了方便直接使用yum安装

yum install -y epel-release
yum install -y redis
systemctl start redis
systemctl enable redis
systemctl status redis

查看状态如下图说明启动成功
image.png

3.nginx配置

lua获取请求类型:

ngx.var.request_method

lua获取GET请求参数:

ngx.var.arg_参数名

lua获取POST请求参数:

ngx.req.read_body()
ngx.req.get_post_args().参数名

lua获取请求头数据:

ngx.req.get_headers()["参数名"]

lua连接redis

local redis = require "resty.redis"
local redisobj = redis:new()
red:set_timeouts(1000, 1000, 1000)
local ok, err = red:connect("host地址", 端口)
ok, err = red:auth("密码")
if not ok then
	ngx.say("failed to connect: ", err)
	return
end
--查询
local res, err = redisobj:get("key")
--插入
redisobj.set("key","value")

nginx配置样例

#测试环境
upstream devserver{
        server 172.16.0.1:8888;
}

#预发布环境
upstream preserver{
        server 172.16.0.2:8888;
}

server {
        listen       80;
        server_name  www.xxx.com;
        proxy_buffer_size   128k;
        proxy_buffers   4 256k;
        proxy_busy_buffers_size   256k;

        location / {
            default_type text/html;
	    content_by_lua_block {    
                local redis = require "resty.redis"
                local redisobj = redis:new()
                red:set_timeouts(1000, 1000, 1000)
                local ok, err = red:connect("127.0.0.1", 6379)
		ok, err = red:auth("password")
                if not ok then
                    ngx.say("failed to connect: ", err)
                    return
                end

		local local_ip = ngx.req.get_headers()["X-Real-IP"]
		if local_ip == nil then
    			local_ip = ngx.req.get_headers()["x_forwarded_for"]
		end
		if local_ip == nil then
    			local_ip = ngx.var.remote_addr
		end
                
		local res, err = redisobj:get(local_ip)
		if res == "1" then
                    ngx.exec("@preserver")
 		    return
                end
                ngx.exec("@devserver")
          }
        }

        location @preserver {
  	    proxy_pass http://preserver;
            proxy_set_header Host $host;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
	    proxy_set_header User-Agent $http_user_agent;
	    proxy_set_header terrace_type $http_terrace_type;
	    proxy_set_header token $http_token;
	    proxy_set_header version $http_version;
            proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504 http_404;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_redirect off;
            proxy_buffering off;
	}

        location @devserver {
  	    proxy_pass http://devserver;
            proxy_set_header Host $host;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
	    proxy_set_header User-Agent $http_user_agent;
	    proxy_set_header terrace_type $http_terrace_type;
	    proxy_set_header token $http_token;
	    proxy_set_header version $http_version;
            proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504 http_404;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_redirect off;
            proxy_buffering off;
	}
}