BLOG

ブログ

AWSでサブドメインなし(wwwなし)からサブドメインあり(wwwあり)へのリダイレクト設定

背景

AWSのS3 + cloudfrontでwebサイトを公開したが、 その際にwwwありのURL(https://www.sink-capital.com/)で公開を行った。 URLバーにはsink-capital.comと表示されていたので気づかなかったが、 サブドメインなしのURL(https://sink-capital.com/)だと繋がらないことに気付き付き修正した。

一般的な方法

基本的にはAmazon Route 53 を使って、サブドメイン(www)なしから、ありドメインへリダイレクト に記載のある方法で、 サブドメインなしのURLから来たリクエストを、 全てサブドメインありのURLにリダイレクトすることで実現を行う。

  1. サブドメインなしのURLからリクエストが来る
  2. route53のAレコードaliasでS3にリクエストを飛ばす
  3. S3の設定でサブドメインありのURLにリダイレクト

ただし上記の対応だとhttpsに対応できないという問題点があった。 (route53とS3だと証明書を入れ込むことができないため)

今回の対応方法

webサイト自体の公開方法と同様にcloudfrontを利用してS3に接続を行った。

  1. サブドメインなしのURLからリクエストが来る
  2. route53のAレコードaliasでcloudfrontにリクエストを飛ばす
  3. cloudfrontからS3にリクエストを流す
  4. S3の設定でサブドメインありのURLにリダイレクト

実際のコード(terraform)

resource aws_cloudfront_distribution sci_website {
  enabled = true
  aliases = ["${var.root_domain_name}"]
  comment = "${terraform.workspace}-www-root-alias"
  default_root_object = "index.html"
  price_class = "PriceClass_All"

  origin {
    domain_name = "${var.root_domain_name}.s3-website-${var.region}.amazonaws.com"
    origin_id = "S3-${aws_s3_bucket.sci_www_root_alias_bucket.bucket}"

    custom_origin_config {
          http_port = 80
          https_port = 443
          origin_protocol_policy = "http-only"
          origin_ssl_protocols = ["TLSv1"]
    }
  }

  default_cache_behavior {
    allowed_methods = [
      "GET",
      "HEAD"]
    cached_methods = [
      "GET",
      "HEAD"]
    target_origin_id = "S3-${aws_s3_bucket.sci_www_root_alias_bucket.bucket}"

    forwarded_values {
      query_string = true
      cookies {
        forward = "none"
      }
    }

    viewer_protocol_policy = "https-only"
    min_ttl = 3600
    default_ttl = 10800
    max_ttl = 86400
  }

  restrictions {
    geo_restriction {
      restriction_type = "none"
    }
  }

  viewer_certificate {
    acm_certificate_arn = "${aws_acm_certificate.sci_com.arn}"
    ssl_support_method = "sni-only"
    minimum_protocol_version = "TLSv1"
  }
}

resource aws_route53_record root {
  zone_id = "${var.route53_zone_id}"
  name = "${var.root_domain_name}"
  type = "A"

  alias {
    name = "${aws_cloudfront_distribution.sci_website.domain_name}"
    zone_id = "${aws_cloudfront_distribution.sci_website.hosted_zone_id}"
    evaluate_target_health = true
  }
}

resource aws_route53_record cert_validation {
  zone_id = "${var.route53_zone_id}"
  name = "${aws_acm_certificate.sci_com.domain_validation_options.0.resource_record_name}"
  type = "${aws_acm_certificate.sci_com.domain_validation_options.0.resource_record_type}"
  ttl = 60
  allow_overwrite = true
  records = [
    "${aws_acm_certificate.sci_com.domain_validation_options.0.resource_record_value}"]
}

resource aws_acm_certificate sci_com {
  provider = "aws.ue1"
  domain_name = "${var.root_domain_name}"
  subject_alternative_names = []
  validation_method = "DNS"
}

resource aws_acm_certificate_validation sci_com_validation {
  provider = "aws.ue1"
  certificate_arn = "${aws_acm_certificate.sci_com.arn}"
  validation_record_fqdns = [
    "${aws_route53_record.cert_validation.0.fqdn}"]
}
resource aws_s3_bucket sci_www_root_alias_bucket {
  bucket = "${var.root_domain_name}"
  acl = "private"

  website {
    redirect_all_requests_to = "https://www.${var.root_domain_name}"
  }
}
variable "route53_zone_id" {}
variable "root_domain_name" {
  default = "sink-capital.com"
}
variable "region" {
  default = "ap-northeast-1"
}

※ドメイン名はお好きに

感想

  • サイト公開時のterraformがあったのですぐできたが、割と0からやろうとすると大変なイメージ
  • 結局wwwをつけたほうがいいのか正解がよくわからない。。。(もともとつけないURLで公開すべき?)

参照リンク

SinkCapitalではデータに関する支援を行っています

弊社はスペシャリスト人材が多く在籍するデータ組織です。 データ分析や分析基盤の設計などでお困りの方がいらっしゃれば、 まずは無料で、こちらから各分野のスペシャリストに直接相談出来ます。