在用 nginx 配置 superset 反向代理,而且使用 map 經過 cookie 分流的時候,遇到十分詭異的問題,訪問主頁的時候老是被重定向到 upstream 同名的域名html
upstream release { server 127.0.0.1:8088 weight=1 max_fails=1 fail_timeout=30s; } upstream development { server 127.0.0.1:8089 weight=1 max_fails=1 fail_timeout=30s; } map $COOKIE_version $env { default release; release release; development development; } server { listen 10001; server_name localhost; location / { proxy_pass http://$env; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_http_version 1.1; proxy_connect_timeout 600; proxy_send_timeout 600; proxy_read_timeout 600; send_timeout 600; proxy_set_header X-Forwarded-For $remote_addr; } }
結果以下python
curl -I http://127.0.0.1:10001/ HTTP/1.1 302 FOUND Server: nginx/1.17.1 Date: Thu, 15 Aug 2019 07:54:44 GMT Content-Type: text/html; charset=utf-8 Content-Length: 241 Connection: keep-alive Location: http://release/superset/welcome // 這裏是重定向的 header
搞的我一度覺得是否是最新版的 nginx 在 map 和 upstream 的定義上有 bug,可是想了想彷佛不可能,而後從結果表現和程序員廣泛的行爲習慣分析來看(superset代碼巨複雜,加上 python 這靈活腳本語言加成,真的不想翻它的代碼),應該是代理裏讀取了 host 這個 header,而後 location,而後看到 nginx proxy 文檔裏有這句話nginx
Allows redefining or appending fields to the request header passed to the proxied server. The value can contain text, variables, and their combinations. These directives are inherited from the previous level if and only if there are no proxy_set_header directives defined on the current level. By default, only two fields are redefined:程序員
proxy_set_header Host $proxy_host; proxy_set_header Connection close;
謎題解開了。cookie
代理配置加上這個,就能夠把本來的 Host 傳給 superset架構
proxy_set_header Host $http_host;
curl -I http://127.0.0.1:10001/ HTTP/1.1 302 FOUND Server: nginx/1.17.1 Date: Thu, 15 Aug 2019 08:06:47 GMT Content-Type: text/html; charset=utf-8 Content-Length: 241 Connection: keep-alive Location: http://127.0.0.1:10001/superset/welcome
更多架構、PHP、GO相關踩坑實踐技巧請關注個人公衆號 app