Methods of payment Abuse

How to Get Nginx Errors Delivered Right to Telegram

Yesterday, 17:52

Let’s be honest — no server runs without errors forever. No matter how perfectly you configure it, sooner or later something will go wrong. Maybe a file won’t be found, a script will fail, or a resource becomes unavailable. The key is not to panic, but to catch the issue as early as possible so you can fix it right away. But who really wants to keep staring at logs in the terminal all day, searching for errors manually? Wouldn’t it be much nicer if your server could just send you a message on Telegram the moment something breaks?

Let me show you how to set that up in just 15–20 minutes.

Step 1: Create Your Own Telegram Bot

First, you’ll need a bot that will send you those notifications.

How to create a bot:
1. Open Telegram and search for @BotFather — he’s the official “father of all bots”.

2. Send the command /newbot and follow the instructions.

→ You’ll be asked to choose a name and username for your bot. It takes just a minute or two.

3. Once you’re done, you’ll get an API token — it’s a long string of letters and numbers.

→ Copy and save it somewhere safe — you’ll need it later.

Important:
After creating your bot, send it a message — something simple like /start. This step is required so Telegram knows you’ve “met” your bot and allows it to message you.

How to get your Chat ID:

1. Find another bot called @myidbot.

2. Send it the command /getid.

→ It will reply with your Chat ID. Save it — this tells your bot where to send notifications.

Step 2: Set Up the Error Notification Script

Now let’s make your server “talk” to your bot. We’ll write a simple Bash script that monitors your Nginx error log and sends new entries to Telegram.
Here’s the script you’ll need. Save it as check_nginx_errors.sh:

#!/bin/bash

BOT_TOKEN="yourtoken"
CHAT_ID="your_chat_id"
LOG_FILE="/var/log/nginx/error.log"
LAST_POSITION_FILE="/var/tmp/nginx_log_position"

if [ ! -f "$LAST_POSITION_FILE" ]; then
    echo "Position file not found. Creating a new one: $LAST_POSITION_FILE"
    echo "0" > "$LAST_POSITION_FILE"
fi

LAST_POSITION=$(cat "$LAST_POSITION_FILE" 2>/dev/null || echo "0")
NEW_LINES=$(tail -n +$((LAST_POSITION + 1)) "$LOG_FILE" 2>/dev/null)

if [ -n "$NEW_LINES" ]; then
    echo "$NEW_LINES" | while read -r LINE; do
        curl -s -X POST "https://api.telegram.org/bot$BOT_TOKEN/sendMessage" -d chat_id=$CHAT_ID -d text="$LINE"
    done
    wc -l < "$LOG_FILE" > "$LAST_POSITION_FILE"
else
    echo "No errors"
fi

Don’t forget to make it executable:

chmod +x check_nginx_errors.sh

Step 3: Automate the Script with Cron

So you don’t have to run the script manually, let’s set it to run every 5 minutes using cron.

How to do it:
1. Open your crontab for editing:

crontab -e

2. Add this line to run the script every 5 minutes:

 */5 * * * * /full/path/to/your/check_nginx_errors.sh

Important:
Make sure you provide the full path, or cron won’t find your script.

How to Test Your Setup

Let’s check if everything is working correctly.
Simulate an error by adding a fake log entry:

echo "$(date) [error] 12345#0: *1 open() \"/path/to/nonexistent/file\" failed (2: No such file or directory)" >> /var/log/nginx/error.log

After a few minutes, you should receive this message in Telegram.
If it arrives — congratulations, your setup works!

That’s It!

Now you’ll always know about the latest Nginx errors right in Telegram, no matter where you are. No more digging through server logs manually — your server will ping you when something goes wrong. And if you like, you can tweak the script further — add filters, improve the message format, or even send alerts to a team chat. It’s all up to you.