<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>服务器安全维护工作室 &#187; 使用LVS或者阿里云的SLB后如何获取访客真实的IP地址</title>
	<atom:link href="https://www.fuwuqiok.com/tag/%e4%bd%bf%e7%94%a8lvs%e6%88%96%e8%80%85%e9%98%bf%e9%87%8c%e4%ba%91%e7%9a%84slb%e5%90%8e%e5%a6%82%e4%bd%95%e8%8e%b7%e5%8f%96%e8%ae%bf%e5%ae%a2%e7%9c%9f%e5%ae%9e%e7%9a%84ip%e5%9c%b0%e5%9d%80/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.fuwuqiok.com</link>
	<description></description>
	<lastBuildDate>Sun, 01 Mar 2020 07:28:40 +0000</lastBuildDate>
	<language>zh-CN</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.2.26</generator>
	<item>
		<title>使用LVS或者阿里云的SLB后如何获取访客真实的IP地址</title>
		<link>https://www.fuwuqiok.com/%e4%bd%bf%e7%94%a8lvs%e6%88%96%e8%80%85%e9%98%bf%e9%87%8c%e4%ba%91%e7%9a%84slb%e5%90%8e%e5%a6%82%e4%bd%95%e8%8e%b7%e5%8f%96%e8%ae%bf%e5%ae%a2%e7%9c%9f%e5%ae%9e%e7%9a%84ip%e5%9c%b0%e5%9d%80/</link>
		<comments>https://www.fuwuqiok.com/%e4%bd%bf%e7%94%a8lvs%e6%88%96%e8%80%85%e9%98%bf%e9%87%8c%e4%ba%91%e7%9a%84slb%e5%90%8e%e5%a6%82%e4%bd%95%e8%8e%b7%e5%8f%96%e8%ae%bf%e5%ae%a2%e7%9c%9f%e5%ae%9e%e7%9a%84ip%e5%9c%b0%e5%9d%80/#comments</comments>
		<pubDate>Fri, 10 Mar 2017 14:21:11 +0000</pubDate>
		<dc:creator><![CDATA[admin]]></dc:creator>
				<category><![CDATA[aliyun服务器代维]]></category>
		<category><![CDATA[linux服务器代维]]></category>
		<category><![CDATA[使用LVS或者阿里云的SLB后如何获取访客真实的IP地址]]></category>

		<guid isPermaLink="false">https://www.fuwuqiok.com/?p=3128</guid>
		<description><![CDATA[<p>阿里云服务器使用SLB后如何获取真实IP（使用LVS等负载均衡方案之后如何获取真实的IP） 使用7层负载均衡之 [&#8230;]</p>
<p><a rel="nofollow" href="https://www.fuwuqiok.com/%e4%bd%bf%e7%94%a8lvs%e6%88%96%e8%80%85%e9%98%bf%e9%87%8c%e4%ba%91%e7%9a%84slb%e5%90%8e%e5%a6%82%e4%bd%95%e8%8e%b7%e5%8f%96%e8%ae%bf%e5%ae%a2%e7%9c%9f%e5%ae%9e%e7%9a%84ip%e5%9c%b0%e5%9d%80/">使用LVS或者阿里云的SLB后如何获取访客真实的IP地址</a>，首发于<a rel="nofollow" href="https://www.fuwuqiok.com">服务器安全维护工作室</a>。</p>
]]></description>
				<content:encoded><![CDATA[<div>
<div><strong>阿里云服务器使用SLB后如何获取真实IP（使用LVS等负载均衡方案之后如何获取真实的IP）</strong></div>
<div>使用7层负载均衡之后最终realserver的访问都是由proxy发起的，所以日志中记录的访问IP都是proxy的IP，这时候如果想获取真实IP，<a href="http://help.aliyun.com/manual?spm=0.0.0.0.H4nGuM&amp;helpId=1357%EF%BC%8C" target="_blank">阿里云官方</a>提供了使用 http_realip_module或者mod_rpaf来获取真实IP<strong>（这种办法除了可以获取IP记录到日志中，还可以禁止特定IP访问站点），这种方法有个弊病就是需要配置文件里面把proxy的请求IP全写进去，而且如果SLB的服务器集群扩容，而你不知道， 那么很可惜扩容的服务器发起的请求，在您的日志中记录的都是他的服务器IP而不是访客的ip~~</strong></div>
<div></div>
</div>
<div></div>
<div>其实不用那么复杂，SLB服务器会把客户的真实IP记录在http头X_FORWARDED_FOR 字段中。</div>
<div>实际apache和nginx都是可以直接读取http头中任何字段值的，所以可以在logformat中配置X_FORWARDED_FOR 这个字段，测试情况如下：</div>
<div></div>
<div><strong>Apache：</strong></div>
<div>日志格式配置</div>
<div><a href="https://www.fuwuqiok.com/wp-content/uploads/2017/03/ee.jpg"><img class="attachment-medium" src="https://www.fuwuqiok.com/wp-content/uploads/2017/03/ee.jpg" alt="ee" width="967" height="28" /></a></div>
<div><img src="http://www.cnhejia.cn/wp-content/plugins/jquery-image-lazy-loading/images/grey.gif" alt="" /></div>
<div id="highlighter_29251" class="syntaxhighlighter  ">
<div class="lines">
<div class="line alt1">
<table>
<tbody>
<tr>
<td class="number"><code>1</code></td>
<td class="content"><code class="plain">LogFormat </code><code class="string">"'realip:'%{X-Forwarded-For}i 'slbip:'%h %l %u %t \"%r\" %&gt;s %b \"%{Referer}i\" \"%{User-Agent}i\""</code> <code class="plain">combined</code></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<div></div>
<div>重启apache后测试可以获取到真实IP，情况如下：</div>
<div><a href="https://www.fuwuqiok.com/wp-content/uploads/2017/03/2.jpg"><img class="attachment-medium" src="https://www.fuwuqiok.com/wp-content/uploads/2017/03/2.jpg" alt="2" width="842" height="302" /></a></div>
<div><strong>其中有realip还是-的记录，记录是SLB服务器健康检查的记录并不是客户访问的记录，可以忽略。</strong></div>
<div></div>
<div><img src="http://www.cnhejia.cn/wp-content/plugins/jquery-image-lazy-loading/images/grey.gif" alt="" /></div>
<div></div>
<div></div>
<div><strong>Nginx</strong></div>
<div>日志配置情况如下：</div>
<div><a href="https://www.fuwuqiok.com/wp-content/uploads/2017/03/ff.jpg"><img class="attachment-medium" src="https://www.fuwuqiok.com/wp-content/uploads/2017/03/ff.jpg" alt="ff" width="897" height="58" /></a></div>
<div><img src="http://www.cnhejia.cn/wp-content/plugins/jquery-image-lazy-loading/images/grey.gif" alt="" /></div>
<div>
<div id="highlighter_371661" class="syntaxhighlighter  ">
<div class="lines">
<div class="line alt1">
<table>
<tbody>
<tr>
<td class="number"><code>1</code></td>
<td class="content"><code class="plain">log_format  main  </code><code class="string">'realip:$http_x_forwarded_for slbip:$remote_addr - $remote_user [$time_local] "$request" '</code></td>
</tr>
</tbody>
</table>
</div>
<div class="line alt2">
<table>
<tbody>
<tr>
<td class="number"><code>2</code></td>
<td class="content"><code class="string">'$status $body_bytes_sent "$http_referer" '</code></td>
</tr>
</tbody>
</table>
</div>
<div class="line alt1">
<table>
<tbody>
<tr>
<td class="number"><code>3</code></td>
<td class="content"><code class="string">'"$http_user_agent"'</code><code class="plain">;</code></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<div></div>
<div>测试后可以获取真实的IP，情况如下：</div>
<div><a href="https://www.fuwuqiok.com/wp-content/uploads/2017/03/gg.jpg"><img class="attachment-medium" src="https://www.fuwuqiok.com/wp-content/uploads/2017/03/gg.jpg" alt="gg" width="611" height="345" /></a></div>
<div><img src="http://www.cnhejia.cn/wp-content/plugins/jquery-image-lazy-loading/images/grey.gif" alt="" /></div>
<div></div>
<div><strong>同上，没获取到realip的都是健康检查的记录，不是客户访问的记录，可以忽略。</strong></div>
<div></div>
<div>这个方法非阿里云官方提供的，我在自己的几台服务器测试都完全没问题，总之，如果这种方法你测试可以满足你的需求，那么你可以尝试一下~~</div>
</div>
<p><a rel="nofollow" href="https://www.fuwuqiok.com/%e4%bd%bf%e7%94%a8lvs%e6%88%96%e8%80%85%e9%98%bf%e9%87%8c%e4%ba%91%e7%9a%84slb%e5%90%8e%e5%a6%82%e4%bd%95%e8%8e%b7%e5%8f%96%e8%ae%bf%e5%ae%a2%e7%9c%9f%e5%ae%9e%e7%9a%84ip%e5%9c%b0%e5%9d%80/">使用LVS或者阿里云的SLB后如何获取访客真实的IP地址</a>，首发于<a rel="nofollow" href="https://www.fuwuqiok.com">服务器安全维护工作室</a>。</p>
]]></content:encoded>
			<wfw:commentRss>https://www.fuwuqiok.com/%e4%bd%bf%e7%94%a8lvs%e6%88%96%e8%80%85%e9%98%bf%e9%87%8c%e4%ba%91%e7%9a%84slb%e5%90%8e%e5%a6%82%e4%bd%95%e8%8e%b7%e5%8f%96%e8%ae%bf%e5%ae%a2%e7%9c%9f%e5%ae%9e%e7%9a%84ip%e5%9c%b0%e5%9d%80/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
