Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NGINX error: "Invalid port in upstream" when using proxy_pass with IPv6

Tags:

nginx

I have been unable to find an explanation for this NGINX error on nginx version: nginx/1.9.14.

This nginx.conf attempts to forward a client request to from webserver port 442 to port 9442.

When using an IPv4 client and server address everything works fine and the webserver request is forwarded to 9442. When using an IPv6 address client and server address the following error occurs:

2017/08/21 19:05:56 [error] 6694#0: *5 invalid port in upstream "2000::157:9442/", client: 2000::158, server: , request: "GET / HTTP/1.1",   host: "[2000::157]:442"

nginx.conf:

http {
    server {
        listen       442 ssl; # IPv4 support
        listen       [::]:442 ssl; # IPv6 support

        ssl_certificate      /etc/ssl/active.crt;
        ssl_certificate_key  /etc/ssl/active.key;

        ssl on;
        ssl_session_cache builtin:1000 shared:SSL:10m;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_prefer_server_ciphers on;

        location / {
            proxy_pass https://$server_addr:9442$request_uri;
        }
    }
}

What does "invalid port in upstream" mean exactly?

Note that the IPv6 addresses in this example are made up for the sake of this example and are on a private network not seen by the outside world.

like image 207
Chadness3 Avatar asked Nov 23 '25 22:11

Chadness3


1 Answers

So the issue is because of the IP formats. $sever_addr will get 127.0.0.1 or any local ip for IPv4. While for IPv6 it would get ::1 or 2000::157 like the one you got. Or something which will have : in the address.

Now when you proxy_pass using $server_addr, it become either http://127.0.0.1:9442 or http://::1:9442. IPv4 one is valid but the IPv6 one is not valid. It needs to be http://[::1]:9442. The fix is easy, we use a map in our http block.

map $server_addr $proxy_pass_ip {
    default "$server_addr";
    ~.*:.*   "[$server_addr]";
}

And we change our location block like

location / {
   proxy_pass https://$proxy_pass_ip:9442$request_uri;
}
like image 106
Tarun Lalwani Avatar answered Nov 25 '25 13:11

Tarun Lalwani