私はfastcgi_pass
を使ってnginxの設定をしています。 proxy_pass
と同じように動作すると確信しています。匿名/ゲストユーザ用nginxを使用して、バックエンドから複数のセットクッキー応答ヘッダーを条件付きで非表示にする方法はありますか?
、私はPHPのsession_start()
(私もcache-control
、expires
とpragma
ヘッダを非表示)によって設定された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;
}
}
'if'は問題ではありません。問題は、いくつかのレスポンスでは 'fastcgi_hide_header set-cookie; 'が必要で、いくつかはセットクッキーヘッダーを渡したいということです。 1つのセットのクッキーヘッダーがある場合、私はいつも非表示にして、それを条件付きで元に戻すことができます。しかし、複数のセットのクッキーヘッダーがある場合、私はそれを行う方法がわかりません。 –