Setup Nginx Reverse Proxy (IP Based Setup)¶
In this step, we configure Nginx to:
- Reverse proxy requests to TG-FileStream workers
- Load balance multiple worker instances
- Handle large file streaming properly
⚠ At this stage, we are NOT using a domain yet. We will configure using the server IP address.
1. Install Nginx¶
Enable and start:
2. Ensure Workers Are Running¶
Make sure these services are active:
sudo systemctl status tgfs
sudo systemctl status tgfs-worker@8081
sudo systemctl status tgfs-worker@8082
sudo systemctl status tgfs-worker@8083
Workers should be running on:
- 127.0.0.1:8080
- 127.0.0.1:8081
- 127.0.0.1:8082
- 127.0.0.1:8083
3. Create Nginx Configuration¶
Create a new site configuration:
Paste the following:
upstream aiohttp_backend {
least_conn;
keepalive 32;
server 127.0.0.1:8080 max_fails=3 fail_timeout=30s;
server 127.0.0.1:8081 max_fails=3 fail_timeout=30s;
server 127.0.0.1:8082 max_fails=3 fail_timeout=30s;
server 127.0.0.1:8083 max_fails=3 fail_timeout=30s;
}
server {
listen 80;
server_name _;
location / {
proxy_pass http://aiohttp_backend;
proxy_http_version 1.1;
proxy_buffering off;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
Explanation:
least_conn→ Sends traffic to worker with least active connectionskeepalive 32→ Keeps upstream connections aliveproxy_buffering off→ Prevents buffering large streams
4. Enable Site¶
Remove default site (recommended):
5. Test Configuration¶
If successful:
Reload Nginx:
6. Test Using Server IP¶
Find your server IP:
Now open in browser:
Or test:
If configured correctly, requests should be forwarded to your workers.
How Load Balancing Works Here¶
User ↓ Nginx (Port 80) ↓ aiohttp_backend (least_conn) ↓ Workers 8080–8083 ↓ Telegram
Nginx distributes traffic based on active connections.
Scaling Workers¶
To add more workers:
- Start new worker:
- Add to upstream block:
- Reload Nginx:
Troubleshooting¶
502 Bad Gateway¶
Check worker status:
Check logs: