How to Fix Nginx Connection Refused on Debian 11


The Root Cause On Debian 11, “Nginx Connection Refused” typically indicates that either the Nginx service is not running or a firewall (like UFW) is actively blocking inbound connections to the Nginx listening ports. This often occurs after a system reboot, a configuration error preventing Nginx from starting, or an unconfigured firewall rule.

Quick Fix (CLI) First, verify Nginx service status and restart if necessary:

sudo systemctl status nginx
sudo systemctl start nginx
sudo systemctl enable nginx

Next, ensure the firewall permits Nginx traffic. If UFW is active:

sudo ufw allow 'Nginx HTTP'
sudo ufw allow 'Nginx HTTPS'
sudo ufw reload

Configuration Check The primary configuration file to inspect is /etc/nginx/nginx.conf, or more commonly, specific site configuration files located in /etc/nginx/sites-available/ (e.g., /etc/nginx/sites-available/default). Within the server block, confirm the listen directives are correctly defined for the desired ports (e.g., 80 for HTTP, 443 for HTTPS).

# Example from /etc/nginx/sites-available/default
server {
    listen 80;
    listen [::]:80; # Ensure IPv6 listening is also configured if needed
    # server_name your_domain.com; # Ensure this matches your expected domain or IP
    # ... other directives
}

After making changes, always test the Nginx configuration syntax and reload the service:

sudo nginx -t
sudo systemctl reload nginx

Verification Execute a curl command from the server itself to confirm Nginx is responding:

curl -v http://localhost

Alternatively, check if Nginx is actively listening on the expected ports:

sudo ss -tuln | grep -E '80|443'

Look for LISTEN status entries associated with Nginx on ports 80 or 443.