---
title: "LNAMP架构中后端Apache获取用户真实IP地址的2种方法"
author: "yzyzero"
date: "2015-06-29T11:47:54Z"
tags: []
categories: ["开源软件"]
slug: "lnamp-apache-ip-2"
draft: false
lastmod: "2015-06-29T11:47:54Z"
id: 2222790
url: "https://yzyzero.iteye.com/blog/2222790"
---

**一、Nginx反向代理配置：**

**1、虚拟主机配置**

.代码如下:

location / {  
    try\_files $uri @apache;  
}

 

location @apache {  
internal;  
    proxy\_pass http://127.0.0.1:8080;  
    include proxy.conf;  
}

location ~ .\*\\.(php|php5)?$  {  
    proxy\_pass http://127.0.0.1:8080;  
    include proxy.conf;  
}

**2.代理配置**

.代码如下:

proxy\_connect\_timeout 300s;#请求连接后端服务器超时时间。即在规定时间，后端必须响应前端握手请求。  
proxy\_send\_timeout 900;#后端数据回传时间。即在规定时间，后端服务器必须将所有数据回传完毕。  
proxy\_read\_timeout 900;#后端服务器响应时间。即连接成功，在后端排队等候处理不能超过的时间。  
proxy\_buffer\_size 32k;#从后端服务器读取的头信息的缓冲区大小。  
proxy\_buffers 4 64k;#从后端服务器读取的头信息的缓冲区数目和大小  
proxy\_busy\_buffers\_size 128k;#如果服务器繁忙，可申请的proxy\_buffers大小  
proxy\_temp\_file\_write\_size 64k;#代理缓存临时文件大小  
proxy\_max\_temp\_file\_size 128m;#代理最大缓存临时文件大小  
proxy\_redirect off;  
proxy\_hide\_header Vary;  
proxy\_set\_header Accept-Encoding '';  
proxy\_set\_header Referer $http\_referer;  
proxy\_set\_header Cookie $http\_cookie;  
proxy\_set\_header Host $host;  
proxy\_set\_header X-Real-IP $remote\_addr;  
proxy\_set\_header X-Forwarded-For $proxy\_add\_x\_forwarded\_for;

 

**二、Apache获取真实IP模块**  
获取真实IP地址有Apache有2个模块：  
mod\_rpaf：Apache-2.2支持；Apache-2.4不支持。网上教程很多，而且官网链接已失效  
mod\_remoteip：Apache-2.4自带模块；Apache-2.2支持；推荐

**A、使用mod\_rpaf模块**  
**1、安装**  
wget http://stderr.net/apache/rpaf/download/mod\_rpaf-0.6.tar.gz  
tar -xzvf mod\_rpaf-0.6.tar.gz  
cd mod\_rpaf-0.6/  
/usr/local/apache/bin/apxs  -i -c -n mod\_rpaf-2.0.slo mod\_rpaf-2.0.c  
**2、添加Apache配置**

.代码如下:

vi  /usr/local/apache/conf/httpd.conf  
Include conf/extra/httpd-rpaf.conf  
vi /usr/local/apache/conf/extra/httpd-rpaf.conf

 

LoadModule rpaf\_module        modules/mod\_rpaf-2.0.so  
RPAFenable On  
RPAFsethostname On  
RPAFproxy\_ips 127.0.0.1 10.8.0.110 # 代理服务器的ip地址（记得做相应修改）  
RPAFheader X-Forwarded-For

备注：RPAFproxy\_ips后面添加代理服务器的ip地址,有几个填几个  
**3.测试**

.代码如下:

\# /usr/local/apache/bin/apachectl -t  
\# /usr/local/apache/bin/apachectl restart  
\# 看日志是否获取到了真实IP

**B、使用mod\_remoteip模块**  
1、Apache-2.2下配置mod\_remoteip如下：  
**1）、安装**

.代码如下:

wget https://github.com/ttkzw/mod\_remoteip-httpd22/raw/master/mod\_remoteip.c  
/usr/local/apache/bin/apxs -i -c -n mod\_remoteip.so mod\_remoteip.c

**2）、修改配置文件：**

.代码如下:

vi /usr/local/apache/conf/httpd.conf  
Include conf/extra/httpd-remoteip.conf  
vi /usr/local/apache/conf/extra/httpd-remoteip.conf  
LoadModule remoteip\_module modules/mod\_remoteip.so  
RemoteIPHeader X-Forwarded-For  
RemoteIPInternalProxy 127.0.0.1

**3）、测试：**

.代码如下:

\# /usr/local/apache/bin/apachectl -t  
\# /usr/local/apache/bin/apachectl restart  
\# 看日志

**2、Apache-2.4配置mod\_remoteip**  
自带mod\_remoteip模块不需要安装，只需要修改日志格式

.代码如下:

LogFormat "%h %a %l %u %t \\"%r\\" %&gt;s %b \\"%{Referer}i\\" \\"%{User-Agent}i\\"" combined  
LogFormat "%h %a %l %u %t \\"%r\\" %&gt;s %b" common  
LogFormat "%h %l %u %t \\"%r\\" %&gt;s %b \\"%{Referer}i\\" \\"%{User-Agent}i\\" %I %O" combinedi

在日志格式中加上%a