根据http_cookie首选项的国家代码,将其重写到Nginx上的相应站点

根据http_cookie首选项的国家代码,将其重写到Nginx上的相应站点,nginx,nginx-ingress,Nginx,Nginx Ingress,如何基于最终用户首选的cookies进行路由? 我们有Nginx/1.17.10在AKS中作为pod运行。电子商务网站是在这个网站上托管的。 CloudFlare是充当DNS和WAF的前端。 CloudFlare已启用GeoIP,因此我们有参数-$http\u cf\u ipcountry来跟踪国家代码。但是,我们正在寻找最终用户保存的首选项,并将其发送到该特定区域 例如: If $http_cookie --> COUNTRY_CODE=UAE; Then rewrite to exam

如何基于最终用户首选的cookies进行路由? 我们有Nginx/1.17.10在AKS中作为pod运行。电子商务网站是在这个网站上托管的。 CloudFlare是充当DNS和WAF的前端。 CloudFlare已启用GeoIP,因此我们有参数-$http\u cf\u ipcountry来跟踪国家代码。但是,我们正在寻找最终用户保存的首选项,并将其发送到该特定区域

例如:

If $http_cookie --> COUNTRY_CODE=UAE;
Then rewrite to example.com --> example.com/en-ae
If $http_cookie --> COUNTRY_CODE=KW;
Then rewrite to example.com --> example.com/en-kw
If there is no preference saved on cookie, then route to default "example.com"
Http_cookie参数还包含其他详细信息,如_cfduid、国家代码、货币代码、汇率

处理此要求的最佳方法是什么?
有人帮我吗,谢谢

我将创建一个映射来处理和构造重定向URL。

这将把重写url设置为变量$new_uri。如果不存在cookie值,则默认值为/en/。现在您可以创建一个重写规则

 rewrite ^(.*)$ $new_uri permanent;
下面是根据请求更新的配置示例

map $cookie_user_country $new_uri {
    default /en-en/;
    UAE /en-ae/;
    KW /en-kw/;
}


server {
        listen 8080;
        return 200 "$uri \n";
}

server {
        listen 8081;
        rewrite ^(.*)$ $new_uri permanent;
        return 200 "$cookie_user_country \n";
}
使用
$cookie\u NAME
指令获取单个cookie的正确值。
$http_VAR
包含特定http请求头的值

有关更多详细信息,请参阅我的卷曲请求

[root@localhost conf.d]# curl -v --cookie "user_country=KW; test=id; abcc=def" localhost:8081
* About to connect() to localhost port 8081 (#0)
*   Trying ::1...
* Connection refused
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 8081 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.29.0
> Host: localhost:8081
> Accept: */*
> Cookie: user_country=KW; test=id; abcc=def
>
< HTTP/1.1 301 Moved Permanently
< Server: nginx/1.17.6
< Date: Sun, 26 Apr 2020 12:34:15 GMT
< Content-Type: text/html
< Content-Length: 169
< Location: http://localhost:8081/en-kw/
< Connection: keep-alive
<
<html>
<head><title>301 Moved Permanently</title></head>
<body>
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.17.6</center>
</body>
</html>
* Connection #0 to host localhost left intact
这将列出nginx二进制文件中的所有“可打印”字符串,并通过“ngx_http_map_模块”对输出进行grep。结果应该如下所示:

[root@localhost conf.d]# strings `which nginx` | grep ngx_http_map_module | head -1

--> ngx_http_map_module

如果输出为ngx_http_map_模块的eq,则当前运行的NGINX二进制文件是使用map支持编译的。如果没有->请确保您使用的是使用映射支持编译的NGX二进制文件。

我将创建一个映射来处理和构造重定向URL。

这将把重写url设置为变量$new_uri。如果不存在cookie值,则默认值为/en/。现在您可以创建一个重写规则

 rewrite ^(.*)$ $new_uri permanent;
下面是根据请求更新的配置示例

map $cookie_user_country $new_uri {
    default /en-en/;
    UAE /en-ae/;
    KW /en-kw/;
}


server {
        listen 8080;
        return 200 "$uri \n";
}

server {
        listen 8081;
        rewrite ^(.*)$ $new_uri permanent;
        return 200 "$cookie_user_country \n";
}
使用
$cookie\u NAME
指令获取单个cookie的正确值。
$http_VAR
包含特定http请求头的值

有关更多详细信息,请参阅我的卷曲请求

[root@localhost conf.d]# curl -v --cookie "user_country=KW; test=id; abcc=def" localhost:8081
* About to connect() to localhost port 8081 (#0)
*   Trying ::1...
* Connection refused
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 8081 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.29.0
> Host: localhost:8081
> Accept: */*
> Cookie: user_country=KW; test=id; abcc=def
>
< HTTP/1.1 301 Moved Permanently
< Server: nginx/1.17.6
< Date: Sun, 26 Apr 2020 12:34:15 GMT
< Content-Type: text/html
< Content-Length: 169
< Location: http://localhost:8081/en-kw/
< Connection: keep-alive
<
<html>
<head><title>301 Moved Permanently</title></head>
<body>
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.17.6</center>
</body>
</html>
* Connection #0 to host localhost left intact
这将列出nginx二进制文件中的所有“可打印”字符串,并通过“ngx_http_map_模块”对输出进行grep。结果应该如下所示:

[root@localhost conf.d]# strings `which nginx` | grep ngx_http_map_module | head -1

--> ngx_http_map_module

如果输出为ngx_http_map_模块的eq,则当前运行的NGINX二进制文件是使用map支持编译的。如果不是->请确保您使用的是使用map支持编译的NGX二进制文件。

我说得对吗。您正在发送一个cookie和HTTP请求。如果我的答案不是使用正确的变量来获取cookie值,您可以共享有关cookie的更多详细信息。我将编辑我的答案。下面是$http_cookie值:
“\u fbp=fb.1.157973518938.1290691001;cto_bundle=xxxx;\u ga=xxx;…\u gid=GA1.3.378961375.1587548967;\uu stgeo=\x220\x22;visitorId=ad743657-3f13-44d9-83d2-cd15ade6b204;user_country=KW;…;user_country\u selection=KW”
Nginx版本为1.17,没有看到地图启用,我们需要nginx plus吗?只需再次检查以确定。NGINX开源中也提供了http_映射_模块。我不确定您使用的是NGINX的哪个二进制文件。为了确保您的NGINX版本是使用map模块构建的,请您为我运行
strings`which NGINX`| grep ngx_http|u map_模块| head-1
,并共享结果。
root@nginxweb-767474c9fc xdstg:/#strings`哪个nginx` | grep ngx_http_map_模块| head-1 ngx_http_map_模块
此确认是否已启用?你能不能看一看这些书,就像$http\u cookie\u USER\u COUNTRY帮不了我。我说的对吗。您正在发送一个cookie和HTTP请求。如果我的答案不是使用正确的变量来获取cookie值,您可以共享有关cookie的更多详细信息。我将编辑我的答案。下面是$http_cookie值:
“\u fbp=fb.1.157973518938.1290691001;cto_bundle=xxxx;\u ga=xxx;…\u gid=GA1.3.378961375.1587548967;\uu stgeo=\x220\x22;visitorId=ad743657-3f13-44d9-83d2-cd15ade6b204;user_country=KW;…;user_country\u selection=KW”
Nginx版本为1.17,没有看到地图启用,我们需要nginx plus吗?只需再次检查以确定。NGINX开源中也提供了http_映射_模块。我不确定您使用的是NGINX的哪个二进制文件。为了确保您的NGINX版本是使用map模块构建的,请您为我运行
strings`which NGINX`| grep ngx_http|u map_模块| head-1
,并共享结果。
root@nginxweb-767474c9fc xdstg:/#strings`哪个nginx` | grep ngx_http_map_模块| head-1 ngx_http_map_模块
此确认是否已启用?你能看一看吗,就像$http\u cookie\u USER\u COUNTRY没有帮我一样尝试过。