2016-04-08 5 views
0

私はfastcgi_passを使ってnginxの設定をしています。 proxy_passと同じように動作すると確信しています。匿名/ゲストユーザ用nginxを使用して、バックエンドから複数のセットクッキー応答ヘッダーを条件付きで非表示にする方法はありますか?

、私はPHPのsession_start()(私もcache-controlexpirespragmaヘッダを非表示)によって設定されたset-cookieレスポンスヘッダを非表示にしたいが、ログインしたユーザのための(と匿名ユーザーがログインしたとき)、バックエンドによって送信されたすべてのset-cookieヘッダーを渡したいと思います。

私はアプリを特別なヘッダー(X-SPECIAL)に設定しました。これは、nginxでどのような種類の応答であるかを決定するために検査することができます。

バックエンドが単一のset-cookieヘッダーを送信すると、必要な場合にのみ$upstream_http_set_cookieを使用して正常に渡します。しかし、バックエンドが複数のクッキーを複数のset-cookieレスポンスヘッダーを使用して設定すると、$upstream_http_set_cookieには1つの値しか含まれないため、クライアントは1つのクッキーしか見ることができません。この結果、ログインが機能しなくなります。

$upstream_http_set_cookieですべての値を取得するにはどうすればよいですか? あるいは、条件付きでfastcgi_hide_headersを設定する方法はありますか?私はhide_headersが変数をサポートしていないことを知っています、そして、それは要求ごとに異なって設定することはできないようです。

openrestyとluaを使用する必要がありますか?

マイ設定:

fastcgi_cache_path /var/nginx/cache levels=1:2 keys_zone=my_cache:10m; 

map $upstream_http_x_special $not_store_in_cache { 
    default "1"; 
    guest "0"; 
} 

map $upstream_http_x_special $maybe_set_cookie { 
    default $upstream_http_set_cookie; 
    guest ""; 
} 

server { 
    listen 80 default_server; 
    listen [::]:80 default_server ipv6only=on; 

    root /usr/share/nginx/html; 
    index index.php index.html index.htm; 

    server_name localhost; 

    location/{ 
     try_files $uri $uri/ =404; 
    } 

    error_page 404 /404.html; 
    error_page 500 502 503 504 /50x.html; 
    location = /50x.html { 
     root /usr/share/nginx/html; 
    } 

    location ~ \.php$ { 
     try_files $uri =404; 

     set $not_from_cache ""; 
     if ($request_method !~ ^(GET|HEAD)$) { 
      set $not_from_cache "1"; 
     } 
     if ($cookie_phpsessid != '') { 
      set $not_from_cache "1"; 
     } 
     # conditionally don't serve request from cache 
     fastcgi_cache_bypass $not_from_cache; 
     # conditionally don't store response into cache 
     fastcgi_no_cache $not_from_cache$not_store_in_cache; 
     fastcgi_cache my_cache; 
     fastcgi_cache_key $scheme$host$request_uri; 
     fastcgi_cache_valid any 1s; 
     # serve from cache while generating next response 
     fastcgi_cache_use_stale error timeout updating http_500 http_503; 
     # single request to backend, no matter how many requests for same resource. 
     fastcgi_cache_lock on; 
     # store in cache regardless of response headers 
     fastcgi_ignore_headers expires cache-control set-cookie; 
     # remove these headers from response before passing back to client. 
     fastcgi_hide_header pragma; 
     fastcgi_hide_header expires; 
     fastcgi_hide_header cache-control; 
     fastcgi_hide_header x-special; 

     # Fake conditional hide response header by always hiding and 
     # conditionally adding it back. 
     # Works only when there is a single "set-cookie" header, 
     # fails when backend sets multiple cookies. 
     fastcgi_hide_header set-cookie; 
     add_header Set-Cookie $maybe_set_cookie; 

     # standard code for php backend 
     fastcgi_split_path_info ^(.+\.php)(/.+)$; 
     fastcgi_pass unix:/var/run/php5-fpm.sock; 
     fastcgi_index index.php; 
     fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
     include fastcgi_params; 
    } 
} 

答えて

0

は、ここで問題if回避$not_from_cacheを設定する方法です。完全な答えではなく、いくつかの助けを借りて1つにつながる可能性があります。

map $request_method,$cookie_phpsessid,$uri $not_from_cache { 
    default 0; 
    ~*^POST,[^,]*,.*\.php$ 1; # match php POST request 
    ~*,[^,]+,.*\.php$ 1;  # match php request with $cookie_phpsessid set 
} 
+0

'if'は問題ではありません。問題は、いくつかのレスポンスでは 'fastcgi_hide_header set-cookie; 'が必要で、いくつかはセットクッキーヘッダーを渡したいということです。 1つのセットのクッキーヘッダーがある場合、私はいつも非表示にして、それを条件付きで元に戻すことができます。しかし、複数のセットのクッキーヘッダーがある場合、私はそれを行う方法がわかりません。 –

関連する問題