【问题标题】:How to whitelist remote ip addresses in nginx behind a load balancer Laravel如何在负载均衡器 Laravel 后面的 nginx 中将远程 IP 地址列入白名单
【发布时间】:2020-10-20 13:17:03
【问题描述】:

我有一个我想通过负载均衡器后面的 nginx 在我的服务器上允许的 IP 地址列表。我目前正在从 apache 迁移到 nginx,我之前在 apache 中的设置是这样的:

    RewriteBase /
    RewriteCond %{REMOTE_HOST} !^123.456.123.789
    RewriteCond %{REMOTE_HOST} !^456.123.789.123
    RewriteCond %{REMOTE_HOST} !^123.567.456.789
    RewriteCond %{REMOTE_HOST} !^123.456.789.100
    RewriteCond %{REMOTE_HOST} !^127.0.0.1
    RewriteCond %{REQUEST_URI} !/maintenance.php$ [NC]
    RewriteCond %{REQUEST_URI} !\.(jpe?g?|png|gif|css|js|api) [NC]
    RewriteRule .* /maintenance.php [R=302,L]

我想通过重定向除 ip 地址列表之外的所有请求在 nginx 中应用类似的方法。


location / { 


    if ($remote_host !~ "^123.456.123.789"){
        rewrite ^(.*)$ /maintenance.php redirect;
    }
    if ($remote_host !~ "^456.123.789.123"){
        rewrite ^(.*)$ /maintenance.php redirect;
    }
    if ($remote_host !~ "^123.567.456.789"){
        rewrite ^(.*)$ /maintenance.php redirect;
    }
    if ($remote_host !~ "^123.456.789.100"){
        rewrite ^(.*)$ /maintenance.php redirect;
    }
    if ($remote_host !~ "^127.0.0.1"){
        rewrite ^(.*)$ /maintenance.php redirect;
    }


    try_files $uri $uri/ /index.php?$query_string;
    gzip_static on;
}

【问题讨论】:

    标签: laravel nginx amazon-elastic-beanstalk


    【解决方案1】:

    您的意思是,除了您的.htaccess 所做的白名单 IP 和静态资源列表之外的所有请求?试试这个:

    map $uri $checkip {
        /maintenance.php                "-";
        ~\.(jpe?g?|png|gif|css|js|api)  "-";
    }
    
    map $checkip$remote_addr $stop {
        ~^-              "";
        123.456.123.789  "";
        456.123.789.123  "";
        123.567.456.789  "";
        123.456.789.100  "";
        127.0.0.1        "";
        default          1;
    }
    
    server {
        ...
        if ($stop) {
            return 302 /maintenance.php;
        }
        # your locations here
        ...
    

    【讨论】:

    • 感谢您的回复,是的!我想要的是阻止所有请求,但允许列入白名单的 IP 和静态资源。抱歉,我的母语不是英语。
    猜你喜欢
    • 1970-01-01
    • 2019-08-13
    • 1970-01-01
    • 1970-01-01
    • 2013-10-12
    • 1970-01-01
    • 2022-11-01
    • 2021-01-21
    • 2020-04-14
    相关资源
    最近更新 更多