Sporadic timeouts in Nginx reverse proxy when connecting to Telegram Bot API

I’m having trouble with my Nginx setup as a reverse proxy for the Telegram Bot API. It works fine when I only use my local Bot API server, but I get random timeouts when I add the Telegram server to the mix.

Here’s a simplified version of my config:

upstream telegram_proxy {
    server local_server:443;
    server telegram_api:443;
}

server {
    listen 80;
    server_name my_ip;

    location /bot {
        proxy_pass https://telegram_proxy;
        proxy_set_header Host $host;
        proxy_ssl_name telegram_api;
        proxy_ssl_protocols TLSv1.2 TLSv1.3;
        proxy_connect_timeout 60s;
        proxy_read_timeout 120s;
        proxy_send_timeout 60s;
    }
}

The error log shows:

[error] upstream timed out while connecting to upstream, request: "POST /bot123/getMe HTTP/1.1", upstream: "https://149.154.167.220:443/bot123/getMe"

This only happens when the Telegram API is in the upstream block. I’ve checked my network and the local server works fine. Any ideas on why this is happening and how to fix it? Thanks!

I’ve encountered similar issues with Nginx reverse proxying to external APIs. The sporadic timeouts you’re experiencing could be due to network latency or temporary connectivity issues between your server and Telegram’s API. One approach that might help is implementing a health check for your upstream servers. This way, Nginx can detect when the Telegram API is unresponsive and route traffic accordingly. You could also try increasing your timeout values slightly, though this is more of a band-aid solution. Another thing to consider is rate limiting. Telegram has strict rate limits, and if you’re hitting them, it could cause timeouts. Implementing a rate limiting strategy on your end might help mitigate this. Lastly, ensure your SSL certificates are up-to-date and properly configured. Sometimes, SSL handshake issues can manifest as connection timeouts. Hope this helps point you in the right direction for troubleshooting!

I’ve dealt with similar timeout issues when setting up Nginx as a reverse proxy for external APIs. One thing that’s worked well for me is implementing retries and backoff strategies. You could try adding something like this to your location block:

proxy_next_upstream error timeout http_500 http_502 http_503 http_504;
proxy_next_upstream_tries 3;
proxy_next_upstream_timeout 10s;

This tells Nginx to retry the request on another upstream server if it encounters certain errors or timeouts. It’ll attempt up to 3 retries within a 10-second window.

Also, consider adding some logging to track these timeouts more closely. You can use the $upstream_response_time variable in your log format to see how long each request is taking. This might help you identify patterns or specific requests that are causing issues.

Remember, external APIs can be unpredictable, so building in some resilience is key. Hope this helps!

hey man, i’ve seen this before. it’s prolly due to network issues or telegram’s rate limits. try adding a health check to ur upstream config. smthn like:

upstream telegram_proxy {
server local_server:443;
server telegram_api:443 max_fails=3 fail_timeout=30s;
}

this might help nginx handle those timeouts better. good luck!