如何根据终端用户喜欢的cookie进行路由?我们有Nginx1.17.10在AKS中以pod的形式运行,电子商务网站就托管在上面。CloudFlare是前端的DNS和WAF。CloudFlare开启了GeoIP,所以我们有参数--$http_cf_ipcountry来追踪国家代码。但是我们正在寻找最终用户保存的偏好,并路由到该特定区域。
例子:Http_cookie参数也是持有的。
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, COUNTRY_CODE_PREV, CURRENYCY_CODE , EXCHANGE_RATE。
应该用什么方法来处理这个要求呢?谁能帮我解决一下,谢谢!
我将创建一个地图来处理构建重定向URLs。http:/nginx.orgendocshttpngx_http_map_module.html#map。
这将把重写的url设置为一个变量$new_uri。如果没有cookie值,默认为en-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_NAME
指令来获取单个cookie的正确值。该 $http_VAR
包含一个特定的HTTP请求头的值。
更多细节请参见我的curl请求。
[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
类型
strings `which nginx` | grep ngx_http_map_module | head -1
这将列出nginx二进制中所有 "可打印 "的字符串,并通过 "ngx_http_map_module "来获取输出结果。结果应该是这样的。
[root@localhost conf.d]# strings `which nginx` | grep ngx_http_map_module | head -1
--> ngx_http_map_module
如果输出结果为ngx_http_map_module,则说明当前运行的nginx二进制文件已被编译为支持map的文件。如果不是 -> 确保你使用的是一个支持map编译的NGX二进制文件。