[英]How to force Cloudfront to make all requests from origin forced to be HTTPS?
[英]How to make sure cloudfront behavior uses the correct origin without trailing slash
我們在一個雲端托管了幾個具有獨立 Hosted Origins 的項目。 雲端有一個指向Origin1
的默認行為。
我們可以通過訪問website.com
來訪問它
子目錄/experience
的第二個行為應該指向另一個來源Origin2
。
cloudfront 使用 Cloudfront function 來處理尾隨/
如下
function handler(event) {
var request = event.request;
var uri = request.uri;
// Check whether the URI is missing a file name.
if (uri.endsWith('/')) {
request.uri += 'index.html';
}
// Check whether the URI is missing a file extension.
else if (!uri.includes('.')) {
request.uri += '/index.html';
}
return request;
}
當訪問website.com/experience/
時, Origin2
的預期結果發生了。 但是,如果您在website.com/experience
中省略尾部斜杠,則 Origin 似乎指向Origin1
。 /experience
的index.html
被調用,但該文件夾中的文件路徑期望根為/experience
,但原點仍指向Origin1
。 有什么方法可以將/experience
的行為設置為指向正確的來源嗎?
我查看了 Cloudfront 函數以進行重定向,但看起來您無法更改原點。
當 function 更改 uri 值時,它不會更改請求的緩存行為或源請求發送到的源。
來自Cloudfront Functriions 事件結構 | 請求 Object
我們還設置了一個 static 托管的 s3 Bucket 來托管一個項目,同樣的結果發生在該 Bucket 中的/experience
的子目錄中。 cloudfront 上的原始路徑的路徑模式為/experience*
並且原始路徑為/experience
AWS Buckets 是“線性”的,因此沒有層次結構,但我希望如果目錄名稱存在,或者雲端行為指向正確的來源,那么就不會有問題。
這是預期的行為。 就像你說的,改變uri
不會改變原點。
您可以執行 URL 重定向來處理website.com/experience
路徑:
function handler(event) {
var request = event.request;
var uri = request.uri;
// Check whether the URI is missing a file name.
if (uri.endsWith('/')) {
request.uri += 'index.html';
}
// Check whether the URI is missing a file extension.
else if (!uri.includes('.')) {
request.uri += '/index.html';
}
// Check /experience path
else if (uri === '/experience') {
var response = {
statusCode: 302,
statusDescription: 'Found',
headers:
{ "location": { "value": Origin2-PATH } }
}
return response;
}
return request;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.