Search code examples
nginxreverse-proxy

Nginx proxy pass directive: Invalid port in upstream error


I am doing load balancing with Nginx. Here is my config

upstream web_backend {
        least_conn;
        server localhost:8001  max_fails=3 fail_timeout=60s;
        server localhost:8002  max_fails=3 fail_timeout=60s;
}


server {
    listen 8545;

    server_name _;

    location / {
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_pass http://web_backend;
    }
}


server {
        listen 8001;
        server_name localhost;
        location / {
        proxy_pass https://some_other_url/v3/cxnmcdinwrtyf93vcwdiyfx8q6xqwxv9qg7c93fgcb;

        }
}

server {
        listen 8002;
        server_name localhost;
        location / {
        proxy_pass 'https://chipdunk-dude:gorgeous-serpents-clubbed-orphans@nd-657-555-555-777.dogify.com';
        }
}

as you can see the url at port 8002 is weird (dont even know what this kind of urls are called) because it has ":" in the url, Nginx gives me this error

nginx: [emerg] invalid port in upstream "chipdunk-dude:gorgeous-serpents-clubbed-orphans@nd-657-555-555-777.dogify.com" in /etc/nginx/sites-enabled/default:60

The url at port 8001 works fine.


Solution

  • Everything before the @ is userinfo which should be encoded by the browser and included as a separate request header according to RFC 7617.

    Nginx is not a browser and cannot do it for you.

    You could probably convert that into Base64 and use a proxy_set_header to set the Authorization header.

    For example:

    proxy_set_header Authorization "Basic Y2hpcGR1bmstZHVkZTpnb3JnZW91cy1zZXJwZW50cy1jbHViYmVkLW9ycGhhbnM=";
    proxy_pass https://nd-657...;