Nginx監控請求lua指令碼

來源:互聯網
上載者:User

Nginx監控請求lua指令碼 當應答狀態代碼不是200時,通過udp向伺服器發送報告。monitor.lua[plain] --   -- Config Nginx.conf:  --  -- set $str $uri;  -- content_by_lua_file;  --   -- location /proxy {  --    proxy_pass http://{your-server}$str;  -- }  --    --  -- Extends int to 4 byte  --  function pad32bit(num)      local hex = ""      local rem = num      for i=1,4 do        local bit = rem % 256        rem = math.floor(rem /256)          hex = hex .. string.char(bit)      end      return hex  end    local url = ngx.var.uri  local res = ngx.location.capture("/proxy", {vars = {str = url} })    -- Response  ngx.say(res.body)    -- Send udp message  if res.status ~= 200 then    -- Protocol  -- Length | version | Top-Level no (4) | Second-Level no | json body      --local mydata = require "mydata"    --local udpsock = mydata:socket()      local udpsock = ngx.socket.udp()    udpsock:settimeout(0)    local ok, err = udpsock:setpeername("224.3.29.71", 10000)      local body = "host:" .. ngx.var.host  .. ", url:" .. url .. ", status:" .. res.status    local leng = 4 + 3 + string.len(body)      local msg = pad32bit(leng) .. "\1\4\1" .. body      ngx.say("<!--" .. msg .. "-->")      local ok, err = udpsock:send(msg)    end   Nginx.conf[plain] upstream web{       server 127.0.0.1:8080;       server 127.0.0.1:8081;  }    server {      listen       80;      server_name  localhost;        #charset koi8-r;        location / {          root   html;          index  index.html index.htm;            set $str $uri;          content_by_lua_file /home/hailin/monitor.lua;      }        location /proxy {           resolver 8.8.8.8;             proxy_pass http://web$str;      }   

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.