Jekyll、Hugo、Pelican などの静的サイト ジェネレーターで生成された静的 Web サイトをホストする安価な方法がいくつかあります。 このブログ全体は、Jekyll を使用して静的に生成されています。ただし、上記のオプションのいずれも使用できません。なぜなら、このブログの存続期間中に、ドメイン名を変更し、URL スキームを変更し、投稿の名前を変更したためです。古い URL をすべて維持する 私はこのブログを Apache を使用してホストしており、最近では単一の仮想マシンで nginx を使用しており、どちらのソフトウェアのリダイレクト機能も問題なく動作しますが、新しい別の場所でホストする準備ができていました。 以前の投稿で、Google App Engine と Python を使用して古いドメインから新しいドメインに URL をリダイレクトする方法について説明しましたが、静的コンテンツを提供する方法が必要になりました **および** 同じドメインから URL をリダイレクトします。同じドメイン リダイレクト要件が、Google App Engine の静的コンテンツのみの機能 (上のリストにリンクされています) を単純に使用できない理由です。ただし、Google App Engine を単純な Golang アプリケーションと組み合わせて使用​​して、静的コンテンツと同じドメイン リダイレクトの両方を提供できます。 ## Google App Engine を選ぶ理由 この投稿の残りの部分に入る前に、なぜ Google App Engine でブログをホストするのか疑問に思われるかもしれません。理由は次のとおりです。 - トラフィックが App Engine の 28 インスタンス時間と 1 日あたり 1 GB の下りトラフィックの無料枠内に収まる場合、ブログのホスティングは実質的に無料です - 更新のプッシュは 1 つのコマンドで実行されます - ロギングとモニタリングは、Stackdriver を使用して統合されています - トラフィック パターンに基づく自動アップおよびダウン スケーリング - 数回クリックするだけで、Web ログを BigQuery などに簡単にプッシュして、長期保存やアドホック分析を行うことができます - LetÃâ¢ãâãâs Encrypt を使用したマネージド SSL 証明書 ## 前提条件 この投稿では、次のことを前提としています。 - Google Cloud Platform (GCP) に精通しており、既に GCP プロジェクトを作成している - Google Cloud SDK がインストールされている - あなたは Google アカウントに対する gcloudcommand GCP プロジェクトを作成する まだ作成していない場合 **GCP プロジェクトは次の手順に従います。 - ウェブブラウザを開き、Google アカウントを作成またはログインします - GCP コンソールに移動します - これが最初の GCP プロジェクトである場合は、GCP プロジェクトを作成するよう求められます。各 Google アカウントには、GCP で 12 か月以内に使用できる $300 のクレジットが付与されます。 GCP プロジェクトを作成するにはクレジット カードを入力する必要がありますが、300 ドルのクレジットが消費されるか、12 か月が経過するまで請求されません。 - これが新しい GCP プロジェクトの場合は、GCP コンソールの [Compute Engine] セクションに移動して Compute Engine API を有効にし、初期化が完了するまで待つ必要があります。 Google Cloud SDK をインストールする まだインストールしていない場合は、 **Google Cloud SDK はこちらの手順に従ってください gcloud を認証する GCP プロジェクトを作成し、Google Cloud SDK をインストールしたら、最後のステップは、 gcloud コマンドを Google アカウントに追加します。ターミナル アプリケーションを開き、次のコマンドを実行します。 gcloud 認証ログイン Web ブラウザーで Web ページが開きます。 Google アカウントを選択し、GCP へのアクセスを許可します。完了すると、認証され、先に進む準備が整います ## ディレクトリを作成する 次に、ワークステーションのどこかにディレクトリを作成して、Google App Engine アプリケーションを保存します。 mkdir ~/Sites/example.com/app_engine そのディレクトリに移動します。 cd ~/Sites/example.com/app_engine この記事の残りの部分では、このディレクトリ内で作業していると仮定します さらに、その中にディレクトリを作成します。 **static という **app_engine** ディレクトリ mkdir ~/Sites/example.com/app_engine/static 後でこのディレクトリにアクセスします ## app.yaml を作成 通常、Google App Engine には次の 2 つのファイルが必要です。 **app.yaml** と、Python、Golang、Java、または PHP で記述された **アプリケーション ファイル** - この場合は Golang になります。 **app.yaml** は、アプリケーションを実行するために必要な構成を提供します。 **app.yaml に存在できるさまざまなパラメーターが多数あります。これらのパラメーターは、使用するプログラミング言語によって異なる場合があります。この記事では Golang を使用します。使用可能なすべての Golang パラメーターはこちらで確認できます。 ファイルを作成 **app.yaml** の内容は次のとおりです。 ランタイム: go api_version: go1 ハンドラー: - url:script: _go_app secure: 常に redirect_http_response_code: 301 注意してください **secure: always** が設定されています。これは、Golang アプリケーションが常に HTTPS 経由で提供されることを意味します。エンド ユーザーが HTTP 経由で Web アプリケーションに移動すると、デフォルトで HTTPS バージョンに 302 リダイレクトされます。これが **redirect_http_response_code: 301** も設定されている理由です。私は常に Web アプリケーションを HTTPS 経由で提供したいと考えており、検索エンジンが HTTP から HTTPS へのリダイレクトを一時的なリダイレクトとして解釈することを望んでいません。それは永続的なリダイレクトです 静的アセットがあり、おそらくそうである場合は、App Engine にこれを通知し、アプリケーションではなくオブジェクト ストレージからそれらのアセットを提供することをお勧めします。これを行うのは簡単で、 **app.yaml** ファイル たとえば、favicon ファイル、CSS ディレクトリ、Javascript ディレクトリ、images ディレクトリがある場合は、次を使用します。 **app.yaml** ファイル: ランタイム: go api_version: go1 ハンドラー: - url: /favicon.png$ static_files: static/favicon.png アップロード: static/favicon.png - url: /css static_dir: static/css - url: /js static_dir: static/js - url: /images static_dir: static/images - url:script: _go_app secure: 常に redirect_http_response_code: 301 ## main.go を作成する 次に、Golang アプリケーション ファイルが必要です 次のコードがニーズを満たすようにするには、ファイルを作成します **main.go 以下のコードをコピーして貼り付け、次の変更を加えます。 - の中に domainvariable、値を変更して、ドメイン名と正しい HTTP プロトコルを一致させます - の中に urlsmap で、必要なリダイレクトに一致するようにすべてのキーと値のペアを置き換えます。各キーを現在のドメインのパス部分 (httpsexample.com/example-post-1.html ではなく /example-post-1.html) に置き換えます。生き続ける。次に、各値を、リダイレクト先の現在のドメインの新しい URL のパス部分に置き換えます。 すべてのリダイレクトは 301 リダイレクトになります。これは、変更することで変更できます 以下のコードの **301** を **302** などの別の HTTP リダイレクト ステータス コードに変更します。 package main import ( "net/http""os""strings") func init() { http.HandleFunc handler) } func handler(w http.ResponseWriter, r *http.Request) { // true (ok) if request値の場合、パスは urls マップにあります。ok := urls[r.URL.Path]; ok { 値 = ドメイン + 値 http.Redirect(w, r, 値, 301) } else { パス := "static/"+ r.URL.Path // HTTP リクエストが存在するディレクトリに対するものである場合は 403 を返します。 f, err := os.Stat(path); の場合、index.html ファイルは含まれません。 err == nil&& f.IsDir() {index := strings.TrimSuffix(path,+ "/index.html"if _, err := os.Open(index); err != nil { w.WriteHeader(403) w.Writebytehtml> 403禁止します

403 Forbidden

return } } // Return custom 404 page if HTTP request is to a non-existent file if _, err := os.Stat(path); os.IsNotExist(err) { w.WriteHeader(404) http.ServeFile(w, r, "static/404.html") return // Withoutreturn, a "404 page not found" string will be displayed at the bottom of your custom 404 page } http.ServeFile(w, r, path) return } } var domain string = "httpsexample.com" var urls = map[string]string{ "/example-post-1.html": "/post/example-post-1.html", "/example-post-2.html": "/post/example-post-2.html", "/example-post-3.html": "/post/example-post-3.html", } ## Generate the Static Content With **app.yaml** and **main.go** saved, the last piece is to generate your static content and store it in the **static** directory you created earlier How you do this entirely depends on what static site generator you are using If you are using Jekyll, you can configure the **destination** parameter in Jekyll’s _ **config.yml** file to save your static content in any directory on your workstation. So, you could set the **destination** parameter to Sites/example.com/app_engine/static and, every time you run jekyll build, the static content will be saved in that directory ## Deploy to App Engine With **app.yaml **main.go and your static content generated, you are ready to deploy your Google App Engine application Assuming gcloud is already pointed at the Google Cloud Project you want to deploy to, verify with gcloud config list project, run the following command: gcloud app deploy The command will output the appspot URL your application will be deployed to and ask if you want to continue. Typically, the appspot URL is **httpsyour-project-id.appspot.com This is also a useful self-check to make sure you are not deploying your application to the wrong Google Cloud Project. If everything looks okay, type **Y** and **Enter** to deploy your application. Depending on how large your static content is, your application should be deployed within about one minute ## Setup DNS At this point, your application is deployed under URL **httpsyour-project-id.appspot.com Unless your website uses that as its domain name, you will probably want to setup a custom domain that uses your actual current domain name The App Engine section of the Google Cloud Console can be used to do this. Go here and follow the instructions to configure your custom domain Once that is complete and DNS has had time to propagate, you should be able to navigate in your web browser to one of your current domain’s old URLs, for example **httpsexample.com/example-post-1.html and have it redirect to your current domain’s new URLs, for example **httpsexample.com/post/example-post-1.html** ## Pushing Updates To push updates, make the necessary changes in your static site’s source directory, regenerate the static content, and redeploy to Google App Engine by changing into the Sites/example.com/app_engine** directory and running gcloud app deploy ## References - A Surprising Feature of Golang that Colored Me Impressed - How to check if a map contains a key in go? - Disable directory listing with http.FileServer - 3 Ways to Disable http.FileServer Directory Listings - Handling HTTP Request Errors in GO - HTTP and Error management in Go - please add ability to set custom 404 notFoundHandler for http.FileServer