LoginSignup
0
0

More than 1 year has passed since last update.

【Ionic/Angular Universal】本番環境のみ強制SSL(httpsリダイレクト)の設定

Posted at

背景

Angularアプリケーションを、Google App Engineの環境にデプロイした利用しているアプリで、公式ドキュメントにある設定を試しても強制SSLが対応できなかった

※ 一部抜粋

app.yaml
handlers:
- url: /.*
  script: auto
  secure: always
  redirect_http_response_code: 301

そのため、以下のAngularUniversalの設定ファイルにロジックを追記して、https以外のプロトコルの場合は強制的にhttpsにリダイレクトさせる設定を追加した

設定内容

server.ts
import { environment } from './src/environments/environment';

 ~~ 中略 ~~

// The Express app is exported so that it can be used by serverless Functions.
export function app(): express.Express {

  ~~ 中略 ~~

  // All regular routes use the Universal engine
  server.get('*', (req, res) => {
    // 本番環境のみhttps強制リダイレクト
    if (environment.production && req.headers['x-forwarded-proto'] !== 'https') {
      return res.redirect(['https://', req.get('Host'), req.url].join(''));
    }

    res.render(indexHtml, {
      req,
      providers: [{ provide: APP_BASE_HREF, useValue: req.baseUrl }],
    });
  });

  return server;
}

※ ローカル環境はhttpのままアクセスさせたかったので、本番環境のみhttpsにリダイレクトさせるようにした

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0