※こちらは旧サイトです(新サイトはこちら

Nginx + Docker + Apache 2.4 + mod_remoteipでアクセス元IPを取得

2015-10-13 16:46:04

Nginxをリバースプロキシに、DockerのApache(2.4)コンテナでmod_remoteipを使ってアクセス元IPを取得した時のメモ。

ホスト側( Nginx)の設定

$ sudo vim /etc/nginx/nginx.conf
// httpディレクティブに追記
proxy_redirect    off;
proxy_set_header  Host               $host;
proxy_set_header  X-Real-IP          $remote_addr;
proxy_set_header  X-Forwarded-Host   $host;
proxy_set_header  X-Forwarded-Server $host;
proxy_set_header  X-Forwarded-For    $proxy_add_x_forwarded_for;
$ sudo vim /etc/nginx/sites-available/mysite
server {
    listen 80;
    server_name [コンテナに流すドメイン];

    // serverディレクティブに追記
    location / {
        proxy_pass http://[コンテナのIP]:[コンテナのhttpポート];
        break;
    }
}
Nginx再起動
$ sudo systemctl restart nginx

コンテナ側(Apache)の設定

$ sudo vim /etc/apache2/sites-available/mysite
// VirtualHostディレクティブに追記
<IfModule remoteip_module>
    RemoteIPHeader X-Forwarded-For
    RemoteIPInternalProxy 127.0.0.1
    RemoteIPInternalProxy [ホスト側のIP]
</IfModule>
$ sudo vim /etc/apache2/apache2.conf
// LogFormatに「%a」追加
LogFormat "%h %a %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %a %l %u %t \"%r\" %>s %O" common
mod_remoteipを有効
$ sudo a2enmod remoteip
再起動
$ sudo systemctl restart apache2