If you run Nginx as a reverse proxy and recently enabled ConfigServer Security & Firewall (CSF), a 502 Bad Gateway usually means Nginx cannot reach your backend (like PHP-FPM or Apache). This guide walks you through the most common causes and fixes, from simple port checks to adjusting CSF’s firewall rules.
What You’ll Need
- Root or sudo access to your server
- SSH terminal (e.g., PuTTY, Terminal)
- Basic familiarity with command-line editing (nano or vi)
- Access to CSF’s configuration file (
/etc/csf/csf.conf)
Step-by-Step
- Confirm the 502 is Nginx-related. Open a browser and visit your site. If you see a generic “502 Bad Gateway” page, Nginx is running but cannot reach the upstream. Check Nginx’s error log:
tail -f /var/log/nginx/error.log– look for lines like “connect() failed (111: Connection refused)”. - Check if your backend is listening. Run
netstat -tulpn | grep :80(or the port your backend uses, e.g., 8080, 9000). If nothing appears, your backend (PHP-FPM, Apache, Node.js) is not running. Start it:systemctl start php-fpmorsystemctl start httpd(exact service name depends on your OS). - Verify the backend port is allowed in CSF. CSF by default blocks all ports except those listed in
TCP_INandTCP_OUT. If your backend listens on a non-standard port (e.g., 8080), it is blocked. Edit/etc/csf/csf.conf, findTCP_IN, and add the port (e.g.,8080). Save the file. - Restart CSF to apply changes. Run
csf -r(orcsf -rato also reload iptables). This flushes and re-applies the firewall rules. Then test your site again. - Check if CSF is blocking the loopback interface. Sometimes CSF’s
LOOPBACKsetting is set to0, which can block local traffic. Incsf.conf, ensureLOOPBACKis1. If not, set it to1and restart CSF. - Inspect Nginx’s upstream configuration. If your backend is on a Unix socket (like
unix:/var/run/php-fpm/www.sock), ensure the socket file exists and has correct permissions. Runls -la /var/run/php-fpm/– if the socket is missing, restart PHP-FPM. Also check that the Nginx user (oftenwww-data) has read/write access to the socket. - Test the backend directly. From the command line, run
curl -I http://127.0.0.1:8080(replace with your backend’s IP/port). If you get a response, the backend is fine. If not, the issue is with the backend itself, not the firewall. - Check for IP-based restrictions in CSF. CSF can block specific IPs or countries. Run
csf -gto see if your IP is blocked. If so, remove it withcsf -a(allow) orcsf -dr(deny remove). - Review Nginx’s proxy_pass directive. Ensure the
proxy_passURL matches the backend’s actual address. For example, if your backend is on port 8080, the directive should beproxy_pass http://127.0.0.1:8080;. A mismatch will cause 502s. - Restart Nginx after any config changes. Run
nginx -tto test the config, thensystemctl restart nginx.
Common Mistakes
- Forgetting to restart CSF after editing csf.conf. Changes only take effect after
csf -r. - Blocking the loopback interface. Setting
LOOPBACKto0can break internal communication between Nginx and its backend. - Using the wrong port. If your backend listens on 127.0.0.1:9000, but you open port 9000 in CSF’s TCP_IN, that’s fine, but if you open 9000 and your backend is on 8080, it won’t help.
- Editing the wrong configuration file. Some systems use
csf.confin/etc/csf/, but others may have it elsewhere. Always verify the path.
Tips
- Before making changes, back up your configuration:
cp /etc/csf/csf.conf /etc/csf/csf.conf.bak. - Use
csf -vto see the current firewall rules in a human-readable format. - If you are using a control panel like cPanel or DirectAdmin, they may have their own firewall integration. Check their documentation for CSF-specific settings.
- Consider adding your backend port to both
TCP_INandTCP_OUTif the backend makes outbound connections (e.g., to a database). - Monitor Nginx’s error log in real time:
tail -f /var/log/nginx/error.logwhile testing changes.
Troubleshooting
If the steps above do not resolve the issue, try the following:
- Check if SELinux or AppArmor is interfering. On CentOS/RHEL, SELinux can block Nginx from connecting to sockets. Temporarily set SELinux to permissive (
setenforce 0) and test. If that fixes it, adjust the SELinux policy. On Ubuntu/Debian, check AppArmor status withaa-status. - Look for other firewall rules. CSF may not be the only firewall. Check iptables directly:
iptables -L -n. If you see a DROP rule for your backend port, you may need to add an allow rule manually. - Test with CSF temporarily disabled. Run
csf -xto disable the firewall (not recommended for production, but for testing). If the site works, the issue is definitely CSF. Re-enable withcsf -e. - Check for IP conflicts. If your backend is on a different server, ensure that server’s firewall allows connections from your Nginx server’s IP. Also check that the backend’s IP is reachable from the Nginx server (ping it).
- Review Nginx’s error log for more details. Sometimes the log shows “upstream prematurely closed connection” which indicates a backend crash, not a firewall issue. In that case, focus on the backend’s own logs (e.g., PHP-FPM’s log).
Frequently Asked Questions
Why did CSF start blocking my backend after I enabled it?
CSF’s default policy is to deny all incoming connections except those explicitly allowed. If your backend port was not listed in TCP_IN, it gets blocked.
How do I open a port in CSF?
Edit /etc/csf/csf.conf, find TCP_IN (for incoming) and TCP_OUT (for outgoing), add the port number separated by commas, then run csf -r.
What is the loopback interface and why does it matter?
The loopback interface (127.0.0.1) is used for local communication between processes on the same server. If CSF blocks it, Nginx cannot reach PHP-FPM or other local services.
Can I allow all traffic from a specific IP in CSF?
Yes, use csf -a IP_ADDRESS to add the IP to the allow list. This bypasses all firewall rules for that IP.
What if my backend is on a Unix socket instead of a TCP port?
CSF does not block Unix sockets, so the issue is likely file permissions or the socket not being created. Check that the socket file exists and is writable by the Nginx user.
How do I know which port my backend uses?
Look at your Nginx configuration file (e.g., /etc/nginx/sites-available/default). The proxy_pass directive will show the address and port, or the socket path.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Valheim Server Not Showing in Community List Fixes
- DisplayPort vs HDMI: Which Cable Should You Use?
- Soundbar vs 2.1 Speaker System: Which Is Better?
- Survive Lava for Brainrot Tier List170 points
- MySQL Dump Import: Fix Common Errors
- Mouse Moving on Its Own? Causes & Fixes