簡體   English   中英

如何使用 AWS 的 AWS Cloudfront 模塊轉發所有 cookies

[英]How to foward all cookies with the AWS Cloudfront Module from AWS

我如何在 AWS Cloudfront 模塊中設置 cookies 我在 AWS Terraform 模塊的官方文檔中找不到任何內容

module "cdn" {
  source = "terraform-aws-modules/cloudfront/aws"

  ordered_cache_behavior = [
    {
      path_pattern           = "/wp-admin/*"
      target_origin_id       = "loadbalancer"
      viewer_protocol_policy = "redirect-to-https"

      allowed_methods = ["GET", "HEAD", "OPTIONS", "PUT", "POST", "PATCH", "DELETE"]
      headers         = ["*"]
      forward_cookies = "all"
      compress        = true
      query_string    = true
    },


    {
      path_pattern           = "/wp-login.php/"
      target_origin_id       = "loadbalancer"
      viewer_protocol_policy = "redirect-to-https"

      allowed_methods = ["GET", "HEAD", "OPTIONS", "PUT", "POST", "PATCH", "DELETE"]
      headers         = ["*"]
      forward_cookies = "all"
      compress        = true
      query_string    = true
    }
  ]

我嘗試了不同的方法,比如 set


cookies[*]
forward_cookies = "all"

    forwarded_values {
      query_string = false
      headers      = ["Origin"]

      cookies {
        forward = "all"
      }
    }

當我運行它時,我沒有收到任何錯誤,但它在 Cloudfront 中將任何內容設置為無。 當我嘗試設置任何 cookies 時,也會發生同樣的情況。有人有解決問題的辦法嗎,還是我應該使用官方資源。

查看源代碼,您似乎需要指定cookies_forward = "all"而不是forward_cookies 像這樣:

  ordered_cache_behavior = [
    {
      path_pattern           = "/wp-admin/*"
      target_origin_id       = "loadbalancer"
      viewer_protocol_policy = "redirect-to-https"

      allowed_methods = ["GET", "HEAD", "OPTIONS", "PUT", "POST", "PATCH", "DELETE"]
      headers         = ["*"]
      cookies_forward = "all"
      compress        = true
      query_string    = true
    },

您可以使用指定不緩存 cookies 的緩存策略,而不是使用cookies_forward = "all"屬性。

為此,您可以使用 AWS 提供的默認緩存策略之一,如下所示:

data "aws_cloudfront_cache_policy" "caching_disabled" {
  name = "Managed-CachingDisabled"
}

然后將其設置在您的ordered_cache_behavior塊之一中,如下所示:

  ordered_cache_behavior = [
    {
      ...
      cache_policy_id = data.aws_cloudfront_cache_policy.caching_disabled.id
      ...
    }

或者您可以創建自己的,如下所示:

resource "aws_cloudfront_cache_policy" "forward_all_cookies" {
  name        = "Forward-All-Cookies"
  comment     = "Policy with cookie caching disabled"
  default_ttl = 0
  max_ttl     = 0
  min_ttl     = 0
  parameters_in_cache_key_and_forwarded_to_origin {
    cookies_config {
      cookie_behavior = "none"
    }
    headers_config {
      header_behavior = "none"
    }
    query_strings_config {
      query_string_behavior = "none"
    }
  }
}

然后將其設置在您的ordered_cache_behavior塊之一中,如下所示:

  ordered_cache_behavior = [
    {
      ...
      cache_policy_id = aws_cloudfront_cache_policy.forward_all_cookies
      ...
    }

進一步參考: https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/using-managed-cache-policies.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM