nginx 502 and 403 on RHEL with correct permissions: SELinux AVC denials, not chmod
After moving our app to RHEL 9, nginx returned 502 for the proxied API and 403 for static files even though Unix permissions were fine. Both were SELinux denials. We read the AVC records, set one boolean, relabelled one directory, and left enforcing mode on.
We migrated a Node.js API and its static front end from an Ubuntu VM to a new RHEL 9 VM. Same nginx configuration, same app, same file layout. On the first smoke test the API returned 502 Bad Gateway and the front end returned 403 Forbidden.
nginx's error log was clear about the symptom but not the cause:
2026/12/29 10:14:02 [crit] 2141#2141: *7 connect() to 127.0.0.1:3000 failed (13: Permission denied) while connecting to upstream, client: 198.51.100.40, server: app.example.com, request: "GET /api/health HTTP/1.1", upstream: "http://127.0.0.1:3000/api/health"
2026/12/29 10:14:09 [error] 2141#2141: *9 open() "/srv/app/public/index.html" failed (13: Permission denied), client: 198.51.100.40, server: app.example.com, request: "GET / HTTP/1.1"
Error 13 is EACCES. Connecting to a local TCP port does not involve file permissions at all, which was the first hint that something other than Unix permissions was involved.
Reproducing it
Every request reproduced both errors. curl http://127.0.0.1:3000/api/health from a shell on the same host worked, so the app itself was up.
What we ruled out
File permissions.
namei -l /srv/app/public/index.htmlshowed every directory world-executable and the file world-readable; nginx'snginxuser could read it in a plain Unix sense.The app listening on the wrong address.
ss -tlnpshowednodeon127.0.0.1:3000.A host firewall rule. firewalld does not filter loopback traffic, and the error was
EACCES, not a timeout.
Root cause
SELinux was in enforcing mode, as it is by default on RHEL. nginx runs in the httpd_t domain. Two separate policy rules blocked it:
httpd_tis not allowed to open outbound TCP connections to arbitrary ports unless thehttpd_can_network_connectboolean is on. Port 3000 is not a web port in the policy.Files under
/srv/app/publiccarried the labelvar_t, inherited from the directory we created withmkdirand filled byrsync.httpd_tmay only read content labelled for web serving, such ashttpd_sys_content_t.
On Ubuntu the same layout worked because Ubuntu uses AppArmor, and the nginx package does not ship an enforcing AppArmor profile.
What is happening under the hood
SELinux attaches a security context (user:role:type:level) to every process, file and port. Policy rules say which process types may do what to which object types. Access is granted only if both the Unix permission check and the SELinux check pass. When SELinux denies an access, the kernel returns EACCES to the program, exactly like a permission error, and writes an AVC (Access Vector Cache) record to the audit log.
Booleans are switches compiled into the policy that enable optional rules without writing a custom module. File labels come from rules in the policy that map paths to types; restorecon applies those rules, and semanage fcontext adds local rules for paths the default policy does not know, such as /srv/app.
Environment
Component | Version / value |
|---|---|
OS | RHEL 9.5, kernel 5.14 |
SELinux | Enforcing, targeted policy |
nginx | 1.26 from the RHEL AppStream module |
App | Node.js 22 on |
Content path |
|
Minimal reproduction
sudo mkdir -p /srv/demo && echo ok | sudo tee /srv/demo/index.html
ls -Z /srv/demo/index.html # unconfined_u:object_r:var_t:s0
# nginx server block with root /srv/demo; and proxy_pass http://127.0.0.1:3000;
sudo systemctl reload nginx
curl -i http://localhost/ # 403
curl -i http://localhost/api/ # 502
Who is affected
Anyone running nginx or Apache httpd on RHEL, CentOS Stream, Fedora, Rocky or AlmaLinux with SELinux enforcing, who proxies to a non-standard port or serves files from outside /usr/share/nginx/html or /var/www. The same pattern hits other confined daemons: PostgreSQL with a data directory on a new volume, or a web server reading files from a user's home directory.
The fix
Step 1: read the denials instead of guessing.
sudo ausearch -m AVC,USER_AVC -ts recent -i
type=AVC msg=audit(12/29/2026 10:14:02.311:612) : avc: denied { name_connect } for pid=2141 comm=nginx dest=3000 scontext=system_u:system_r:httpd_t:s0 tcontext=system_u:object_r:ntop_port_t:s0 tclass=tcp_socket permissive=0
type=AVC msg=audit(12/29/2026 10:14:09.874:618) : avc: denied { read } for pid=2141 comm=nginx name=index.html dev="vda4" ino=393287 scontext=system_u:system_r:httpd_t:s0 tcontext=unconfined_u:object_r:var_t:s0 tclass=file permissive=0
Read each record as: process type (scontext) tried permission (name_connect, read) on object type (tcontext) of class (tclass), and was denied. Port 3000 is labelled ntop_port_t in the targeted policy; the file is var_t.
audit2why explains what would allow each denial:
sudo ausearch -m AVC -ts recent --raw | audit2why
For the first record it reports that the boolean httpd_can_network_connect would allow the access. For the second it reports a missing allow rule, meaning the label is wrong rather than the policy. If the setroubleshoot-server package is installed, sudo sealert -a /var/log/audit/audit.log gives a longer human-readable explanation with suggested commands.
Step 2: allow nginx to connect to the upstream.
sudo setsebool -P httpd_can_network_connect on
getsebool httpd_can_network_connect # httpd_can_network_connect --> on
-P makes it persistent across reboots. Without it, the fix silently disappears at the next boot.
Step 3: label the content directory for web serving.
sudo semanage fcontext -a -t httpd_sys_content_t "/srv/app/public(/.*)?"
sudo restorecon -Rv /srv/app/public
ls -Z /srv/app/public/index.html # ... httpd_sys_content_t ...
semanage fcontext records the rule in local policy so future restorecon runs, and full filesystem relabels, keep the correct type. If nginx also needs to write somewhere, such as an upload directory, label only that directory httpd_sys_rw_content_t.
We reloaded nginx and both requests succeeded.
Alternatives and trade-offs
Narrower than the boolean: move the app to a labelled web port. If the upstream listens on a port already labelled
http_port_t(8080 is not; 8008 and 8009 are among those in the targeted policy, assemanage port -l | grep http_port_tshows), or you relabel your port withsemanage port -m -t http_port_t -p tcp 3000(-mbecause 3000 already has a type;-ais for ports with no label), nginx may connect without enablinghttpd_can_network_connectfor every port. Check withsesearch -A -s httpd_t -t http_port_t -c tcp_socket -p name_connect(fromsetools-console) whether that rule is unconditional or tied to a boolean on your policy version. We kept the boolean because the proxy also talks to other internal services.chcon -t httpd_sys_content_t. Works immediately but is temporary: the nextrestoreconor relabel reverts it. Usesemanage fcontextplusrestorecon.A custom policy module from
audit2allow -M. Useful for genuinely custom access, but generating modules from every denial tends to grant far more than needed. Prefer booleans and labels first.setenforce 0orSELINUX=disabled. Not a fix. It removes a protection layer for every service on the host. If you need to debug, put only the one domain in permissive mode temporarily withsudo semanage permissive -a httpd_t, collect denials, then remove it with-d.
How to test the fix
curl -s -o /dev/null -w '%{http_code}\n' http://localhost/api/health # 200
curl -s -o /dev/null -w '%{http_code}\n' http://localhost/ # 200
sudo ausearch -m AVC -ts recent # <no matches>
getenforce # Enforcing
We also rebooted the VM once to confirm the boolean and labels persisted.
Confirming in production
After deploying to production hosts, we checked the audit log for any new httpd_t denials over the first day:
sudo ausearch -m AVC -ts today -c nginx
No records. One more denial did show up a week later when the team added a /srv/app/uploads directory written by the app and served by nginx; the same semanage fcontext pattern (with httpd_sys_rw_content_t for the writer) fixed it in minutes, because we now knew to look in the audit log first.
Regression guard
The SELinux settings live in our Ansible role next to the nginx configuration, using the ansible.posix.seboolean and community.general.sefcontext modules, so a rebuilt host gets them automatically. Our smoke test after provisioning also fails if getenforce does not print Enforcing, which stops anyone from "fixing" a future denial by disabling SELinux in the image.
Security implications
The bug was SELinux doing its job: a confined web server was prevented from reaching ports and files it had not been granted. The fix widens that grant, so keep it as small as possible. httpd_can_network_connect lets the web server connect to any TCP port, which matters if nginx is compromised; labelling a specific port is tighter. Label only the content directory, not a parent like /srv. Never disable SELinux globally to make an error go away; that turns a single misconfiguration into an unprotected host.
Prevention checklist
On RHEL-family hosts, check
ausearch -m AVC -ts recentwhenever you see error 13.Label custom content paths with
semanage fcontextplusrestorecon, neverchconalone.Set booleans with
-Pand manage them in configuration management.Use per-domain permissive mode for debugging, never
setenforce 0.Fail provisioning smoke tests if SELinux is not enforcing.
References
RHEL 9 Using SELinux: https://docs.redhat.com/en/documentation/red_hat_enterprise_linux/9/html/using_selinux/index
Troubleshooting problems related to SELinux: https://docs.redhat.com/en/documentation/red_hat_enterprise_linux/9/html/using_selinux/troubleshooting-problems-related-to-selinux_using-selinux
Configuring SELinux for applications and services with non-standard configurations: https://docs.redhat.com/en/documentation/red_hat_enterprise_linux/9/html/using_selinux/configuring-selinux-for-applications-and-services-with-non-standard-configurations_using-selinux
nginx
proxy_pass: https://nginx.org/en/docs/http/ngx_http_proxy_module.html
Add a label
Embed this post
<iframe src="https://hibug.me/selinux-nginx-permission-denied-avc-rhel" width="700" height="420" frameborder="0" loading="lazy"></iframe>
I am a frontend engineer who has worked on dashboards, booking flows and design systems, mostly in React and TypeScript. I care about the parts of the web platform that frameworks hide: forms, cookies, rendering cost and what the browser actually does with our code. The bugs I write about usually passed every review and only appeared with production data: a map with a million points, a name with an accent in a PDF, a cookie that one subdomain never sees. Each post shows a minimal reproduction, why the browser or framework behaves that way, and a fix with a test.
Related Stories
Azure managed identity gets 401 from our API: token issued for the wrong audience
Comments
0No comments yet. Be the first to comment!
Sign In to post a comment.