nginx结合lua做转发,负载均衡
直接nginx编写
#lua拦截转发location / {# 使用 Lua 脚本处理转发逻辑content_by_lua_block {-- 获取请求参数(如 ?backend=1)local dade = "大得2"-- 根据参数动态转发if dade == "大得1" then -- 内部跳转到后端1ngx.exec("@backend1")endif dade == "大得2" then -- 内部跳转到后端1ngx.exec("@backend2")end-- 默认响应ngx.say("Hello, Lua + Nginx!")}}# 直接指定后端服务器地址,不依赖 upstreamlocation @backend1 {proxy_pass http://192.168.192.128:8000; # 替换为你的实际后端1地址proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;}location @backend2 {proxy_pass http://192.168.192.128:8001; # 替换为你的实际后端2地址proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;}
把lua文件抽离
app.lua
-- 设置响应类型为JSON
-- ngx.header['Content-Type'] = 'application/json; charset=utf-8'
-- ngx.say('{"code":2000,"data":[],"message":"请求成功,你好我是lua"}')
-- 获取请求参数(如 ?backend=1)
local dade = "大得2"
-- 根据参数动态转发
if dade == "大得1" then -- 内部跳转到后端1ngx.exec("@backend1")
end
if dade == "大得2" then -- 内部跳转到后端1ngx.exec("@backend2")
end
-- 默认响应
ngx.say("Hello, Lua + Nginx!")
nginx文件
#lua拦截转发location / {# 使用 Lua 脚本处理转发逻辑content_by_lua_file "/www/app.lua";}# 直接指定后端服务器地址,不依赖 upstreamlocation @backend1 {proxy_pass http://192.168.192.128:8000; # 替换为你的实际后端1地址proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;}location @backend2 {proxy_pass http://192.168.192.128:8001; # 替换为你的实际后端2地址proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;}
轮训,负载均衡
nginx
# 定义共享内存区域,用于存储轮询状态
lua_shared_dict round_robin 1m;
server
{listen 80;server_name 192.168.192.128;index index.php index.html index.htm default.php default.htm default.html;root /www/wwwroot/192.168.192.128;#CERT-APPLY-CHECK--STARTinclude /www/server/panel/vhost/nginx/well-known/192.168.192.128.conf;#CERT-APPLY-CHECK--END#SSL-START SSL相关配置#error_page 404/404.html;#SSL-END#ERROR-PAGE-STARTerror_page 404 /404.html;#error_page 502 /502.html;#ERROR-PAGE-END#PHP-INFO-STARTinclude enable-php-80.conf;#PHP-INFO-END#REWRITE-STARTinclude /www/server/panel/vhost/rewrite/192.168.192.128.conf;#REWRITE-END#禁止访问的文件或目录location ~ ^/(\.user.ini|\.htaccess|\.git|\.env|\.svn|\.project|LICENSE|README.md){return 404;}#lua拦截转发location / {# 使用 Lua 脚本处理转发逻辑content_by_lua_file "/www/app.lua";}# 直接指定后端服务器地址,不依赖 upstreamlocation @backend1 {proxy_pass http://192.168.192.128:8000; # 替换为你的实际后端1地址proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;}location @backend2 {proxy_pass http://192.168.192.128:8001; # 替换为你的实际后端2地址proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;}#一键申请SSL证书验证目录location ~ \.well-known{allow all;}#禁止在证书验证目录放入敏感文件if ( $uri ~ "^/\.well-known/.*\.(php|jsp|py|js|css|lua|ts|go|zip|tar\.gz|rar|7z|sql|bak)$" ) {return 403;}location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)${expires 30d;error_log /dev/null;access_log /dev/null;}location ~ .*\.(js|css)?${expires 12h;error_log /dev/null;access_log /dev/null;}access_log /www/wwwlogs/192.168.192.128.log;error_log /www/wwwlogs/192.168.192.128.error.log;
}
app.lua
-- 设置响应类型为JSON
-- ngx.header['Content-Type'] = 'application/json; charset=utf-8'
-- ngx.say('{"code":2000,"data":[],"message":"请求成功,你好我是lua"}')
-- 获取请求参数(如 ?backend=1)-- 获取共享内存字典
local dict = ngx.shared.round_robin-- 读取当前计数,默认为0
local count, err = dict:get("request_count")
if not count thencount = 0
end-- 计算下一个计数(取模2实现0和1的交替)
local next_count = (count + 1) % 2
dict:set("request_count", next_count)-- 根据计数决定使用哪个后端
local dade
if count == 0 thendade = "大得1"
elsedade = "大得2"
end-- 根据参数动态转发
if dade == "大得1" then -- 内部跳转到后端1ngx.exec("@backend1")
end
if dade == "大得2" then -- 内部跳转到后端1ngx.exec("@backend2")
end
-- 默认响应
ngx.say("Hello, Lua + Nginx!")