Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
nginx:基于头值在请求中设置cookie_Nginx - Fatal编程技术网

nginx:基于头值在请求中设置cookie

nginx:基于头值在请求中设置cookie,nginx,Nginx,我刚刚开始使用nginx,我正在使用它来代理应用服务器。如果http请求中存在特定的自定义头,我想在向应用程序服务器发送的代理请求中设置cookie。逻辑是: if X-SESSID in request AND SESSID is not already a cookie in the request set cookie "SESSID=$http_X-SESSID" 在Apache2中,我可以通过这样做来实现这一点: RewriteCond %{HTTP:X-SESSID} ^(

我刚刚开始使用nginx,我正在使用它来代理应用服务器。如果http请求中存在特定的自定义头,我想在向应用程序服务器发送的代理请求中设置cookie。逻辑是:

if X-SESSID in request
AND SESSID is not already a cookie in the request
    set cookie "SESSID=$http_X-SESSID"
在Apache2中,我可以通过这样做来实现这一点:

RewriteCond %{HTTP:X-SESSID} ^(.*)$
RewriteCond %{HTTP_COOKIE} !SESSID [NC]
RewriteRule (.*) - [E=SESSID:%1]
RequestHeader set Cookie "SESSID=%{SESSID}e" env=SESSID

nginx中的等效方法是什么?

您可以这样做:

# $sessid variable will get a "sessid=$http_x_sessid" value
# if the X-Sessid HTTP header is set or an empty value otherwise
map $http_x_sessid $sessid {
    ""       "";
    default  "sessid=$http_x_sessid";
}

# $sessid_cookie variable will get a value of $sessid variable
# if no sessid cookie passed with the request or an empty value otherwise
map $cookie_sessid $sessid_cookie {
    ""       $sessid;
    default  "";
}

server {
    ...
    # in the same location block where you have a proxy_pass directive
    proxy_set_header Cookie "$http_cookie$sessid_cookie";
    ...
}
请参阅
映射
块、
$http.
变量和
$cookie.
变量的说明

更新@2020.11.12

回顾一个答案,我认为有一个缺陷。如果浏览器发送一些带有传入请求的cookie,则应使用
添加额外的cookie要与其他Cookie分开的前缀。以下是更新版本:

# prepend cookie with the "; " if the other cookies exists
map $http_cookie $prefix_cookie {
    ""       "";
    default  "; ";
}

# $sessid variable will get a "sessid=$http_x_sessid" value (optionally prepended
# with "; ") if the X-Sessid HTTP header is set or an empty value otherwise
map $http_x_sessid $sessid {
    ""       "";
    default  "${prefix_cookie}sessid=${http_x_sessid}";
}

# $sessid_cookie variable will get a value of $sessid variable
# if no sessid cookie passed with the request or an empty value otherwise
map $cookie_sessid $sessid_cookie {
    ""       $sessid;
    default  "";
}

server {
    ...
    # in the same location block where you have a proxy_pass directive
    proxy_set_header Cookie "$http_cookie$sessid_cookie";
    ...
}

您可以这样做:

# $sessid variable will get a "sessid=$http_x_sessid" value
# if the X-Sessid HTTP header is set or an empty value otherwise
map $http_x_sessid $sessid {
    ""       "";
    default  "sessid=$http_x_sessid";
}

# $sessid_cookie variable will get a value of $sessid variable
# if no sessid cookie passed with the request or an empty value otherwise
map $cookie_sessid $sessid_cookie {
    ""       $sessid;
    default  "";
}

server {
    ...
    # in the same location block where you have a proxy_pass directive
    proxy_set_header Cookie "$http_cookie$sessid_cookie";
    ...
}
请参阅
映射
块、
$http.
变量和
$cookie.
变量的说明

更新@2020.11.12

回顾一个答案,我认为有一个缺陷。如果浏览器发送一些带有传入请求的cookie,则应使用
添加额外的cookie要与其他Cookie分开的前缀。以下是更新版本:

# prepend cookie with the "; " if the other cookies exists
map $http_cookie $prefix_cookie {
    ""       "";
    default  "; ";
}

# $sessid variable will get a "sessid=$http_x_sessid" value (optionally prepended
# with "; ") if the X-Sessid HTTP header is set or an empty value otherwise
map $http_x_sessid $sessid {
    ""       "";
    default  "${prefix_cookie}sessid=${http_x_sessid}";
}

# $sessid_cookie variable will get a value of $sessid variable
# if no sessid cookie passed with the request or an empty value otherwise
map $cookie_sessid $sessid_cookie {
    ""       $sessid;
    default  "";
}

server {
    ...
    # in the same location block where you have a proxy_pass directive
    proxy_set_header Cookie "$http_cookie$sessid_cookie";
    ...
}