LoginSignup
11
11

More than 5 years have passed since last update.

nginxでrequest_uriに応じてHTTP/HTTPS片方のみ許可

Last updated at Posted at 2013-05-10

トップページ("/")はHTTPで見せるけど, それ以外のページ(= ログイン後)はHTTPSのみ許容するという設定. server directive内で使う.
nginxは多段ifが使えないので力技な書き方になる(他にいいやり方あるのだろうか...)

set $https_and_top "";
if ( $scheme = "https" )  { set $https_and_top "${https_and_top}HTTPS"; }
if ( $request_uri = "/" ) { set $https_and_top "${https_and_top}&TOP"; }
if ( $https_and_top = "HTTPS&TOP" ) {
    return 301 http://hoge.com/;
}

set $http_and_not_top "";
if ( $scheme = "http" )    { set $http_and_not_top "${http_and_not_top}HTTP"; }
if ( $request_uri != "/" ) { set $http_and_not_top "${http_and_not_top}&NOTTOP"; }
if ( $http_and_not_top = "HTTP&NOTTOP") {
    return 301 https://hoge.com$request_uri;
}
11
11
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
11
11