Skip to content

Setup Nginx Reverse Proxy (IP-Based Setup)

In this step, we configure Nginx to:

  • Reverse proxy requests to TG-FileStream
  • Handle HTTP traffic
  • Properly stream large files

⚠ At this stage, we are NOT using a domain. We will configure Nginx using the server IP address.


1. Install Nginx

sudo apt update
sudo apt install nginx -y

Enable and start Nginx:

sudo systemctl enable nginx
sudo systemctl start nginx

2. Ensure TGFS Service Is Running

Make sure the main service is active:

sudo systemctl status tgfs

The application should be running on:

  • <Bind Address>:<PORT>

Example

127.0.0.1:8080

3. Create Nginx Configuration

Create a new site configuration:

sudo nano /etc/nginx/sites-available/tgfs

Paste:

upstream aiohttp_backend {
    least_conn;
    keepalive 32;

    server <Bind Address>:<PORT> 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;
    }
}

Adjust:

  • Replace <Bind Address> with the HOST value from your .env.
  • Replace <PORT> with the PORT value from your .env.

Example
upstream aiohttp_backend {
    least_conn;
    keepalive 32;

    server 127.0.0.1:8080 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;
    }
}

4. Enable Site

sudo ln -s /etc/nginx/sites-available/tgfs /etc/nginx/sites-enabled/

Remove the default site (recommended):

sudo rm /etc/nginx/sites-enabled/default

5. Test Configuration

sudo nginx -t

If successful:

syntax is ok
test is successful

Reload Nginx:

sudo systemctl reload nginx

6. Test Using Server IP

Get your server IP:

curl ifconfig.me

Open in browser:

http://<your-server-ip>

Or test using curl:

curl http://<your-server-ip>

If configured correctly, requests should be forwarded to TGFS.


Troubleshooting

502 Bad Gateway

Check TGFS status:

sudo systemctl status tgfs

Check logs:

sudo journalctl -u tgfs -f

Nginx Errors

sudo nginx -t
sudo journalctl -u nginx -f