【问题标题】:nginx maintainence page with ip whitelist带有ip白名单的nginx维护页面
【发布时间】:2014-06-20 06:47:16
【问题描述】:

对于我的网站,我希望能够建立一个维护 503 页面,但有一个 IP 地址白名单,可以正常使用该网站

我必须在 lua 中制作这样的东西吗?

我看到一些问题,例如

Nginx Ip Whitelist

How can I setup a custom 503 error page in NGINX?

其中解释了如何单独执行此操作,但我想将它们组合起来,以便我可以将站点离线以供外部世界使用,但仍然能够从某些 IP 地址正常测试它

【问题讨论】:

    标签: nginx lua http-status-code-503


    【解决方案1】:

    您可以使用 ngx_http_geo_module:

    geo $denied {
        default 1; # nobody is allowed access by default
    
        # but people from the following networks/ip addresses are allowed access
        include whitelist;
        127.0.0.1      0;
        192.168.1.0/24 0;
    }
    
    server {
        location / {
            if ($denied) {
                return 503;
            }
        }
    }
    

    【讨论】:

    【解决方案2】:

    不需要lua,只需使用ngx_http_access_module

    location / {
        deny  192.168.1.1;
        allow 192.168.1.0/24;
        allow 10.1.1.0/16;
        allow 2001:0db8::/32;
        deny  all;
    }
    

    适用于位置块以及以下任何一个:http、服务器、位置、limit_except。

    如果您坚持使用 lua,请遵循 instructions to get lua working,然后您可以使用 openresty 自述文件中的示例:

    location / {
        access_by_lua_block {
            -- check the client IP address is in our black list
            if ngx.var.remote_addr == "132.5.72.3" then
                ngx.exit(ngx.HTTP_FORBIDDEN)
            end
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2012-12-04
      • 1970-01-01
      • 2011-08-24
      • 1970-01-01
      • 2014-02-23
      • 2018-07-05
      • 2014-06-02
      • 2017-07-21
      • 2013-05-11
      相关资源
      最近更新 更多