facebook twitter hatena line email

「Linux/nginx/apache経由」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
(apacheに引き渡す際のreverse_proxy設定)
(mod_remoteipの設定)
 
(同じ利用者による、間の5版が非表示)
行20: 行20:
 
     listen      8090;
 
     listen      8090;
 
     server_name  example.localhost;
 
     server_name  example.localhost;
     location ~* \.(jpg|gif|png|swf|css|js|inc|ico|pdf|flv|gz|woff|html|htm|txt|xml)$ {
+
     location ~* \.(jpg|jpeg|gif|png|swf|css|js|inc|ico|pdf|flv|gz|woff|html|htm|txt|xml)$ {
 
       root    /var/www/zend/example/public;
 
       root    /var/www/zend/example/public;
 
       index  index.html;
 
       index  index.html;
行43: 行43:
 
  ・・略・・
 
  ・・略・・
  
==mod_rpafのインストール==
+
==apache側で、nginxからのクライアントipを受ける方法==
apacheのipが127.0.0.1になる問題回避で使用
+
===mod_remoteipをインストール===
$ sudo yum install httpd-devel
+
<pre>
$ cd /usr/local/src
+
# mod_remoteipのインストール確認
$ sudo wget https://raw.github.com/ttkzw/mod_rpaf-0.6/master/mod_rpaf-2.0.c --no-check-certificate
+
$ httpd -M 2>/dev/null | grep remoteip
$ sudo /usr/sbin/apxs -i -c -n mod_rpaf-2.0.so mod_rpaf-2.0.c
+
> remoteip_module (shared) # となればインストールされてる
$ sudo vi /etc/httpd/conf.d/mod_rpaf.conf
+
</pre>
LoadModule rpaf_module modules/mod_rpaf-2.0.so
+
 
RPAFenable On
+
===mod_remoteipの設定===
RPAFsethostname On
+
/etc/httpd/conf/httpd.confの下の方に以下追加
RPAFproxy_ips 127.0.0.1 10. 172.16.
+
<pre>
RPAFheader X-Forwarded-For
+
LoadModule remoteip_module modules/mod_remoteip.so
$ sudo /etc/init.d/httpd restart
+
 
 +
<IfModule remoteip_module>
 +
  RemoteIPHeader X-Forwarded-For
 +
  RemoteIPInternalProxy [ここにnginxのproxyサーバipを入れる]
 +
  LogFormat "%a %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
 +
  LogFormat "%a %l %u %t \"%r\" %>s %b" common
 +
</IfModule>
 +
</pre>
 +
 
 +
%aの部分がnginxのproxyのipから、クライアントipに差し替わる
 +
 
 +
apache再起動
 +
service httpd restart

2025年7月26日 (土) 02:21時点における最新版

apacheに引き渡す際のreverse_proxy設定

apacheでなく別のnginxに引き渡したい時も同様に使える(cookieもひき注がれる)

  • /etc/nginx/conf.d/example.conf
server {
   listen       8090;
   server_name  example.localhost;
   location / {
           proxy_pass http://127.0.0.1:80;
           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;
   }
}

動的処理はapacheに渡し、静的ファイルはapacheに渡さない場合の設定

server {
   listen       8090;
   server_name  example.localhost;
   location ~* \.(jpg|jpeg|gif|png|swf|css|js|inc|ico|pdf|flv|gz|woff|html|htm|txt|xml)$ {
     root    /var/www/zend/example/public;
     index   index.html;
     ssi     on;
     access_log off;
     break;
   }
   location / {
・・略・・

正規表現の最後に$が付いているが.png?v=20150101などの画像でもhitする

apache側port変更設定

  • /etc/httpd/conf/httpd.conf
Listen 80
Listen 8080
NameVirtualHost *:80
NameVirtualHost *:8080
  • /etc/httpd/conf/extra/example.conf
<VirtualHost *:8080>
・・略・・

apache側で、nginxからのクライアントipを受ける方法

mod_remoteipをインストール

# mod_remoteipのインストール確認
$ httpd -M 2>/dev/null | grep remoteip 
> remoteip_module (shared) # となればインストールされてる

mod_remoteipの設定

/etc/httpd/conf/httpd.confの下の方に以下追加

LoadModule remoteip_module modules/mod_remoteip.so

<IfModule remoteip_module>
  RemoteIPHeader X-Forwarded-For
  RemoteIPInternalProxy [ここにnginxのproxyサーバipを入れる]
  LogFormat "%a %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
  LogFormat "%a %l %u %t \"%r\" %>s %b" common
</IfModule>

%aの部分がnginxのproxyのipから、クライアントipに差し替わる

apache再起動

service httpd restart