<?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; aws elb负载均衡下，Apache及Nginx上进行访问限制</title>
	<atom:link href="https://www.fuwuqiok.com/tag/aws-elb%e8%b4%9f%e8%bd%bd%e5%9d%87%e8%a1%a1%e4%b8%8b%ef%bc%8capache%e5%8f%8anginx%e4%b8%8a%e8%bf%9b%e8%a1%8c%e8%ae%bf%e9%97%ae%e9%99%90%e5%88%b6/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>aws elb负载均衡下，Apache及Nginx上进行访问限制</title>
		<link>https://www.fuwuqiok.com/aws-elb%e8%b4%9f%e8%bd%bd%e5%9d%87%e8%a1%a1%e4%b8%8b%ef%bc%8capache%e5%8f%8anginx%e4%b8%8a%e8%bf%9b%e8%a1%8c%e8%ae%bf%e9%97%ae%e9%99%90%e5%88%b6/</link>
		<comments>https://www.fuwuqiok.com/aws-elb%e8%b4%9f%e8%bd%bd%e5%9d%87%e8%a1%a1%e4%b8%8b%ef%bc%8capache%e5%8f%8anginx%e4%b8%8a%e8%bf%9b%e8%a1%8c%e8%ae%bf%e9%97%ae%e9%99%90%e5%88%b6/#comments</comments>
		<pubDate>Wed, 06 Dec 2017 03:07:00 +0000</pubDate>
		<dc:creator><![CDATA[admin]]></dc:creator>
				<category><![CDATA[服务器代维]]></category>
		<category><![CDATA[网站代维]]></category>
		<category><![CDATA[网站防挂马]]></category>
		<category><![CDATA[aws elb负载均衡下，Apache及Nginx上进行访问限制]]></category>
		<category><![CDATA[DDOS防御]]></category>
		<category><![CDATA[linux服务器代维]]></category>
		<category><![CDATA[服务器代维安全设置]]></category>
		<category><![CDATA[服务器安全]]></category>
		<category><![CDATA[服务器环境配置]]></category>
		<category><![CDATA[网站木马后门清除]]></category>

		<guid isPermaLink="false">https://www.fuwuqiok.com/?p=3411</guid>
		<description><![CDATA[<p>架构Web系统时在负载均衡下运行多台Web服务器，无论是在物理环境还是在云端都是常见场景。在阿里云上实现同样的 [&#8230;]</p>
<p><a rel="nofollow" href="https://www.fuwuqiok.com/aws-elb%e8%b4%9f%e8%bd%bd%e5%9d%87%e8%a1%a1%e4%b8%8b%ef%bc%8capache%e5%8f%8anginx%e4%b8%8a%e8%bf%9b%e8%a1%8c%e8%ae%bf%e9%97%ae%e9%99%90%e5%88%b6/">aws elb负载均衡下，Apache及Nginx上进行访问限制</a>，首发于<a rel="nofollow" href="https://www.fuwuqiok.com">服务器安全维护工作室</a>。</p>
]]></description>
				<content:encoded><![CDATA[<h2 id="post_title" class="rich_font"></h2>
<div id="left_col">
<div id="article">
<div id="post_image"><a href="https://www.fuwuqiok.com/wp-content/uploads/2017/12/AccessLimitBehindLoadblancer.jpg"><img class="attachment-medium" src="https://www.fuwuqiok.com/wp-content/uploads/2017/12/AccessLimitBehindLoadblancer.jpg" alt="AccessLimitBehindLoadblancer" width="804" height="504" /></a></div>
<div id="single_share_top" class="single_share clearfix"></div>
<div class="post_content clearfix">
<p>架构Web系统时在负载均衡下运行多台Web服务器，无论是在物理环境还是在云端都是常见场景。在阿里云上实现同样的架构时使用SLB + ECS<br />
实现，当SLB在Web系统前端时和在单台ECS上运行Web系统时有些差异。</p>
<p>例如Web服务器识别的访问源IP地址就不同，SLB + ECS架构时Web服务器识别到的访问源IP地址(客户端地址)将会是SLB内网IP地址。而访问源IP地址将会被添加到X-Forwarded-For的末尾。虽然X-Forwarded-For不是RFC规定内容，而在使用代理或者负载均衡服务时的实际标准。</p>
<h3 class="style3a">Apache上的访问限制</h3>
<p>在Apache2.2里使用mod_setenvif模块进行，指定IP地址的访问限制。</p>
<pre>&lt;Location /&gt;
  # 禁止所有客户端的访问
  Order Allow,Deny
  Allow from all
&lt;/Location&gt;
 
&lt;Location /site_b&gt;
  SetEnvIf X-Forwarded-For "192\.168\.0\.0.*" allowed_ip
 
  # 仅允许指定IP访问
  Order Deny,Allow
  Deny from all
  Allow from env=allowed_ip
&lt;/Location&gt;
</pre>
<p>接下来介绍一下在Apache2.4上进行访问限制的方法。Apache2.4里使用mod_remoteip模块，在Amazon Linux的httpd24包默认激活的状态。该模块提供的RemoteIPHeader指令，确认X-Forwarded-For内容并让Apache识别为访问源IP地址(客户端IP)。利用该模块可简单的进行访问限制。</p>
<pre># 把X-Forwarded-For设定为访问源IP(客户端IP)
RemoteIPHeader X-Forwarded-For
 
&lt;Location /&gt;
  # 允许所有客户端的访问
  Require all granted
&lt;/Location&gt;
 
&lt;Location /&gt;
  # 仅允许指定的IP地址访问
  Require ip 192.168.0.0
&lt;/Location&gt;
</pre>
<h3 class="style3a">Nginx上的访问限制</h3>
<p>在Nginx上使用ngx_http_map_module模块 $http_x_forwarded_for 进行访问限制，nginx.conf上的设定如下。</p>
<pre>map $http_x_forwarded_for $allowed {
    default deny;
    ~\s*192.168.0.0$ allow;
    ~\s*8.8.8.*$   allow;
}

location / {
    if ( $allowed = "deny" ) { return 403; }
    alias /path/to/document_root
}
</pre>
<p><a href="http://nginx.org/en/docs/http/ngx_http_map_module.html" target="_blank">ngx_http_map_module</a></p>
<h3 class="style3a">小结</h3>
<p>阿里云的SLB是一个简单易用的负载均衡器，可使用“加权轮询”、“加权最小连接数”、“轮询”分配方式。</p>
<p>在负载均衡下配置Web服务器时，访问日文文件里记录获取访问源IP(客户端IP)时，也使用 X-Forwarded-For 信息。</p>
<p>比如在SLB下运行Nginx时，获取访问源IP地址时也需添加 $http_x_forwarded_for。</p>
<pre>log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                  '$status $body_bytes_sent "$http_referer" '
                  '"$http_user_agent" "$http_x_forwarded_for" $request_time';</pre>
</div>
</div>
</div>
<p><a rel="nofollow" href="https://www.fuwuqiok.com/aws-elb%e8%b4%9f%e8%bd%bd%e5%9d%87%e8%a1%a1%e4%b8%8b%ef%bc%8capache%e5%8f%8anginx%e4%b8%8a%e8%bf%9b%e8%a1%8c%e8%ae%bf%e9%97%ae%e9%99%90%e5%88%b6/">aws elb负载均衡下，Apache及Nginx上进行访问限制</a>，首发于<a rel="nofollow" href="https://www.fuwuqiok.com">服务器安全维护工作室</a>。</p>
]]></content:encoded>
			<wfw:commentRss>https://www.fuwuqiok.com/aws-elb%e8%b4%9f%e8%bd%bd%e5%9d%87%e8%a1%a1%e4%b8%8b%ef%bc%8capache%e5%8f%8anginx%e4%b8%8a%e8%bf%9b%e8%a1%8c%e8%ae%bf%e9%97%ae%e9%99%90%e5%88%b6/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
