後端Apache獲取前端Nginx反向代理的真實IP地址

第一:Nginx配置

           ①:編輯nginx虛擬主機配置文件。(因爲我引用了虛擬主機單獨配置文件,所以修改的不是nginx.conf)

vim /usr/local/nginx/conf/conf.d/vhos_blog.conf

           ②:在虛擬主機標籤內添加如下語句:

proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;

第二:Apache配置

           說明:Apache獲取真實IP地址有2個模塊:mod_rpaf:Apache-2.2支持;Apache-2.4不支持;mod_remoteip:Apache-2.4自帶模塊;Apache-2.2 支持,本次 實驗使用mod_remoteip

           ①:動態加載mod_remoteip模塊

            編輯httpd.conf文件

vim /usr/local/apache/conf/httpd.conf

            找到如下代碼,去掉#號註釋使其生效

#LoadModule remoteip_module modules/mod_remoteip.so

           ②:在httpd.conf中加入如下代碼,用Nginx的真實地址替換代碼中的127.0.0.1

RemoteIPHeader X-Forwarded-For
RemoteIPInternalProxy 127.0.0.1

           ③:在httpd.conf中找到

LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined

                將日誌格式中的h%改成a%

LogFormat "%a %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章