WordPress 301重定向,从Cloudfront到LoadBalancer - AWS

1zmg4dgp  于 2023-10-17  发布在  WordPress
关注(0)|答案(1)|浏览(128)

问题:当进入example.com域时,它会301重定向到LoadBalancer地址(loadbalancer.xx.xx.com)。该网站应该从原始域地址提供服务。
我的基础设施:
ECS(Dockefile)(port 80)-> LoadBalancer(port 80)-> CloudFront(port 443)-> example.com
例如,当我第一次启动应用程序时,看到WordPress配置屏幕会立即将我重定向到
example.com/wp-admin/install.php->301-> loadbalancer.xx.xx.com/wp-admin/install.php
我是如何修复它的:
1.我完全清理了.htaccess文件,不幸的是,这没有帮助
1.我把它添加到wp-docker.php
define('WP_HOME',getenv_docker('WP_HOME',''));
define('WP_SITEURL',getenv_docker(' WP_SITEURL',''));
现在,当我去example.com它重定向我到
example.com/wp-admin/install.php
这是正确的。在填写安装表单后,它再次运行一个301重定向到负载平衡器。
https://example.com -> http://loadbalancer.xx.xx.com
1.我查了数据库。我把它转储到file.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 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
  }
  ..........
  ..............

问:有没有人知道我们的问题在哪里以及如何解决?

9lowa7mx

9lowa7mx1#

为了将来。
有两个问题。

  1. Cloudfront中的错误缓存。我禁用了该高速缓存,然后调整它。在那之后,我没有CSS样式。
  2. CSS样式没有加载,因为我的LoadBalancer的端口是80而不是443。

相关问题