A minor problem with superset nginx reverse proxy configuration

Keywords: Nginx curl Python PHP

When using nginx to configure superset reverse proxy and map to distribute through cookie s, we encounter very strange problems. When visiting the home page, we are always redirected to the domain name of upstream with the same name.

    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;
    }
}

give the result as follows

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 // / Here is the redirected header

For a time, I thought that the latest version of nginx had bug s in the definitions of map and upstream, but it seemed impossible to think about it. Then I looked at the result performance and the general behavior habits of programmers (superset code is very complicated, plus python, a flexible scripting language addition, I really don't want to flip through its code). It should be the agent that reads the host header, then locates, and then sees this sentence in the nginx proxy document.

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;

The puzzle was solved.

Adding this to the proxy configuration allows you to pass the original Host to 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

More architecture, PHP, GO-related pit-stepping techniques, please pay attention to my public number

Posted by Zoofu on Thu, 03 Oct 2019 20:20:33 -0700