<?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; Amazon CloudFront常见错误配置及解决方法</title>
	<atom:link href="https://www.fuwuqiok.com/tag/amazon-cloudfront%e5%b8%b8%e8%a7%81%e9%94%99%e8%af%af%e9%85%8d%e7%bd%ae%e5%8f%8a%e8%a7%a3%e5%86%b3%e6%96%b9%e6%b3%95/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>Amazon CloudFront常见错误配置及解决方法</title>
		<link>https://www.fuwuqiok.com/amazon-cloudfront%e5%b8%b8%e8%a7%81%e9%94%99%e8%af%af%e9%85%8d%e7%bd%ae%e5%8f%8a%e8%a7%a3%e5%86%b3%e6%96%b9%e6%b3%95/</link>
		<comments>https://www.fuwuqiok.com/amazon-cloudfront%e5%b8%b8%e8%a7%81%e9%94%99%e8%af%af%e9%85%8d%e7%bd%ae%e5%8f%8a%e8%a7%a3%e5%86%b3%e6%96%b9%e6%b3%95/#comments</comments>
		<pubDate>Fri, 27 Jul 2018 08:56:40 +0000</pubDate>
		<dc:creator><![CDATA[admin]]></dc:creator>
				<category><![CDATA[Amazon AWS]]></category>
		<category><![CDATA[AWS]]></category>
		<category><![CDATA[linux服务器代维护]]></category>
		<category><![CDATA[代维]]></category>
		<category><![CDATA[服务器代维]]></category>
		<category><![CDATA[Amazon CloudFront常见错误配置及解决方法]]></category>

		<guid isPermaLink="false">https://www.fuwuqiok.com/?p=3617</guid>
		<description><![CDATA[<p>很多的用户在最初使用CloudFront做Web类内容分发的时候遇到无法调通的情况，本文总结了用户在配置过程中 [&#8230;]</p>
<p><a rel="nofollow" href="https://www.fuwuqiok.com/amazon-cloudfront%e5%b8%b8%e8%a7%81%e9%94%99%e8%af%af%e9%85%8d%e7%bd%ae%e5%8f%8a%e8%a7%a3%e5%86%b3%e6%96%b9%e6%b3%95/">Amazon CloudFront常见错误配置及解决方法</a>，首发于<a rel="nofollow" href="https://www.fuwuqiok.com">服务器安全维护工作室</a>。</p>
]]></description>
				<content:encoded><![CDATA[<p>很多的用户在最初使用CloudFront做Web类内容分发的时候遇到无法调通的情况，本文总结了用户在配置过程中遇到的常见错误，内容涵盖了大部分用户遇到的情况。</p>
<h3>错误一  源访问权限未放开</h3>
<p>这种错误常见于用S3做源的情况, 引起这种错误的原因是s3的访问控制没有对CloudFront开放。从浏览器中返回的错误通常类似于下图：</p>
<p><a href="https://www.fuwuqiok.com/wp-content/uploads/2018/07/1025-1.png"><img class="attachment-medium" src="https://www.fuwuqiok.com/wp-content/uploads/2018/07/1025-1.png" alt="1025-1" width="817" height="153" /></a></p>
<p>更具体些，可分为以下两个场景：</p>
<p><strong>场景1. CloudFront使用了Restrict Bucket Access</strong></p>
<p>在创建distribution的时候选择了Restrict Bucket Access 为yes, 但 Grant Read Permissions on Bucket, 选择的是”No, I Will Update Permissions”， 而用户事后却没有在s3的桶里更新policy。如下图所示。</p>
<p><a href="https://www.fuwuqiok.com/wp-content/uploads/2018/07/1025-2.png"><img class="attachment-medium" src="https://www.fuwuqiok.com/wp-content/uploads/2018/07/1025-2.png" alt="1025-2" width="668" height="443" /></a></p>
<p><strong>解决方法：</strong></p>
<p>方法1, 在S3中增加桶的策略，使该桶允许该CloudFront访问，以下是policy示例，其中标黄部分需要替换成用户自己的信息。</p>
<p><code>{</code></p>
<p><code>                "Version": "2008-10-17",</code></p>
<p><code>                "Id": "PolicyForCloudFrontPrivateContent",</code></p>
<p><code>                "Statement": [</code></p>
<p><code>                                {</code></p>
<p><code>                                                "Sid": "1",</code></p>
<p><code>                                                "Effect": "Allow",</code></p>
<p><code>                                                "Principal": {</code></p>
<p><code>                                                                "AWS": "arn:aws:iam::CloudFront:user/CloudFront Origin Access Identity E344H6KAFBMK0I"</code></p>
<p><code>                                                },</code></p>
<p><code>                                                "Action": "s3:GetObject",</code></p>
<p><code>                                                "Resource": "arn:aws:s3:::elastictcoutputthumb/*"</code></p>
<p><code>                                }</code></p>
<p><code>                ]</code></p>
<p><code>}</code></p>
<p>方法2, 重新创建distribution, 新建的distribution中Grant Read Permissions on Bucket选择yes, Update bucket policy， 这样当distribution创建完成后，s3桶的policy会被自动更新。</p>
<p><strong>场景2. 普通的S3回源</strong></p>
<p>CloudFront 并未使用Restrict Bucket Access, 这种情况下如果s3中的对象没有设置成可被公共访问，也会出现Access Denied的错误。</p>
<p><strong>解决方法：</strong></p>
<p>可以通过设置s3桶的bucket policy或者设置s3中对象的Object ACL来实现。 例如，通过 AWS 控制台设置存储桶的bucket policy:</p>
<p><a href="https://www.fuwuqiok.com/wp-content/uploads/2018/07/1025-3.png"><img class="attachment-medium" src="https://www.fuwuqiok.com/wp-content/uploads/2018/07/1025-3.png" alt="1025-3" width="607" height="401" /></a></p>
<p>通过 AWS 控制台设置S3对象的Object ACL:</p>
<p><a href="https://www.fuwuqiok.com/wp-content/uploads/2018/07/1025-4.png"><img class="attachment-medium" src="https://www.fuwuqiok.com/wp-content/uploads/2018/07/1025-4.png" alt="1025-4" width="684" height="234" /></a></p>
<p>注：如果想了解S3访问控制的详细内容，请参考：<a href="http://docs.aws.amazon.com/AmazonS3/latest/dev/s3-access-control.html" target="_blank" rel="noopener noreferrer">http://docs.aws.amazon.com/AmazonS3/latest/dev/s3-access-control.html</a></p>
<h3>错误二 使用自定义域名但未在CloudFront中配置</h3>
<p>用户有时不直接使用CloudFront的distribution产生的域名，而是使用了自定义的域名并用CNAME的方式指到CloudFront的域名, 例如使用cdn.mydomain.com  CNAME到d1cbzf61pdxxxx.CloudFront.net。此外，如果使用Route53作为DNS, 也可以不采用CNAME的方式，而是采用Alias的方式。</p>
<p>CloudFront规定当使用自定义域名并配置该域名使用CNAME或Alias的方式指向CloudFront distribution的域名的时候，需要在CloudFront相应的distribution中提供该自定义的域名，如果使用了多个自定义的域名，则提供多个自定义的域名。如果没有提供，就会出现类似下图的错误：</p>
<p><a href="https://www.fuwuqiok.com/wp-content/uploads/2018/07/1025-5.png"><img class="attachment-medium" src="https://www.fuwuqiok.com/wp-content/uploads/2018/07/1025-5.png" alt="1025-5" width="608" height="226" /></a></p>
<p><strong>解决方法：</strong></p>
<p>可以通过AWS控制台，对distribution中的Alternate Domain Names(CNAMEs)进行设置：</p>
<p><a href="https://www.fuwuqiok.com/wp-content/uploads/2018/07/1025-6.png"><img class="attachment-medium" src="https://www.fuwuqiok.com/wp-content/uploads/2018/07/1025-6.png" alt="1025-6" width="667" height="275" /></a></p>
<h3>错误三 访问路径错误</h3>
<p>配置完CloudFront的Behavior后，用户有时不能给出正确的url来访问想要的资源。 出现访问错误, 如果是回源s3, 返回的错误通常如下：</p>
<p><a href="https://www.fuwuqiok.com/wp-content/uploads/2018/07/1025-7.png"><img class="attachment-medium" src="https://www.fuwuqiok.com/wp-content/uploads/2018/07/1025-7.png" alt="1025-7" width="748" height="196" /></a></p>
<p>如果是回源的自定义网站，返回的错误根据网站的不同而不同，例如：返回”找不到相应的页面”等错误。</p>
<p><strong>解决方法：</strong></p>
<p>避免这种错误很简单，了解CloudFront Behavior的url与所访问的源站资源的对应方法，即可判别自己的url是否正确. 以下举例说明：</p>
<p>某Behavior如下，该Behavior对应的origin ID是S3-hxybucket/Picture:</p>
<p><a href="https://www.fuwuqiok.com/wp-content/uploads/2018/07/1025-8.png"><img class="attachment-medium" src="https://www.fuwuqiok.com/wp-content/uploads/2018/07/1025-8.png" alt="1025-8" width="909" height="258" /></a></p>
<p>进入到Origin查看,可知Origin Domain Name and Path是hxybucket.s3.amazonaws.com/Picture</p>
<p><a href="https://www.fuwuqiok.com/wp-content/uploads/2018/07/1025-81.png"><img class="attachment-medium" src="https://www.fuwuqiok.com/wp-content/uploads/2018/07/1025-81.png" alt="1025-81" width="909" height="258" /></a></p>
<p>如果通过d1cbzf61pdxxxx.CloudFront.net/dog.jpg访问的话， 对应的源站资源是hxybucket.s3.amazonaws.com/Picture/dog.jpg</p>
<p>如果通过d1cbzf61pdxxxx.CloudFront.net/jpg/dog.jpg访问的话，对应的源站资源是hxybucket.s3.amazonaws.com/Picture/jpg/dog.jpg</p>
<p>即: 将CloudFront域名后面的路径追加到Origin Domain Name and Path (注意，除了Domain Name之外，还有Path) 所对应的路径后面, 就是对应到源站的资源, 用户通过该路径即可判断所使用的url是否正确。</p>
<h3>错误四 HTTP Method 设置不当</h3>
<p>在创建Behavior的时候， allowed http methods选项的默认值是GET和HEAD, 有时用户会使用其他的HTTP method, 例如POST, 此时如果还是用默认值，就会出错，返回的错误通常如下：</p>
<p>“This distribution is not configured to allow the HTTP request method that was used for this request. The distribution supports only cachable requests.”</p>
<p><strong>解决办法：</strong></p>
<p>办法很简单，在Behavior中重新设定一下Allowed HTTP Methods选项，使其包含所用的HTTP Method.</p>
<p><a href="https://www.fuwuqiok.com/wp-content/uploads/2018/07/1025-10.png"><img class="attachment-medium" src="https://www.fuwuqiok.com/wp-content/uploads/2018/07/1025-10.png" alt="1025-10" width="616" height="81" /></a></p>
<h3>错误五 设置了Restrict Viewer Access 却没有使用Signed URL或Signed Cookie</h3>
<p>在创建Behavior的时候，Restrict Viewer Access (Use Signed URLs or<br />
Signed Cookies)选项的默认值是No, 如果用户改成了Yes, 此时该Behavior对应的资源必须使用Signed URL 或者Signed Cookie的方式访问，如果使用普通的Url访问，返回的错误通常如下：</p>
<p><a href="https://www.fuwuqiok.com/wp-content/uploads/2018/07/1025-11.png"><img class="attachment-medium" src="https://www.fuwuqiok.com/wp-content/uploads/2018/07/1025-11.png" alt="1025-11" width="522" height="140" /></a></p>
<p><strong>解决方法：</strong></p>
<p>方法１.使用signed url 或signed Cookie进行访问，具体参考：<a href="http://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/PrivateContent.html" target="_blank" rel="noopener noreferrer">http://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/PrivateContent.html</a></p>
<p>方法2. 将Restrict Viewer Access (Use Signed URLs or<br />
Signed Cookies)的值改为No。如图：</p>
<p><a href="https://www.fuwuqiok.com/wp-content/uploads/2018/07/1025-12.png"><img class="attachment-medium" src="https://www.fuwuqiok.com/wp-content/uploads/2018/07/1025-12.png" alt="1025-12" width="581" height="126" /></a></p>
<h3>错误六  Object Caching 设置不当</h3>
<p>虽然能够访问到源，但有时用户会反映使用了CloudFront并没有加速访问，有时甚至效果还不如未使用CloudFront时。 这很可能是由于Object Caching设置不当造成的。</p>
<p><strong>解决方法:</strong></p>
<p>Object Caching有两个选项，分别是Use Origin Cache Headers 和 Customize。默认选项是前者。但是，当默认选择了Use Origin Cache Headers，而源的HTTP header中却没有Cache-control的头，那返回内容就不被缓存了。 因此，用户需谨慎选择，当源的返回值中没有Cache-control头的情况下，选择Customize，Customize中的Default值将会成为TTL时间(时间单位是秒)。</p>
<p><a href="https://www.fuwuqiok.com/wp-content/uploads/2018/07/1025-13.png"><img class="attachment-medium" src="https://www.fuwuqiok.com/wp-content/uploads/2018/07/1025-13.png" alt="1025-13" width="483" height="242" /></a></p>
<p>另外，如果源的返回值中存在Cache-control，而Object Caching又选择了Customize, 这种情况下返回的内容肯定会在CloudFront边缘节点中被缓存。但CloudFront会使用哪个值作为TTL呢？ 这个在CloudFront 文档中有详细的描述， 详见： <a href="http://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/Expiration.html" target="_blank" rel="noopener noreferrer">http://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/Expiration.html</a></p>
<p><a rel="nofollow" href="https://www.fuwuqiok.com/amazon-cloudfront%e5%b8%b8%e8%a7%81%e9%94%99%e8%af%af%e9%85%8d%e7%bd%ae%e5%8f%8a%e8%a7%a3%e5%86%b3%e6%96%b9%e6%b3%95/">Amazon CloudFront常见错误配置及解决方法</a>，首发于<a rel="nofollow" href="https://www.fuwuqiok.com">服务器安全维护工作室</a>。</p>
]]></content:encoded>
			<wfw:commentRss>https://www.fuwuqiok.com/amazon-cloudfront%e5%b8%b8%e8%a7%81%e9%94%99%e8%af%af%e9%85%8d%e7%bd%ae%e5%8f%8a%e8%a7%a3%e5%86%b3%e6%96%b9%e6%b3%95/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
