Nginx + Lua achieve gray Published

General server version update iteration, the need to replace the old version to the new version of the directory, allowing all users to use the new version. If the new version has a bug, or a new version users are not accustomed to result in a poor user experience or even lost. The time between the gray between black and white that were released, allowing some users to use the new version, others continue using the old version, this new version can also be a problem with the losses to a minimum.

heel

Lua script basic grammar

Lua is a compact, lightweight, extensible scripting language
directly yum install lua -yinstalled in the base library. CentOS7.2 or later installed by default

## 命令行交互模式运行
[root@Nginx ~]# lua
Lua 5.1.4 Copyright (C) 1994-2008 Lua.org, PUC-Rio
> print("hello world")
hello world
## 脚本方式运行
[root@Nginx ~]# vim test.lua
#!/usr/bin/lua
print("hello world")
[root@Nginx ~]# lua test.lua 
hello world

Judgment statement

if  如果你有房 then
    我就嫁给你
elseif  你有车 or 你有钱 then
    我就嫁给你
else
    再见
end

while loop

## 1到100之间所有数字相加
[root@Nginx ~]# vim while.lua
#!/usr/bin/lua
sum=0
num=1
while num <=100 do
        sum=sum+num
        num=num+1
end
print("sum="..sum)

for loop

## 1到100之间所有数字相加
[root@Nginx ~]# vim for.lua
#!/usr/bin/lua
sum=0
for i=1,100 do
        sum=sum+i
end
print("sum="..sum)

Nginx + Lua environment to build

By default, Nginx does not support Lua module, you need to install LuaJIT interpreter, and the need to recompile Nginx

Installation dependencies

[root@Nginx ~]# yum -y install gcc gcc-c++ make pcre-devel zlib-devel openssl-devel 

Download and unzip the latest package

## 创建目录
[root@Nginx ~]# mkdir /soft/src -p && cd /soft/src
## 下载 luajit 和 ngx_devel_kit 以及 lua-nginx-module
[root@Nginx src]# wget http://luajit.org/download/LuaJIT-2.0.5.tar.gz
[root@Nginx src]# wget https://github.com/simplresty/ngx_devel_kit/archive/v0.3.1rc1.tar.gz
[root@Nginx src]# wget https://github.com/openresty/lua-nginx-module/archive/v0.10.15.tar.gz
## 解压 luajit 和 ngx_devel_kit 以及 lua-nginx-module
[root@Nginx src]# tar xf v0.10.15.tar.gz
[root@Nginx src]# tar xf v0.3.1rc1.tar.gz
[root@Nginx src]# tar xf LuaJIT-2.0.5.tar.gz

Installation Lua -time compiler luajit

[root@Nginx src]# cd LuaJIT-2.0.5/
[root@Nginx LuaJIT-2.0.5]# make && make install

Compile and install Nginx

## 返回原来的目录
[root@Nginx LuaJIT-2.0.5]# cd /soft/src
## 下载Nginx源码包
[root@Nginx src]# wget http://nginx.org/download/nginx-1.16.0.tar.gz
## 解压并进入源码包
[root@Nginx src]# tar xf nginx-1.16.0.tar.gz
[root@Nginx src]# cd nginx-1.16.0/
## 安装源码包
[root@Nginx nginx-1.16.0]# ./configure --prefix=/etc/nginx \
 --with-http_ssl_module --with-http_stub_status_module --with-http_dav_module \
--add-module=../ngx_devel_kit-0.3.1rc1/ --add-module=../lua-nginx-module-0.10.15
[root@Nginx nginx-1.16.0]# make && make install
## 建立软链接
[root@Nginx nginx-1.16.0]# ln -s /usr/local/lib/libluajit-5.1.so.2 /lib64/libluajit-5.1.so.2
## 内核加入lua库
[root@Nginx nginx-1.16.0]# echo "/usr/local/LuaJIT/lib" >> /etc/ld.so.conf

Nginx call Lua module syntax and variables

  • grammar:
    • set_by_lua --- set Nginx variable
    • set_by_lua_file --- specify settings Nginx variable file
    • access_by_lua --- request processing of the access phase, the access control can be provided
    • access_by_lua_file --- specify access control file
    • content_by_lua --- returned content
    • content_by_lua_file --- return the contents of the specified file
  • variable
    • --- ngx.var Nginx variable
    • acquisition request header ngx.req.get_headers ---
    • ngx.req.get_url_args --- get url request parameters
    • ngx.redirect --- redirection
    • response output content ngx.print ---
    • ngx.say --- output response content, and finally add linefeed
    • output response header ngx.header ---

Test Nginx and Lua whether the connection is successful

[root@Nginx ~]# vim /etc/nginx/conf/nginx.conf
## 在配置文件server标签里下面内容
        location /test {
                default_type text/html;
                content_by_lua_block {
                        ngx.say("Hello World")
                }

## 启动Nginx
[root@Nginx ~]# /etc/nginx/sbin/nginx

Browser access IP地址/testwhether the Hello World (note closed selinux and firewalld)

Nginx + Lua achieve gray Published

Nginx + Lua achieve gray Published works:
When a user requests access to front-end proxy Nginx , the embedded Lua module parses the Nginx configuration file Lua scripting, Lua script will get the client IP address, see Memcached the presence of the key cache, If there will be a reverse proxy to a new version of the upstream pool, then the reverse proxy to the old version of the upstream pool does not exist.

This only major release is gray, the other two nodes is not important what the web container, can access on the line

New and old versions of the server configuration

## 关闭防火墙和selinux
[root@node ~]# setenforce 0
[root@node ~]# systemctl stop firewalld
## 安装启动httpd
[root@node ~]# yum install httpd -y
[root@node ~]# systemctl start httpd
## 新版本配置
[root@node ~]# echo "这是新版本" > /var/www/html/index.html
## 老版本配置
[root@node ~]# echo "这是老版本" > /var/www/html/index.html

These are the two-node configuration can access the IP would not move them anymore

Configure Memcached

## 安装Memcached
[root@Nginx src]# yum install memcached -y
## 下载Lua和memcached连接库,让Lua可以连接Memcached
[root@Nginx src]# cd /soft/src
[root@Nginx src]# wget https://github.com/openresty/lua-resty-memcached/archive/v0.14.tar.gz
[root@Nginx src]# tar xf v0.14.tar.gz
[root@Nginx src]# mkdir /etc/nginx/conf/lua
[root@Nginx src]# cp -r lua-resty-memcached-0.14/lib/resty/memcached.lua /etc/nginx/conf/lua/
## 启动Memcached
[root@Nginx src]# systemctl start memcached
[root@Nginx src]# systemctl enable memcached

Configure Nginx Load Balancing

## 把Nginx配置文件server标签和server标签内容都删了换成下面引用其他配置文件
    include conf.d/*.conf
## 创建引用目录
[root@Nginx ~]# mkdir /etc/nginx/conf/conf.d
## 编写Nginx配置文件
[root@Nginx ~]# vim /etc/nginx/conf/conf.d/lua.conf
## 调用刚刚复制的memcached.lua文件
lua_package_path "/etc/nginx/conf/lua/memcached.lua";
upstream new_version {
    server 192.168.1.2:80;
}
upstream old_version {
    server 192.168.1.3:80;
}
server {
    listen 80;
    server_name 192.168.1.1;
    location / {
        default_type 'text/plain';
        content_by_lua_file /etc/nginx/conf/lua/index.lua;   ## 访问网站交给这个lua脚本
    }
    location @new_version {
        proxy_pass http://new_version;
        proxy_set_header Host $http_host;
    }
    location @old_version {
        proxy_pass http://old_version;
        proxy_set_header Host $http_host;
    }
}

Write Lua script

[root@Nginx ~]# vim /etc/nginx/conf/lua/index.lua
-- 获取客户端头部信息的 x-real-ip
clientIP = ngx.req.get_headers()["X-Real-IP"]
-- 如果clientIP没有获取到客户端IP就获取 x_forwarded_for
if clientIP == nil then
clientIP = ngx.req.get_headers()["x_forwarded_for"]
end
-- 如果clientIP还是没有获取到客户端IP就获取 remote_addr
if clientIP == nil then
clientIP = ngx.var.remote_addr
end
-- 本地加载 memcached
local memcached = require "resty.memcached"
-- 实例化对象
local memc, err = memcached:new()
-- 判断连接是否存在错误
if not memc then
ngx.say("failed to instantiate memc: "..err)
return
 end
-- 建立 memcache 连接
local ok, err = memc:connect("127.0.0.1", 11211)
-- 无法连接往前端抛出错误信息
if not ok then
ngx.say("failed to connect: "..err)
return
end
-- 获取对象中的 ip 存在值赋给 res
local res, flags, err = memc:get(clientIP)
if err then
ngx.say("failed to get clientIP "..err)
return
end
-- 如果 res 值为 1 则调用 local-@new_version
if res == "1" then
ngx.exec("@new_version")
return
end
-- 否则调用 local-@old_version
ngx.exec("@old_version")
return

Start Nginx after the browser can test it

[root@Nginx ~]# /etc/nginx/sbin/nginx

The IP added Memcached allowed to access the new version

## 安装telnet(没有Memcached客户端用telnet暂时代替一下)
[root@Nginx ~]# yum install telnet -y
## 连接Memcached
[root@Nginx ~]# telnet 127.0.0.1 11211
## set 客户端访问的IP(后面的分别是flags、exptime、bytes对应的值,不用管,这不重要)
set 192.168.1.254 0 0 1
## 填写IP键的值(出现STORED就可以了)
1

Guess you like

Origin www.cnblogs.com/songguoyou/p/11883213.html