リダイレクト専用Webサーバ構築

リダイレクト専用Webサーバ構築

Twitter LINEで送る Facebook はてなブログ

概要

お名前.com + AWS(S3 + CloudFront + Certificate Manager)この構成で困ること
CNAME問題があります。お名前.comのCNAMEは「www」なしのドメインをCNAMEが付けれない問題があります。

サブドメインだと問題ないんのですが直だと面倒なので直でリンクできるリダイレクト用のサーバが必須になります。 そのため、今回はvarnishを用いて代用を行います。

varnish

Varnish高負荷な動的サイト向けの高性能HTTPアクセラレータです。
LB的な感じで使われたりします。 Wiki 参考

目次

1.概要

2.手順

3.テスト

4.まとめ

1.概要

wwwがない場合のみVarnishを経由してそれ以外の場合はS3に直接アクセスが可能な状態です.

アプリバージョン
Cent OS7.6.1810
Varnish4.0.5

2.手順

インストール

su - 
yum -y install varnish

vi /etc/varnish/varnish.params

# ポート番号を変更
VARNISH_LISTEN_PORT=80

vi /etc/varnish/default.vcl

# 以下に変更
vcl 4.0;

# Default backend definition. Set this to point to your content server.
backend default {
    .host = "127.0.0.1";
    .port = "80";
}

sub vcl_recv {
    # Happens before we check if we have this in cache already.
    #
    # Typically you clean up the request here, removing cookies you don't need,
    # rewriting the request, etc.
    return (synth(301,req.url));
}

sub vcl_backend_response {
    # Happens after we have read the response headers from the backend.
    #
    # Here you clean the response headers, removing silly Set-Cookie headers
    # and other mistakes your backend does.
}

sub vcl_deliver {
    # Happens when we have all the pieces we need, and are about to send the
    # response to the client.
    #
    # You can do accounting or modifying the final object here.
}

sub vcl_synth {
    set resp.http.Location = "http://www." + req.http.host + req.url;
    set resp.status = 301;
    return (deliver);
}

起動用コマンドを実行

systemctl enable varnish.service
systemctl start varnish.service

今回は強制的にwwwをつけるようにするなので全てのパターンでwwwをつけるようにしています. ここは必要に応じて変更すれば自由に行けます.

3.テスト

テスト実行

$ wget --spider -nv --timeout 60 -t 1 http://keydrop.net
2019-08-30 15:44:47 URL: https://www.keydrop.net/ 200 OK

wwwのコードが返却されます.

4.まとめ

apacheなどのvirtual hostを使わずに大量にできるので管理が簡単に実施可能です. 安いVPSを利用すれば固定IPも手に入るので楽な方法かと思います.

SSLはこの手順では未対応なのでSSL対応は別で手順かしてみようと思います.