Can anyone improve this code as an alternative for the evil IF ?

        location ~ \.php$ {
            try_files $uri $uri/ =404;
            set $mmode 0;
            set_by_lua $notused '
              s = 0;
              local source_fname = ngx.var.document_root .. "/maintenance_mode.html";
              local file = io.open(source_fname);
              if file then ngx.var.mmode=1; file:close(); end;
              s = string.find(ngx.var.remote_addr, "^10.10.");
              if s then ngx.var.mmode=0; end;
            ';
            if ($mmode) { return 503; }
            index  index.html index.htm index.php;
[...]
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
[...]
        }

It works very well but having doubts about the string.find value, I think I need to escape the dots...
    Hello!
    
    On Mon, Jul 14, 2014 at 7:50 AM,  info wrote:
    > Can anyone improve this code as an alternative for the evil IF ?
    [...]
    >             if ($mmode) { return 503; }
    
    This "if" thing is equivalent to the following
    
        rewrite_by_lua '
            local v = ngx.var.mmode
            if v and v ~= "" and v ~= "0" then
                return ngx.exit(503)
            end
        ';
    
    And for your use case you can
    
    1. merge the Lua code in set_by_lua into this rewrite_by_lua, and
    2. eliminate the $mmode nginx variable altogether by your own Lua
    variable (or even nothing).
    
    Regards,
    -agentzh
    
      Cool, here's the final tested version then:

              location / {
                  try_files $uri $uri/ =404;
                  index  index.html index.htm;
              }
              location ~ \.php$ {
                  try_files $uri $uri/ =404;
                  rewrite_by_lua '
                    local s = 0; local v = 0;
                    local source_fname = ngx.var.document_root .. "/maintenance_mode.html";
                    local file = io.open(source_fname);
                    if file then v=1; file:close(); end;
                    if string.find(ngx.var.remote_addr, "^10.10.30.") then v=0; end;
                    if v>0 then return ngx.exit(503); end;
                  ';
                  index  index.html index.htm index.php;
                  fastcgi_ignore_client_abort on;
                  fastcgi_pass   myLoadBalancer;
                  fastcgi_index  index.php;
                  fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
                  include        fastcgi_params;
              }


      Op maandag 14 juli 2014 21:10:07 UTC+2 schreef agentzh:
      Hello!

      On Mon, Jul 14, 2014 at 7:50 AM,  info wrote:
      > Can anyone improve this code as an alternative for the evil IF ?
      [...]
      >             if ($mmode) { return 503; }

      This "if" thing is equivalent to the following

          rewrite_by_lua '
              local v = ngx.var.mmode
              if v and v ~= "" and v ~= "0" then
                  return ngx.exit(503)
              end
          ';

      And for your use case you can

      1. merge the Lua code in set_by_lua into this rewrite_by_lua, and
      2. eliminate the $mmode nginx variable altogether by your own Lua
      variable (or even nothing).

      Regards,
      -agentzh
        Write a Reply...