VyOS nginx service to prevent “failed”

The “Failed” message appears because the default nginx systemd service is trying to start before the configuration is restored. Here’s how to fix it:

Solution: Disable the Default Nginx Service

Since your restore script handles starting nginx, disable the default nginx service:

# Disable the default nginx service
sudo systemctl disable nginx

# Stop it if running
sudo systemctl stop nginx

Update Your Restore Script

Modify /config/scripts/vyos-postconfig-bootup.script to handle nginx properly:

sudo nano /config/scripts/vyos-postconfig-bootup.script

Replace with this improved version:

#!/bin/bash

# Wait for system to be fully ready
sleep 15

# Install nginx if not present
if ! command -v nginx &> /dev/null; then
    apt update
    apt install -y nginx
fi

# Disable the default nginx service to prevent "failed" messages
systemctl disable nginx 2>/dev/null

# Restore nginx configuration from backup
if [ -f /config/scripts/nginx-reverse-proxy ]; then
    mkdir -p /etc/nginx/sites-available /etc/nginx/sites-enabled
    cp /config/scripts/nginx-reverse-proxy /etc/nginx/sites-available/reverse-proxy
    rm -f /etc/nginx/sites-enabled/default
    ln -sf /etc/nginx/sites-available/reverse-proxy /etc/nginx/sites-enabled/reverse-proxy
fi

# Test configuration
nginx -t

# Start nginx manually (not via systemd)
if pgrep nginx > /dev/null; then
    nginx -s reload
else
    nginx
fi

Make it executable:

sudo chmod +x /config/scripts/vyos-postconfig-bootup.script

Alternative: Fix the Systemd Service Ordering

If you prefer using systemd, modify the nginx service to start after VyOS is ready:

sudo systemctl edit nginx

Add this override:

[Unit]
After=vyos-router.service network-online.target
Wants=network-online.target

[Service]
# Wait a bit before starting
ExecStartPre=/bin/sleep 10

Save and exit (Ctrl+X, Y, Enter).

Then backup this override:

sudo mkdir -p /config/scripts/systemd-override
sudo cp /etc/systemd/system/nginx.service.d/override.conf /config/scripts/systemd-override/

Update your boot script to restore this override:

sudo nano /config/scripts/vyos-postconfig-bootup.script
#!/bin/bash

# Wait for system to be ready
sleep 10

# Install nginx if not present
if ! command -v nginx &> /dev/null; then
    apt update
    apt install -y nginx
fi

# Restore systemd override
if [ -f /config/scripts/systemd-override/override.conf ]; then
    mkdir -p /etc/systemd/system/nginx.service.d
    cp /config/scripts/systemd-override/override.conf /etc/systemd/system/nginx.service.d/
    systemctl daemon-reload
fi

# Restore nginx configuration
if [ -f /config/scripts/nginx-reverse-proxy ]; then
    mkdir -p /etc/nginx/sites-available /etc/nginx/sites-enabled
    cp /config/scripts/nginx-reverse-proxy /etc/nginx/sites-available/reverse-proxy
    rm -f /etc/nginx/sites-enabled/default
    ln -sf /etc/nginx/sites-available/reverse-proxy /etc/nginx/sites-enabled/reverse-proxy
fi

# Enable and restart nginx
systemctl enable nginx
systemctl restart nginx

Recommended: Run Nginx as a Standalone Process (No Systemd)

This is the cleanest solution – run nginx directly without systemd:

sudo nano /config/scripts/vyos-postconfig-bootup.script
#!/bin/bash

# Wait for system to be ready
sleep 15

# Install nginx if not present
if ! command -v nginx &> /dev/null; then
    apt update
    apt install -y nginx
fi

# Completely disable systemd service
systemctl disable nginx 2>/dev/null
systemctl stop nginx 2>/dev/null

# Restore nginx configuration
if [ -f /config/scripts/nginx-reverse-proxy ]; then
    mkdir -p /etc/nginx/sites-available /etc/nginx/sites-enabled
    cp /config/scripts/nginx-reverse-proxy /etc/nginx/sites-available/reverse-proxy
    rm -f /etc/nginx/sites-enabled/default
    ln -sf /etc/nginx/sites-available/reverse-proxy /etc/nginx/sites-enabled/reverse-proxy
fi

# Kill any existing nginx processes
pkill nginx 2>/dev/null

# Start nginx as standalone process
/usr/sbin/nginx

# Add to crontab for persistence check
(crontab -l 2>/dev/null | grep -v nginx; echo "*/5 * * * * pgrep nginx > /dev/null || /usr/sbin/nginx") | crontab -

Make it executable:

sudo chmod +x /config/scripts/vyos-postconfig-bootup.script

Test the Solution

# Reboot
sudo reboot

After reboot, check:

# Should show no failed services related to nginx
systemctl --failed

# Check if nginx is running
ps aux | grep nginx

# Test your reverse proxy
curl -H "Host: term11.beenplus.com" http://192.168.99.1

The recommended approach is running nginx as a standalone process (last option), which avoids systemd altogether and prevents any “Failed” messages while keeping nginx functional.

Leave a Reply