Wordpress 301 重定向。从 Cloudfront 到 LoadBalancer - AWS

Wordpress 301 redirect. From Cloudfront to LoadBalancer - AWS

提问人:debek 提问时间:9/18/2023 最后编辑:debek 更新时间:9/20/2023 访问量:81

问:

问题: 当进入 example.com 域时,它会执行 301 重定向到 LoadBalancer 地址 (loadbalancer.xx.xx.com)。应从原始域地址提供站点。

我的基础架构:

ECS (Dockefile)(端口 80) -> LoadBalancer(端口 80) -> CloudFront(端口) 443) -> example.com

例如,当我第一次启动应用程序时,看到 wordpress 配置屏幕会立即将我重定向到

example.com/wp-admin/install.php->301-> loadbalancer.xx.xx.com/wp-admin/install.php

我是如何尝试修复它的:

  1. 我完全清理了.htaccess文件,不幸的是,这没有帮助
  2. 我把它添加到wp-config-docker.php

定义('WP_HOME', getenv_docker('WP_HOME', ''));

定义('WP_SITEURL', getenv_docker('WP_SITEURL', ''));

现在,当我转到 example.com 时,它会将我重定向到

example.com/wp-admin/install.php

这是正确的。填写安装表单后,它会再次运行 301 重定向到负载均衡器。

https://example.com -> http://loadbalancer.xx.xx.com

  1. 我检查了数据库。我把它转储到文件中.sql里面我看不到任何像“loadbalancer.xx.xx.com”这样的条目。

这是我的代码:

来自官方网站的 Wordpress:https://github.com/docker-library/wordpress/tree/940a0d35951a0917622c35acc92b38b1db3c730f/latest/php8.2/apache

ECS Terraform 定义(如 docker-compose):

# ECS Task Definition
resource "aws_ecs_task_definition" "woocommerce_task" {
  depends_on = [ aws_efs_access_point.wp_content ]
  family                   = var.task_family
  network_mode             = var.network_mode
  requires_compatibilities = var.task_compatibilities
  cpu                      = var.task_cpu
  memory                   = var.task_memory
  execution_role_arn       = aws_iam_role.ecs_execution_role.arn
  task_role_arn           = aws_iam_role.ecs_task_role.arn
  container_definitions = jsonencode([
    {
      name      = var.container_name,
      image     = "${var.ecr_repository_url}:${var.container_image_tag}",
      # user = "33:33",
      user = "0:0",
      essential = true,
      mountPoints: [{
        "sourceVolume": "wp-content",
        "containerPath": "/var/www/html/"
        # "containerPath": "/var/www/html/wp-content/"
      }],
      environment = [
        {
          name  = "WORDPRESS_DB_NAME",
          value = var.db_name
        },
        {
          name  = "WORDPRESS_DB_HOST",
          value = "${var.db_host}:3306"
        },
        {
          name  = "WORDPRESS_DB_USER",
          value = "XXXX"
        },
        {
          name  = "WORDPRESS_DB_PASSWORD",
          value = "XXXX"
        },
        {
          name  = "TAR_OPTIONS",
          value = "--no-same-owner"
        },
        # {
        #   name  = "WP_HOME",
        #   value = "https://example.com"
        # },
        # {
        #   name  = "WP_SITEURL",
        #   value = "https://example.com"
        # }
      ],
      portMappings = [{
        containerPort = var.container_port,
        hostPort      = var.container_port,
        protocol      = "tcp"
      }],
      logConfiguration = {
        logDriver = "awslogs",
        options = {
          "awslogs-group"         = aws_cloudwatch_log_group.ecs_logs.name,
          "awslogs-region"        = var.aws_region,
          "awslogs-stream-prefix" = "ecs"
        }
      }
    }
  ])

LoadBalancer Terraform:

resource "aws_lb" "nan_lb" {
  name                       = var.lb_name
  internal                   = false # Set to false if you want to expose the load balancer to the internet. Cloudfront need it.
  load_balancer_type         = "application"
  security_groups = [aws_security_group.allow_all.id]
  enable_deletion_protection = false
  subnets                    = [var.subnet_a_id, var.subnet_b_id]
  access_logs {
    bucket  = aws_s3_bucket.lb_logs.id
    enabled = true
  }
}

Cloudfront Terraform:

# CloudFront Distribution Configuration
resource "aws_cloudfront_distribution" "cloudfront_dist" {
  depends_on   = [aws_wafv2_web_acl.nan_web_acl]
  provider     = aws.east
  aliases      = [var.domain_name, "www.${var.domain_name}"]
  http_version = "http2and3"
  web_acl_id   = aws_wafv2_web_acl.nan_web_acl.arn
  # https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/PriceClass.html
  price_class = "PriceClass_100"

  origin {
    domain_name = aws_lb.nan_lb.dns_name
    origin_id   = "nan_lb"

    custom_origin_config {
      http_port                = 80
      https_port               = 80
      origin_protocol_policy   = "http-only"
      origin_ssl_protocols     = ["TLSv1.2"]
      origin_keepalive_timeout = 5
      origin_read_timeout      = 30
    }
  }

  enabled             = true
  is_ipv6_enabled     = true
  comment             = "CloudFront distribution for nan-woocommerce"
  default_root_object = "index.php"
  # default_root_object = "index.html"

  default_cache_behavior {
    allowed_methods  = ["GET", "HEAD", "POST", "OPTIONS", "PUT", "PATCH", "DELETE"]
    cached_methods   = ["GET", "HEAD", "OPTIONS"]
    target_origin_id = "nan_lb"
    compress         = true

    forwarded_values {
      query_string = true
      query_string_cache_keys = ["*"]

      headers = ["Origin"]

      cookies {
        forward           = "whitelist"
        whitelisted_names = ["comment_*", "wordpress_*", "wp-settings-*"] 
      }
    }

    viewer_protocol_policy = "redirect-to-https"
    min_ttl                = 0
    default_ttl            = 300
    max_ttl                = 31536000
  }
  ..........
  ..............

问题: 有没有人可能知道问题在哪里以及如何解决它?

wordpress apache amazon-cloudfront aws-application-load-balancer

评论

0赞 Mark B 9/18/2023
在任务定义中取消注释 和 环境变量并重新部署时会发生什么情况?WP_HOMEWP_SITEURL

答:

0赞 debek 9/20/2023 #1

为了未来。

有两个问题。

  1. Cloudfront 中的缓存错误。我禁用了缓存,然后对其进行了调整。在那之后,我没有CSS样式。

  2. CSS 样式未加载,因为我的 LoadBalancer 有端口 80 而不是 443。