• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

lua_nginx_module用例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

content_by_lua

server {
    listen 80;
    server_name lua.luckybing.top;
    location / {
        default_type 'text/plain';
        content_by_lua '
        if ngx.var.remote_addr=="203.100.83.125" then
          ngx.say("hello,from yunxiao")
        else
          ngx.say("hello,from other")
        end';
    }
}

rewrite_by_lua

 server {
    listen 80;    
    server_name lua.luckybing.top;
    location / {
        rewrite_by_lua '
          if ngx.var.remote_addr=="203.100.83.125" then
            return ngx.redirect("http://www.luckybing.top", 302)
          else
            return ngx.redirect("http://chat.luckybing.top", 302)
          end';
    }
}

access_by_lua

server {
    listen 80;
    server_name lua.luckybing.top;
    location @client{
        proxy_pass  http://www.baidu.com;
    }
      location / {
        access_by_lua '
            if ngx.var.remote_addr=="203.100.83.125" then
              ngx.exit(ngx.HTTP_FORBIDDEN)
            else
              ngx.exec("@client")
            end';
    }
}

rewrite_by_lua

server {
        listen 80; 
        server_name lua.luckybing.top;
        location @client{
            proxy_pass http://47.95.222.240:3000;
        }
        location @client2{
            proxy_pass http://47.95.222.240:4000;
        }
        location / {
            default_type 'text/html';
            #ngx.var.remote_addr
            rewrite_by_lua '
                 myIP = ngx.req.get_headers()["X-Real-IP"]
                 if myIP == nil then
                    myIP = ngx.req.get_headers()["x_forwarded_for"]
                 end
                 if myIP == nil then
                    myIP = ngx.var.remote_addr
                 end

                if myIP=="203.100.83.125" then
                  ngx.exec("@client2")
                else
                  ngx.exec("@client")
            end';
        } 
    }

 lua-nginx 负载均衡

upstream wxqy {
      ip_hash;
      server  127.0.0.1:4000;
      server  127.0.0.1:4001;
}



server {
    listen 8091;
    server_name wxqy.iyunxiao.net;
    server_tokens off;
    access_log  off;

    location @client{
        proxy_pass http://wxqy;
    }
    location @client2{
        proxy_pass http://127.0.0.1:4002;
    }


    location / {
        proxy_set_header   X-Real-IP        $remote_addr;
        proxy_set_header   REMOTE-HOST      $remote_addr;
        proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
        #proxy_pass  http://wxqy;
        default_type 'text/plain';
        rewrite_by_lua '
         myIP = ngx.req.get_headers()["X-Real-IP"]
        if myIP == nil then
           myIP = ngx.req.get_headers()["x_forwarded_for"]
         end
        if myIP == nil then
           myIP = ngx.var.remote_addr
        end
        if myIP=="172.31.0.2" then
            ngx.exec("@client2")
        else
           ngx.exec("@client")
        end';

    }
}

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Lua 字符串发布时间:2022-07-22
下一篇:
Cocos-2dx-Lua 编译android安装包问题发布时间:2022-07-22
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap