Google Cloud Storage 静的ウェブサイトのホスティング 時、 index.html へリダイレクトされる

静的ウェブサイトのホスティング  |  Cloud Storage  |  Google Cloud

gcs を利用し、 websiteホスティングした時に、
スラッシュ無しのパスへアクセスしたとき、/index.html が存在する場合、/index.html が付加されたURLへリダイレクトしてしまう。
しかも、CNAME (正確には Host ヘッダー)に指定した Host でリダイレクトする。

Nginx などで Proxy した場合は、特に問題となる。

そのため、リダイレクトが発生した場合にRedirect を書き換える必要がある。
~ は正規表現を利用する宣言。
example.com を CNAME とした場合の設定。

    set $proto $scheme;
    if ($http_x_forwarded_proto = "https")  { set $proto "https"; } # ELB, GCLB
    if ($http_cloudfront_forwarded_proto = "https")  { set $proto "https"; } # CloudFront

    # ...
 
    resolver 8.8.8.8 valid=60s;

    proxy_pass http://c.storage.googleapis.com;
    proxy_redirect ~example.com\/(.*)\/index.html $proto://$http_host/$1/$is_args$args; # google cloud storage

    proxy_set_header Host example.com;