Nginx:将 IP 范围重定向到文件(如果存在)

Nginx:将 IP 范围重定向到文件(如果存在)

如果存在,我想将用户重定向到maintenance.php文件,但不重定向到我们的内部地址。现在我通过if语句实现了这一点,但这些在location块中被视为邪恶的。

这是现在的配置:

if (-f $document_root/maintenance.php) {
    return 503;
}

error_page 503 @maintenance;

location @maintenance {
    if ($ourips = 0) {
        rewrite ^(.*)$ /maintenance.php last;
    }
    if ($ourips = 1) {
        rewrite ^(.*)$ /app.php/$1 last;
    }
}

location / {
    # try to serve file directly, fallback to rewrite
    try_files $uri @rewriteapp;
}

location @rewriteapp {
    # rewrite all to app.php
    rewrite ^(.*)$ /app.php/$1 last;
}

location ~ ^/(app|clear|maintenance)\.php(/|$) {
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_split_path_info ^(.+\.php)(/.*)$;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param HTTPS off;
}

有没有其他方法可以做到这一点,而无需在位置@maintenance 中使用两个 if 语句?

答案1

让它工作了,使用了一些 if 但据我所知,它们在这种情况下不应该是邪恶的。

    if (-f $document_root/maintenance.php) {
            set $test A;
    }
    if ($ourips = 0) {
            set $test "${test}B";
    }
    if ($test = AB) {
            return 503;
    }

    error_page 503 @maintenance;

    location @maintenance {
        rewrite ^(.*)$ /maintenance.php last;
    }
    location / {
            # try to serve file directly, fallback to rewrite
            try_files $uri @rewriteapp;
    }
    location @rewriteapp {
            # rewrite all to app.php
            rewrite ^(.*)$ /app.php/$1 last;
    }
    location ~ ^/(app|clear|maintenance)\.php(/|$) {
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_split_path_info ^(.+\.php)(/.*)$;
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_param HTTPS off;
    }

nginx.conf

    geo $ourips {
      default 0;
      xxx.xxx.xxx.xxx 1;
    }

相关内容