    #
    set $target_upstream "unknow-http-host";
    #
    if ($http_host) {
      set $target_upstream $http_host;
    }
    #
    if ($http_host = "www.miwifi.com"){
      #
      rewrite ^(.*)$ http://miwifi.com? break;
      #
    }
    # xiaoqiang doesn't act as htp server
    if ($http_host = "htp.miwifi.com"){
        # close connection directly, without response
        return 444;
    }
    # block the pop-up portal window
    if ($request_uri = "/generate_204"){
        return 204;
    }
    if ($request_uri = "/connecttest.txt"){
        return 200;
    }
    #if ($request_uri = "/hotspot-detect.html"){
    if ($http_host = "captive.apple.com"){
        return 200;
    }
    #
    #
    set $isluci "0";
    #
    set $canproxy "0";
    #
    set $finalvar "";
    set $is_intercept 0;  # 1 indicate true
    #
    # 0.0.0.1:65535 no avaible
    #
    if ($server_port != "8098"){
      #
      #it is NOT REDIRECT from WEBINITRDR, enable proxy
      #
      set $canproxy "1";
      #
    }
    if ( -f "/tmp/state/dns_intercept"){
           set $is_intercept 1;
    }
    if ( !-f "/www/xiaoqiang/diagnosis/index.html") {
           set $is_intercept 0;
    }
    if ($server_port = "8098"){
      #
      set $isluci "0";
      #
    }
    if ($http_host = "miwifi.com"){
      set $isluci "1";
      set $is_intercept 0;
    }
    if ($http_host = "xiaoqiang") {
                        set $isluci "1";
                }
    if ($http_host = "localhost"){
      #
      set $isluci "1";
      #
      set $canproxy "0";
      set $is_intercept 0;
    }
    if ($host = $server_addr){
      #
      set $isluci "1";
      set $is_intercept 0;
      #
    }
    if ($remote_addr = $server_addr){
      #
      set $isluci "1";
      #
      set $canproxy "0";
      #
    }
    if ($remote_addr = "127.0.0.1"){
      #
      set $isluci "1";
      set $canproxy "0";
    }
    if ($remote_addr = "169.254.31.1"){
      #
      set $isluci "1";
      set $canproxy "0";
      set $is_intercept 0;
    }
    if ($remote_addr = "169.254.31.2"){
      #
      set $isluci "1";
      set $canproxy "0";
      set $is_intercept 0;
    }
    #
    set $finalvar "$canproxy $isluci";
    # '0 0' is no access to webadmin, but come from webinitrdr, redirect to web init
    if ($finalvar = "0 0") {
      rewrite ^(.*)$ http://miwifi.com/webinitrdr.html?webinitrdr=yes? break;
      #rewrite ^(.*)$ /webinitrdr.html?webinitrdr=yes? break;
    }
    # when dnsmasq intercept is working, goto network troubleshoot page
    if ($is_intercept) {
            rewrite ^(.*)$ http://miwifi.com/diagnosis/index.html? break;
    }
    # or client requesting us because it cached DNS result, proxy to upstream by http_host
    # to prevent miss cache
    if ($finalvar = "1 0") {
            rewrite ^(.*)$ /miwifi-goto-autoproxy.html? break;
    }
    #
    add_header MiCGI-Switch "$finalvar";
    add_header MiCGI-TproxyInfo "$dst_addr:$dst_port";
    add_header MiCGI-Upstream "$target_upstream";
    add_header  MiCGI-Client-Ip $remote_addr;
    add_header  MiCGI-Host $host;
    add_header  MiCGI-Http-Host $http_host;
    add_header  MiCGI-Server-Ip $server_addr;
    add_header  MiCGI-Server-Port $server_port;
    add_header  MiCGI-Status LUAROOT;
    add_header  MiCGI-Preload no;
    #
    location /miwifi-goto-autoproxy.html {
      #
      internal;
      keepalive_timeout  0;
      #
      resolver 127.0.0.1;
      #
      add_header MiCGI-Switch "$finalvar";
      add_header MiCGI-TproxyInfo "$dst_addr:$dst_port";
      add_header MiCGI-Upstream "$target_upstream";
      add_header MiCGI-Client-Ip $remote_addr;
      add_header MiCGI-Host $host;
      add_header MiCGI-Http-Host $http_host;
      add_header MiCGI-Server-Ip $server_addr;
      add_header MiCGI-Server-Port $server_port;
      add_header MiCGI-Status AUTOPROXY;
      add_header MiCGI-Preload no;
      #
      proxy_redirect off;
      #
      proxy_set_header Host $target_upstream;
      #
      proxy_pass http://$target_upstream/$request_uri;
      #
    }
    location /webinitrdr.html {
      #
      expires epoch;
      keepalive_timeout  0;
      #
      add_header MiCGI-Switch "$finalvar";
      add_header MiCGI-TproxyInfo "$dst_addr:$dst_port";
      add_header MiCGI-Upstream "$target_upstream";
      add_header  MiCGI-Client-Ip $remote_addr;
      add_header  MiCGI-Host $host;
      add_header  MiCGI-Http-Host $http_host;
      add_header  MiCGI-Server-Ip $server_addr;
      add_header  MiCGI-Server-Port $server_port;
      add_header  MiCGI-Preload no;
      #
      add_header  MiCGI-Status WEBINITRDR;
      add_header Cache-Control  'private,max-age=0';
      add_header Content-Type "text/html;charset=utf-8";
      #
      #return 503;
      #
    }
    location /diagnosis {
       expires -1;
       add_header  MiCGI-Host $host;
       add_header  MiCGI-Http-Host $http_host;
       add_header Content-Type "text/html;charset=utf-8";
       root /www/xiaoqiang/;
       index  index.html;
    }
    #
