Nginx + Lua 脚本 动态配置
一、安装LuaJIT2
# 安装下载目录
cd /usr/local/src
# 下载地址
wget https://github.com/openresty/luajit2/archive/refs/tags/v2.1-20201229.tar.gz
# 解压缩文件夹
tar -zxvf v2.1-20201229.tar.gz
# 编译和安装
make PREFIX=/usr/local/luajit
make install PREFIX=/usr/local/luajit
# 安装lua-devel
yum install lua-devel
# 配置环境变量
vim /etc/profile
# 添加下方代码
export LUAJIT_LIB=/usr/local/luajit/lib
export LUAJIT_INC=/usr/local/luajit/include/luajit-2.1
# 使/etc/profile 立刻生效
source /etc/profile
# 检查安装情况
lua -v
二、安装Nginx
1.下载nginx
已经安装过nginx的可以看 “4.追加nginx-module”
# 安装依赖库 这些依赖库主要有g++、gcc、openssl-devel、pcre-devel和zlib-devel
yum -y install make gcc gcc-c++ glibc glibc-devel lsof
yum -y install pcre pcre-devel
yum -y install zlib zlib-devel
yum -y install openssl openssl--devel
# 下载源码包
cd /usr/local/src
wget -d "http://nginx.org/download/nginx-1.8.0.tar.gz"
# 解压 nginx-1.8.0
tar zxvf nginx-1.8.0.tar.gz
2.Nginx Module下载
mkdir -p /usr/local/src/nginx-modules
# ngx_devel_kit 下载
wget https://github.com/vision5/ngx_devel_kit/archive/v0.3.1.tar.gz
tar -xvf v0.3.1.tar.gz
# lua-nginx-module 下载
wget https://github.com/openresty/lua-nginx-module/archive/v0.10.14.tar.gz
tar -xvf v0.10.14.tar.gz
# headers-more-nginx-module 下载
wget https://github.com/openresty/headers-more-nginx-module/archive/v0.33.tar.gz
tar -zxvf v0.33.tar.gz
3.编译nginx
# 编译nginx
./configure \
--prefix=/usr/local/nginx \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-file-aio \
--with-http_sub_module \
--with-http_gzip_static_module \
--with-http_v2_module \
--with-pcre \
--with-http_realip_module \
--with-stream \
--with-stream_ssl_module \
--with-ld-opt="-Wl,-rpath,/usr/local/luajit/lib" \
--add-module=/usr/local/src/nginx-modules/headers-more-nginx-module-0.33 \
--add-module=/usr/local/src/nginx-modules/ngx_devel_kit-0.3.1 \
--add-module=/usr/local/src/nginx-modules/lua-nginx-module-0.10.14
# 安装
make
make install
4.追加nginx-module
# 进入nginx解压目录
cd /usr/local/src/nginx-1.8.0
# 追加nginx-module
./configure
--prefix=/usr/local/src/nginx-modules
--add-module=/usr/local/src/nginx-modules/headers-more-nginx-module-0.33 \
--add-module=/usr/local/src/nginx-modules/ngx_devel_kit-0.3.1 \
--add-module=/usr/local/src/nginx-modules/lua-nginx-module-0.10.14
# 编辑,切记没有make install
make
5.Nginx命令
#启动nginx
nginx
#停止nginx
nginx -s stop
#重启nginx
nginx -s reload
6.验证安装情况
cd /usr/local/nginx/sbin/
nginx -v
1.在nginx配置文件中 server段 添加如下代码
location /lua {
default_type 'text/plain';
content_by_lua 'ngx.say("hello,lua!!!")';
}
2.重启nginx
./nginx -s reload
3.访问:xxx.xxx.xxx.xxx:端口/lua
三、配置
1.Vue反向代理 开关控制
worker_processes 5;
error_log logs/error.log;
error_log logs/error.log notice;
error_log logs/error.log info;
# events事件指令是设定Nginx的工作模式及连接数上限
events {
worker_connections 512;
}
http {
# lua 全局缓存
lua_shared_dict dyn_ups_zone 10m;
# include是个主模块指令,实现对配置文件所包含的文件的设定,可以减少主配置文件的复杂度。类似于Apache中的include方法。
include mime.types;
default_type application/octet-stream;
# sendfile参数用于开启高效文件传输模式。将tcp_nopush和tcp_nodelay两个指令设置为on用于防止网络阻塞;
sendfile on;
# 8080端口 gzip 反向代理
server {
listen 8080;
server_name vue;
# 必须要加 错误页配置
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
#开启gzip
gzip on;
##低于1kb的资源不压缩
gzip_min_length 1k;
##压缩级别1-9,越大压缩率越高,同时消耗cpu资源也越多,建议设置在5左右。
gzip_comp_level 7;
##需要压缩哪些响应类型的资源,多个空格隔开。不建议压缩图片.
gzip_types text/plain application/javascript application/x-javascript text/javascript text/xml text/css;
##配置禁用gzip条件,支持正则。此处表示ie6及以下不启用gzip(因为ie低版本不支持)
gzip_disable "MSIE [1-6]\.";
##是否添加“Vary: Accept-Encoding”响应头
gzip_vary on;
charset utf8;
large_client_header_buffers 4 64k;
client_header_buffer_size 64k;
client_max_body_size 60m;
client_body_buffer_size 128k;
proxy_connect_timeout 600;
proxy_read_timeout 600;
proxy_send_timeout 600;
proxy_buffer_size 64k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
# vue 静态资源 反向代理
location / {
access_by_lua '
local dynupszone = ngx.shared.dyn_ups_zone;
local access = dynupszone:get("accessfoo");
ngx.log(ngx.INFO, "access status: ", access);
if access == "off" then
ngx.status = 403;
ngx.say("403 : no auth to visit big-screen");
ngx.header["X-Nginx-Header"] = "No Nginx Auth ";
ngx.exit(403)
else
ngx.header["X-Nginx-Header"] = "Have Nginx Auth";
end
';
root /dist/;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
# 动态反向代理开关控制入口
location /config {
default_type text/plain;
content_by_lua_block {
local foo = ngx.req.get_uri_args()["foo"]
if foo == nil then
ngx.say("usage: /config?foo=off, or /config?foo=on")
local dynupszone = ngx.shared.dyn_ups_zone;
local access = dynupszone:get("accessfoo");
ngx.say("now status: ", access)
return;
end
ngx.log(ngx.INFO, "vue can visit: ", foo);
ngx.say("vue can visit: ", foo);
ngx.shared.dyn_ups_zone:set("accessfoo", foo);
}
}
}
}
2.动态流量环境切换
worker_processes 2; #配置nginx工作进程数
events {
use epoll;
worker_connections 1024;
}
http {
# lua 全局缓存
lua_shared_dict dyn_ups_zone 10m;
# include是个主模块指令,实现对配置文件所包含的文件的设定,可以减少主配置文件的复杂度。类似于Apache中的include方法。
include mime.types;
default_type application/octet-stream;
#access_log logs/access.log main;
sendfile on;
gzip on;
tcp_nopush on;
tcp_nodelay on;
upstream prod-env {
server 192.168.0.5:8080;
keepalive 50;
}
upstream grayscale-env {
server 192.168.0.6: 8080;
keepalive 50;
}
upstream default {
server 192.168.0.7: 8080;
keepalive 50;
}
server {
listen 80;
server_name 192.168.0.1;
charset utf-8;
# 默认流量环境
set $group "default";
location /api {
access_by_lua '
local dynupszone = ngx.shared.dyn_ups_zone;
local access = dynupszone:get("accessfoo");
ngx.log(ngx.INFO, "accessfoo status: ", access);
if access and access == "on" then
local headers = ngx.req.get_headers()
local staffid = headers["Staff-Id"]
ngx.log(ngx.INFO, "staffid value: ", staffid);
if staffid and staffid:sub(-1) == "5" then
ngx.var.group = "grayscale-env";
else
ngx.var.group = “prod-env";
end
else
ngx.var.group = "default";
end
';
rewrite ^/api/(.*)$ /api/$1 break;
proxy_pass http://$group;
#keepalive_timeout 0;
}
# 动态灰度环境开关控制入口
location /GrayscaleEnvironment {
default_type text/plain;
content_by_lua_block {
local foo = ngx.req.get_uri_args()["config"]
if foo == nil then
ngx.say("usage: /GrayscaleEnvironment?config=off, or /GrayscaleEnvironment?config=on")
local dynupszone = ngx.shared.dyn_ups_zone;
local access = dynupszone:get("accessfoo");
ngx.say("Grayscale Environment Default Off: ", access);
return;
end
ngx.log(ngx.INFO, "Grayscale Environment ON: ", foo);
ngx.say("Grayscale Environment ON: ", foo);
ngx.shared.dyn_ups_zone:set("accessfoo", foo);
}
}
}
}