2014/11/13
在reverse proxy後取得原始客戶端ip
首先文章以以下代稱
Nginx代表的是Reverse proxy server
Apache代表的是Backend server
透過Nginx反向代理Apache
通常在Apache拿到的Client IP會是這個Nginx的IP
為了將Client IP(Remote IP)傳送到Apache
我們必須在Nginx與Apache做點設定
Nginx端的設定:
proxy.conf
```
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
```
此設定是為了將Client IP放到往Apache送的Request header中
但是單單做這樣的這定Apache的log還是不會紀錄Nginx的IP
以下重要的來了
Apache端必須安裝mod_rpaf這個套件
```
sudo apt-get install libapache2-mod-rpaf
```
接著編輯rpaf.conf
```
<ifmodule mod_rpaf-2.0.c>
RPAFenable On
RPAFsethostname On
RPAFproxy_ips 127.0.0.1 ::1 #這邊加上Nginx的IP
RPAFheader X-Forwarded-For
</IfModule>
```
將Nginx與Apache重啟後
Apache的log就會記錄真正的Client IP了
2012/08/24
[apache2] 在NAT底下使不同網站共用80port
先說說我這邊的網路環境
VDSL小烏龜接ASUS-AP在分給兩台主機

Server A的domain為 www.mydomain.tw
其底下有架設很多個virtualhost(play.mydomain.tw、cool.mydomain.tw)
Server B是我新的一台主機 預計給他的domain是ubuntu.mydomain.tw
從外部連到http://mydomain.tw會跑去Server A
現在Server B也要架一個Web(http://ubuntu.mydomain.tw)
由於Server A已經佔用了外部WAN IP(12.34.56.78)的80 port
(AP的NAT是設12.34.56.78:80給172.16.16.2:80)
所以如果直接在DNS上設定ubuntu.mydomain.tw的A紀錄或是CNAME紀錄到最後都會跑到Server A去
但又不想要設12.34.56.78:81給172.16.16.3:80
因為這樣瀏覽web時要加上port號(http://ubuntu.mydomain.tw:81)
這樣實在很醜
有沒有辦法可以做到我的需求:
瀏覽http://www.mydomain.tw會吐出Server A的這個virtualhost東西
瀏覽http://ubuntu.mydomain.tw會吐出Server B的這個virtualhost東西
那就是利用proxypass了
第一步驟先設定好你DNS的紀錄不管是A或CNAME都可以
是A的話把 ubuntu.mydomain.tw 設 12.34.56.78
是CNAME的話 ubuntu.mydomain.tw 設 www.mydomain.tw
(這部分不詳述)
再來就是重點了
在Server A的apache的設定檔要多設
```
<virtualhost *:80>
ServerName ubuntu.mydomain.tw
ProxyRequest Off # 這邊要為Off才安全
ProxyPreserve On
ProxyPass / http://ubuntu.mydomain.tw
ProxyPassReverse / http://ubuntu.mydomain.tw
</VirtualHost>
```
再來,由於Server A這台電腦也不知道ubuntu.mydomain.tw要如何解
所以必須要指定
Windows在C:\windows\system32\drivers\etc\hosts加上
```
172.16.16.3 ubuntu.mydomain.tw
```
Linux在/etc/hosts加上
```
172.16.16.3 ubuntu.mydomain.tw
```
Server B上的apache要把ServerName設為ubuntu.mydomain.tw喔
全部改完後不要忘記重起服務阿
一切正常後外部連連看http://ubuntu.mydomain.tw
應該就可以正常顯示Server B的網站囉
訂閱:
文章 (Atom)