我使用 Nginx 的 map 指令根据自定义 http 标头的值将用户路由到稳定服务器或测试服务器。但是,即使我已确认标头为“TEST”,Nginx 也只会匹配默认值。
worker_processes 2;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" "$http_x_cp_client_class" $upstream';
access_log logs/access.log main;
upstream dataserver_stable {
server localhost:8081;
}
upstream dataserver_beta {
server localhost:8082 fail_timeout=1m;
}
map $http_x-cp-client-class $upstream {
TEST "dataserver_beta";
# STABLE "dataserver_stable"; technically redundant I think
default "dataserver_stable";
}
server {
listen 81;
server_name localhost;
location / {
proxy_set_header Host $host;
add_header My-Upstream $upstream;
proxy_pass http://$upstream;
proxy_connect_timeout 5s;
proxy_read_timeout 10s;
}
}
}
标头:
要求:
GET /pages/widgets/calendar/tradeDateCalendar.tpl.html HTTP/1.1
Host: ny7wlpmurray:81
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
Accept: application/json, text/plain, */*
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36
X-CP-Client-Class: TEST
Referer: http://ny7wlpmurray:81/pages/dashboard/dashboard.html
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.9
Cookie: _ga=GA1.1.1629631386.1511812518; _gid=GA1.1.892280347.1511812518
答案:
HTTP/1.1 200
Server: nginx/1.12.2
Date: Wed, 29 Nov 2017 21:33:54 GMT
Content-Type: text/html
Content-Length: 594
Connection: keep-alive
Accept-Ranges: bytes
ETag: W/"594-1511969617175"
Last-Modified: Wed, 29 Nov 2017 15:33:37 GMT
My-Upstream: dataserver_stable
我写错了吗?我试过 TEST、~TEST、"TEST" 和 "~TEST",但都没用。
答案1
在将 HTTP 请求头转换为变量名时,nginx 会将连字符转换为下划线。因此,代表标题的变量X-CP-Client-Class
将是$http_x_cp_client_class
。
这表明您的map
声明应如下:
map $http_x_cp_client_class $upstream {