Nginx + Lua 重定向分流

  • 默认使用的是宝塔面板,免去安装Nginx和编译Lua模块的麻烦。

1. 伪静态

location /
{
      set $redirecthost 'https://mywebsite-default.com';
      set_by_lua $redirecthost '
          host_table = {}
          host_table[1] = "https://myhost1.com"
          host_table[2] = "https://myhost2.com"
          host_table[3] = "https://myhost3.com"
          -- 拼接新的跳转url
          ngx.var.redirecthost = host_table[math.random(1, #host_table)]
          return ngx.var.redirecthost
      ';
     rewrite ^(.*) $redirecthost$1 permanent;
}

Nginx 搭配 Lua 模块还是挺方便的,除此之外还有个模块 nginx-clojure 。
https://github.com/nginx-clojure/nginx-clojure

Nginx-Clojure is a Nginx module for embedding Clojure or Java or Groovy programs, typically those Ring based handlers.