4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

nginxで特定のURLを知っている人のみアクセスできるWebページをCookieを使って実現する

Posted at

これはなに

Webの開発環境などパブリックにしたくないWebサイトを
特定の他者に公開したいときや、もしくは自分のモバイル端末などで確認したいときがあります。
そういう場合に、Cookieに秘匿情報を設定することで突破することができます。

ここで、リテラシーの問題やスマホブラウザなど機能的にCookieに値を設定するのが困難な場合があります。
そこで特定のURLのみ公開し、そのURLでCookieをセットします。

この設定が生きる場面

  • SPAなどでauthorizationヘッダを使っているためBasic認証が使えない
  • コワーキングスペースなど公共の場所からアクセスしたいのでIPアドレスでの制限が使えない
  • Cookieが容易に変更できないスマホブラウザなどに対応したい

設定部分


    location = /hogehogehoge {
       add_header Set-Cookie "secret=fugafugafuga";
       return 302 https://$host;
    }

    location / {
        set $interrupt true;

        if ( $cookie_secret = "fugafugafuga" ) {
            set $interrupt false;
        }

        # if ( $geo_office ) {
        #     set $interrupt false;
        # }

        if ( $interrupt = true ) {
            return 403;
        }

        proxy_pass                  http://backend;
    }

解説

location = /hogehogehogeが、秘匿情報をセットするURLになります。
適当な文字を設定しましょう。

cookie secretの値にfugafugafugaが入り、トップ画面にリダイレクトされます。

location /では、条件に基づいて$interrupt変数が変更され、
最終的に403を返すかどうかを判定します。

前述したcookiesecretの値で判断することや、geoを使ってIPアドレスで許可することもできます。

4
2
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
4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?