Press "Enter" to skip to content

Apache Require IP Remote IP or X-Forwarded-For

当 Apache 配置了 Require 某个 IP 地址的时候,默认情况下 Apache 是直接获取的和自己进行通信的客户端 IP 地址,如果此时 Apahce 位于阿里云的负载均衡器后面,那么 Apahce 就把负载均衡器的 IP 地址识别为请求者的 IP 地址
此时,应该对 Apache 进行一下配置:

首先,为 Apache 启用 remoteip 模块

root@server:/etc/apache2/mods-available# a2enmod remoteip

然后配置 VHost 或者 Server,增加 RemoteIPHeader 的配置项:
<VirtualHost *:80>
    ServerName xxx.com
    DocumentRoot /var/lib/www/xxx
    RewriteEngine on
    RewriteRule ^/rsrc/(.*)     -                       [L,QSA]
    RewriteRule ^/favicon.ico   -                       [L,QSA]
    RewriteRule ^(.*)$          /index.php?__path__=$1  [B,L,QSA]
    
    # X-Forwarded-For 就是告诉 Apache 获取 X-Forwarded-For 的 IP
    RemoteIPHeader X-Forwarded-For

    <Directory "/home/miroot/www/phabricator/phabricator/webroot">
        # Require all granted
        Require ip 222.222.222.222
    </Directory>
</VirtualHost>

然后重启 Apache 服务,就可以了。

5 Comments

  1. 赚啦
    赚啦 2017年6月28日

    反正闲着也是闲着,就来看看您的博客!

  2. Timmykem
    Timmykem 2017年9月22日

    Hi HI

  3. 套图
    套图 2017年10月20日

    阅读博客获得的进步不亚于阅读一本书。

  4. 套图
    套图 2017年10月23日

    不止一次的来,不止一次的去,来来去去,这就是这个博客的魅力!

  5. 健康
    健康 2017年10月26日

    学海无涯,博客有道!拜读咯!

发表评论

您的电子邮箱地址不会被公开。