nginx+lua脚本实现url参数动态请求服务--jerry出品

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    keepalive_timeout  65;

    server {
        listen       80;
        server_name  localhost;

        location / {
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

    }

#以上代码都是基础配置

#设置服务

  upstream xxx1{
        server 192.168.1.101:8080;
    }
    upstream xxx2 {
        server 192.168.1.101:8090;
    }
    server{
        listen 8888;
        location / {

             #引入ual脚本地址
             content_by_lua_file "lua/test.lua";
        }

        #请求的反向代理地址
        location @tomcat1 {
             proxy_pass http://xxx1;
        }
        location @tomcat2 {
             proxy_pass http://xxx2;
        }
    } 

}

#配置lua脚本

#引入获取参数的方法,
local method = ngx.var.request_method
local code = nil
 --判断请求方式。
if method == "GET" then
    code = ngx.req.get_uri_args()["id"] or 0
        ngx.log(ngx.INFO, " code:", code)
elseif method == "POST" then
    ngx.req.read_body()
    code = ngx.req.get_post_args()["id"] or 0
end
 #math.fmod("prams", 2 )取余函数,tonumber(code)转换成整数函数
if math.fmod(tonumber(code), 2 ) == 0 then

 ngx.exec('@tomcat1') #执行对应的location
end
if math.fmod(tonumber(code), 2 ) == 1 then
 ngx.exec('@tomcat2')
end

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值