# Transport not connecting when nginx loadbalancer used

**URL:** <https://mediasoup.discourse.group/t/transport-not-connecting-when-nginx-loadbalancer-used/6372>\
**Category:** Deployment & Scalability\
**Created:** [December 3, 2024, 6:51am UTC](https://mediasoup.discourse.group/t/transport-not-connecting-when-nginx-loadbalancer-used/6372 "2024-12-03T06:51:53Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![magnimuslaertius](https://yyz2.discourse-cdn.com/free1/user_avatar/mediasoup.discourse.group/magnimuslaertius/32/2711_2.png) [@magnimuslaertius](https://mediasoup.discourse.group/u/magnimuslaertius)\
**Post date:** [December 3, 2024, 6:51am UTC](https://mediasoup.discourse.group/t/transport-not-connecting-when-nginx-loadbalancer-used/6372/1 "2024-12-03T06:51:53Z")

</div>

Hello everyone, I apologize for asking this question but I’ve been stuck over this for many days.

Basically, I’m trying to deploy my application on AWS EC2 instance. I am using docker-compose and nginx for creating multiple replicas of the same server to balance the load between.

And everything is working fine (signalling and stuff) but my webrtc transport are not conneting when I’m using the load balancer. It works when I deploy the same project with only one replica but as soon as I create multiple versions of it, the transport doesn’t connect anymore.

And I am pretty sure it has something to do with rtcMin and rtcMax ports of mediasoup worker not being exposed properly but I can’t figure out a way to do it

Following are my relevant files

docker-compose.yml

```auto
version: '3'
services:
  nginx:
    build: ./nginx
    ports:
      - "8000:8000"
    depends_on:
      - backend
    networks:
      - my_network

  backend:
    build:
      context: ~/Live-streaming/live-streaming-v2
    hostname: server1
    ports:
      - "8000"
      - "10000-10020/udp"
    networks:
      - my_network
    deploy:
      replicas: 3
      update_config:
        parallelism: 1
        delay: 10s
    volumes:
      - /usr/bin:/usr/bin
      - /usr/lib/x86_64-linux-gnu:/usr/lib/x86_64-linux-gnu
      - /usr/share:/usr/share
    environment:
      - RTC_MIN_PORT=10000
      - RTC_MAX_PORT=10020

networks:
  my_network:
    driver: bridge

```

nginx configurations

```auto
upstream backend {
    ip_hash;
    server backend:8000;
}

server {
    listen 8000;

    # Handle general HTTP traffic with load balancing
    location / {
        proxy_pass http://backend;
        proxy_connect_timeout 5s;
        proxy_read_timeout 10s;
        proxy_send_timeout 10s;
        error_log /var/log/nginx/error.log;
        access_log /var/log/nginx/access.log;

        # Forward client IP address to the backend
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
    }

    # Handle WebSocket connections for Socket.IO
    location /socket.io/ {
        proxy_pass http://backend; # Same upstream as regular traffic
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
        proxy_set_header Host $host;

        # Optional timeout adjustments for WebSocket
        proxy_read_timeout 60s;
        proxy_send_timeout 60s;

        # Log for debugging WebSocket issues
        error_log /var/log/nginx/socketio_error.log;
        access_log /var/log/nginx/socketio_access.log;
    }
   }

```

Dockerfile

```auto
FROM node:18
WORKDIR /app
COPY package.json .
RUN npm install
COPY . .
EXPOSE 8000
CMD ["npm", "start"]

```

Thanks a lot for taking your time to read this

---

<div class="post-metadata">

**Author:** ![miroslavpejic85](https://yyz2.discourse-cdn.com/free1/user_avatar/mediasoup.discourse.group/miroslavpejic85/32/1044_2.png) [@miroslavpejic85](https://mediasoup.discourse.group/u/miroslavpejic85)\
**Post date:** [December 3, 2024, 7:53am UTC](https://mediasoup.discourse.group/t/transport-not-connecting-when-nginx-loadbalancer-used/6372/2 "2024-12-03T07:53:37Z")

</div>

Maybe you got **Port Range Conflicts** as docker compose doesn’t natively handle port ranges for replicas. When you define 10000-10020/udp, each replica might try to bind to the same host ports, resulting in conflicts.

Not sure if solve but you can try using Docker’s **host networking mode** for test.

```yaml
backend:
  build:
    context: ~/Live-streaming/live-streaming-v2
  hostname: server1
  network_mode: host
  environment:
    - RTC_MIN_PORT=10000
    - RTC_MAX_PORT=10020

```

This ensures that the container uses the host’s network stack directly, avoiding port conflicts.

---

<div class="post-metadata">

**Author:** ![magnimuslaertius](https://yyz2.discourse-cdn.com/free1/user_avatar/mediasoup.discourse.group/magnimuslaertius/32/2711_2.png) [@magnimuslaertius](https://mediasoup.discourse.group/u/magnimuslaertius)\
**Post date:** [December 3, 2024, 3:48pm UTC](https://mediasoup.discourse.group/t/transport-not-connecting-when-nginx-loadbalancer-used/6372/3 "2024-12-03T15:48:01Z")

</div>

No the ports are being binded properly without conflict

 ![image](https://global.discourse-cdn.com/free1/uploads/mediasoup/original/2X/7/7b9955879b01499f8df01a865a4feafb057f64b4.png)

---

<div class="post-metadata">

**Author:** ![miroslavpejic85](https://yyz2.discourse-cdn.com/free1/user_avatar/mediasoup.discourse.group/miroslavpejic85/32/1044_2.png) [@miroslavpejic85](https://mediasoup.discourse.group/u/miroslavpejic85)\
**Post date:** [December 3, 2024, 8:31pm UTC](https://mediasoup.discourse.group/t/transport-not-connecting-when-nginx-loadbalancer-used/6372/4 "2024-12-03T20:31:14Z")

</div>

```auto
- "10000-10020:10000-10020/udp"

```

---

<div class="post-metadata">

**Author:** ![magnimuslaertius](https://yyz2.discourse-cdn.com/free1/user_avatar/mediasoup.discourse.group/magnimuslaertius/32/2711_2.png) [@magnimuslaertius](https://mediasoup.discourse.group/u/magnimuslaertius)\
**Post date:** [December 4, 2024, 5:53am UTC](https://mediasoup.discourse.group/t/transport-not-connecting-when-nginx-loadbalancer-used/6372/5 "2024-12-04T05:53:52Z")

</div>

If I bind the ports of my replicas to specific ports of the host then I can’t deploy multiple replicas of the same application which then defeats the purpose of loadbalancing. Plus it doesn’t work even after I deploy a single container with its ports binded to specific ports of the host like you suggested

---

<div class="post-metadata">

**Author:** ![miroslavpejic85](https://yyz2.discourse-cdn.com/free1/user_avatar/mediasoup.discourse.group/miroslavpejic85/32/1044_2.png) [@miroslavpejic85](https://mediasoup.discourse.group/u/miroslavpejic85)\
**Post date:** [December 4, 2024, 7:32am UTC](https://mediasoup.discourse.group/t/transport-not-connecting-when-nginx-loadbalancer-used/6372/6 "2024-12-04T07:32:15Z")

</div>

Try testing it in a single container as follows:

### Mapping TCP and UDP Ports for WebRTCTransport.

Ensure both TCP and UDP traffic for WebRTC media streams can pass through by mapping the ports as follows:

```bash
- "10000-10020:10000-10020/udp"
- "10000-10020:10000-10020/tcp"

```

### Configuring Amazon EC2 for WebRTC with Elastic IP

1. **Set Inbound Rules for Security Groups**  
Configure the following inbound rules in your AWS Security Group:

2. **Assign a Static Public IP (Elastic IP) to Your EC2 Instance**  
To prevent the IP address from changing upon instance reboot, assign an Elastic IP:

3. **Update `config.js` with Elastic IP and Restart the Instance**
