添加1,004字节
、 2019年5月11日 (六) 19:26
nginx通过cookie判断该指向那个环境。
* 首先创建两个项目huidu1、huidu2,分别创建index.html文件,内容分别为“huidu1”、“huidu2”
* nginx指向项目
<nowiki>
server {
listen 8086;
root /data/docker/www/huidu1;
}
server {
listen 8087;
root /data/docker/www/huidu2;
}
</nowiki>
* curl访问确认配置正确
<nowiki>
➜ curl http://127.0.0.1:8085/index.html
huidu1
➜ curl http://127.0.0.1:8086/index.html
huidu2
</nowiki>
* nginx配置cookie判断匹配server
<nowiki>
server {
listen 8085;
#匹配cookie
set $to "http://127.0.0.1:8086";# 默认huidu1
if ($http_cookie ~* "huidu=1"){
set $to "http://127.0.0.1:8087";# 匹配到cookie,更改为huidu2
}
location /{
proxy_pass $to;
break;
}
}
</nowiki>
* curl访问确认配置正确
<nowiki>
➜ curl http://127.0.0.1:8085/index.html
huidu1
➜ curl http://127.0.0.1:8085/index.html -H 'Cookie: huidu=1'
huidu2
</nowiki>
* 完成