LoginSignup
0
0

More than 3 years have passed since last update.

nginxリバプロで header を cookieに書き込む方法

Last updated at Posted at 2020-07-23

login基盤から送られてくる認証情報がheaderでくる。それを処理するにはサーバサイドで拾う必要があるけど、先にvueが拾っちゃうとめんどくさくって。vueに行く前にリバプロ側でcookieに入れたかった。これやったらいろいろ楽になった!

example.com.conf

location /login のところだけで、 my_header_name というheaderが来たら cookie MY_COOKIE_NAME にセットするようにしてます。


upstream lapi {
     server my-server-side.local:5000;
}

server {
    listen       80;
    server_name example.com;

     # required when LB offloads SSL
     proxy_set_header         X-Forwarded-HTTPS     on;

     # reverse proxy parameters
     proxy_buffering          off;
     proxy_set_header         Host                  $http_host;
     proxy_set_header         X-Real-IP             $remote_addr;
     proxy_set_header         X-Forwarded-Host      $host;
     proxy_set_header         X-Forwarded-Server    $host;
     proxy_set_header         X-Forwarded-For       $proxy_add_x_forwarded_for;
     proxy_redirect           off;
     proxy_read_timeout       60000;
     proxy_max_temp_file_size 0;

     location /login {

        # insert cookie when a header comes
        if ($http_my_header_name != "") {
           add_header Set-Cookie MY_COOKIE_NAME=$http_my_header_name;
        }

        proxy_pass http://lapi;  
     }

     location / {
        proxy_pass http://lapi;  
     }

nginx 便利!!

nginx posts

nginxのstatusを表示するモジュール /nginx_status
https://qiita.com/uturned0/items/b4eb6839d75050933c7f

nginxにモジュールを追加できるDockerfile
https://qiita.com/uturned0/items/3a0a7cb971edac40b11e

nginxリバプロで header を cookieに書き込む方法
https://qiita.com/uturned0/items/112b8faba403bd534d5c

nginxリバプロで、requestに入っているheaderをlogに出す方法
https://qiita.com/uturned0/items/31162eeb03af05c7dc4b

nginxだけでコンテンツを返す方法 - Load balancerのhealth checkに
https://qiita.com/uturned0/items/a8bcca111b75c5055ed9

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