How to dynamically assign port to proxy_pass

Posted April 19, 2020 2.2k views
NginxLoad Balancing

Hi everybody,
I used two streaming’s servers which host many webradios streams (over 25). Each stream use a port between 8000 and 9000.

I have 3 servers :
-> nginx load-balancer and reverse-proxy
-> 2 streaming’s server (webradios) on ports between 3000 and 4000 .

I use Nginx to :

  • allow HTTPS for all webradios

  • change server on failure (ex.: server down). (high-availability / load-balance)

I’m trying to do something like this :

  1. When user listen a stream on , I want to transparently (proxify) send the request on one of the two Icecast’s server.

  2. If HTTPS and Icecast1 is alive then send the request to Icecast1.

  3. If HTTPS and Icecast1 is down then send the request to Icecast2.

This is the current configuration of Nginx:

#Streaming's cluster :
upstream backend {
    server ;
    server ;

#SSL for all
server {

        listen 8000-9000 ssl ;

        access_log /var/log/nginx/reverse-ssl-access.log;
        error_log /var/log/nginx/reverse-ssl-error.log;

        # ssl on;
        ssl_protocols TLSv1.2 TLSv1.1 TLSv1 ;
        ssl_certificate /etc/letsencrypt/live/;
        ssl_certificate_key /etc/letsencrypt/live/;

        location / {
               proxy_pass http://backend:$server_port;



I try to do something like this : “http://backend:$server_port”.

In the web browser I’ve the error : 502 Bad gateway

In the errors logs on the server : *1 no resolver defined to resolve backend .

Can you help me ?

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Submit an Answer
1 answer

I’ve been debugging this issue lately. Have you got the solution yet?