Fix Nginx proxy blocked by SELinux on Rocky Linux 9
Tell whether a 502 and a Permission denied in error.log come from SELinux, fix it with a boolean, and label a custom document root.
If Nginx is configured correctly but the proxy returns 502, suspect SELinux. This assumes the app is listening normally on 127.0.0.1:8080. For the proxy configuration itself, see the Ubuntu 24.04 reverse proxy guide.
[1] Confirm the symptom. The browser shows 502 Bad Gateway, and /var/log/nginx/error.log contains this message.
# tail -n 5 /var/log/nginx/error.logconnect() to 127.0.0.1:8080 failed (13: Permission denied) while connecting to upstream[2] Check the SELinux mode. In Enforcing mode, policy violations are actually blocked.
# getenforce[3] Find the denial (AVC). This is the command the official documentation uses. If nothing is returned, check that auditd is running, repeat the blocked action, and search again.
# ausearch -m AVC,USER_AVC,SELINUX_ERR,USER_SELINUX_ERR -ts recent[4] Turn on the httpd_can_network_connect boolean. -P keeps it across reboots.
# setsebool -P httpd_can_network_connect 1# getsebool httpd_can_network_connect[5] If the document root is not in a default location such as /srv/site, wrong file labels can cause 403. Give the new path the same rule as /var/www. The official documentation shows -e to make a path equivalent to a default one.
# semanage fcontext -a -e /var/www /srv/site# restorecon -Rv /srv/site[6] Verify the fix. Send the same request again and check that no new (13: Permission denied) appears in error.log.
# curl -sI http://127.0.0.1/ | head -n 1Analyze your Nginx log in the browser ↗ — nothing is uploaded.