How to setting Nginx proxy_pass and forward query parameters

August 5, 2016 1 Min

Let’s see basic config of proxy_pass.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
upstream web {
    server 10.10.10.10;
    server 10.10.10.20;
}

server{
    listen 80;
    server_name
    location / {
        proxy_pass http://web/;
    }
}

Well, this setting is work and look like perfect, but it will get some problem, it’s not forward any http get query string and just forward to web root, so we need to change some thing, just follow below.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
upstream web {
    server 10.10.10.10;
    server 10.10.10.20;
}

server{
    listen 80;
    server_name
    location / {
        proxy_pass http://web$uri;
    }
}

Now we can do more, forward X-FORWARD-FOR to web server, just 3 lines.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
upstream web {
    server 10.10.10.10;
    server 10.10.10.20;
}

server{
    listen 80;
    server_name
    location / {
        proxy_pass http://web$uri;

        proxy_set_header HOST $host;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

[email protected]

Remove excess parameter at proxy_pass, thanks to Ricardo Villaf√°fila Robles.