0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Nginxをリバースプロキシにして、配下にApacheウェブサーバーをおいてみたが

0
Posted at

どうも思ったとおりに動いてくれない。
色々調べて試してみたけどあまり変わらない。
ということで回避方法を備忘録的に残しておきます。

環境

  • VM2台
    • リバースプロキシ
      • CentOS 7
      • nginx-1.16
    • ウェブサーバー
      • CentOS 7
      • Apache 2.4

設定

nginxのプロキシ設定

/etc/nginx/conf.d/proxy.conf
server {
    listen 192.168.xxx.xxx:80;
    listen 192.168.xxx.xxx:443 ssl http2;

    ssl_certificate /etc/nginx/ssl/server.crt;
    ssl_certificate_key /etc/nginx/ssl/server.key;

    server_name site1.example.com;
    server_name site2.example.com;

    client_max_body_size 128m;

    access_log "/var/log/nginx/proxy_access_log";
    error_log "/var/log/nginx/proxy_error_log";

    index index.html index.htm index.php;

    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;
    access_log on;

    location / {
        proxy_pass http://192.168.yyy.yyy:80;
    }
}

これの配下にApacheを置きます。
Apacheの設定は大まかに以下の通りです。

/etc/httpd/conf/httpd.conf
RemoteIPHeader X-Forwarded-For

<IfModule log_config_module>
    LogFormat "%{X-Forwarded-For}i %h %l %u [%{%a %b %d %T %Y}t] \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" mylogformat
</IfModule>

ApacheのアクセスログにクライアントのIPを記録させたいのですが、%hとかを指定していればOKだと思っていました。ところがこれがうまく行かない。%{X-Forwarded-For}iで記録されるようになるのですが、ちょっと違うみたい。

問題点

.htaccessでアクセス制限をかけたいと思っても、REMOTE_ADDRがリバースプロキシサーバーのIPになっているので、そのままRequire ip zzz.zzz.zzz.zzzとはかけない。

この場合、以下のように書くようです。

.htaccess
SetEnvIF X-Forwarded-For "^192\.168\.zzz\.zzz$" AllowIP
Require all denied
Require env AllowIP

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?