Fix SELinux permission denied for nginx on AlmaLinux and Rocky Linux

nginx gives 502 or 403 on AlmaLinux or Rocky even with the right file permissions? Find the SELinux denial and fix it with a boolean, a label or a port.

Tested on AlmaLinux 10, AlmaLinux 9, Rocky Linux 9

Before you start

  • A server running AlmaLinux 9 or 10 or Rocky Linux 9
  • nginx installed from the distribution's packages
  • Root or a user with sudo

When nginx on AlmaLinux or Rocky Linux logs (13: Permission denied) even though the file modes and owners are right, SELinux is blocking it: run sudo ausearch -m avc -ts recent to see the denial. Then fix the one thing it names: sudo setsebool -P httpd_can_network_connect 1 for a reverse proxy that gets 502, semanage fcontext plus restorecon for files outside /usr/share/nginx/html that get 403, or semanage port for nginx listening on an unusual port.

Don't turn SELinux off to make the error go away. Each fix below allows exactly the access nginx needs and leaves the rest of the protection in place. The commands are the same on AlmaLinux 9, AlmaLinux 10 and Rocky Linux 9, where SELinux runs in enforcing mode with the targeted policy by default. We reproduced all three errors below, and fixed each with the command given, on fresh AlmaLinux 10.3 and Rocky Linux 9.9 servers on 3 December 2026; the log lines in the table are copied from them.

The examples use an app on 127.0.0.1:8000 and a site in /srv/www/example. Use your own.

Check that SELinux is the cause

First confirm SELinux is enforcing:

bash
getenforce

It prints Enforcing. If it prints Permissive or Disabled, SELinux isn't blocking anything and the problem is elsewhere: see fix nginx 502 and 504 errors and the Troubleshooting section below.

Then reproduce the error (load the page once) and list the denials from the last ten minutes:

bash
sudo ausearch -m avc -ts recent

Each denial is a line with avc: denied, the action ({ name_connect }, { read }, { name_bind }), the process (comm="nginx") and two labels: scontext is nginx's (httpd_t) and tcontext is the file's or the port's. That second label tells you which fix you need.

For a plain-English reason, install the SELinux tools (they also provide semanage, used below) and pass the denials through audit2why:

bash
sudo dnf install -y policycoreutils-python-utils
sudo ausearch -m avc -ts recent | audit2why

audit2why often names the exact boolean to turn on. nginx's own error log says the same thing from the other side:

bash
sudo tail -n 20 /var/log/nginx/error.log
What nginx logs What it means Fix
connect() to 127.0.0.1:8000 failed (13: Permission denied) while connecting to upstream nginx may not open network connections, so every proxied request is a 502 Let nginx proxy to your app
open() "/srv/www/example/index.html" failed (13: Permission denied) The files carry a label nginx may not read, so the page is a 403 Label a web root outside the default
bind() to 0.0.0.0:8081 failed (13: Permission denied) nginx may not listen on that port, so it fails to start Let nginx listen on another port

Let nginx proxy to your app

nginx runs in the httpd_t domain, the same one Apache uses, so the switches that control it are named httpd_*. Out of the box it may serve files but not connect out to other ports, which is exactly what proxy_pass http://127.0.0.1:8000 does. The boolean httpd_can_network_connect allows it:

bash
getsebool httpd_can_network_connect
sudo setsebool -P httpd_can_network_connect 1

The first command shows --> off before the change. -P keeps the setting after a reboot; without it, the 502s come back on the next boot.

Check it worked

bash
getsebool httpd_can_network_connect
curl -sI http://127.0.0.1/ | head -n 1

The boolean shows --> on, and the proxied site answers with your app's status instead of 502 Bad Gateway. If nginx proxies to a database port directly (rare), the boolean is httpd_can_network_connect_db instead. sudo semanage boolean -l | grep httpd_can_network lists both, with a description of each.

Label a web root outside the default

SELinux decides what nginx may read by the files' label, not their path or owner. The default web root, /usr/share/nginx/html, is labelled httpd_sys_content_t. A directory you create elsewhere, such as /srv/www, gets a generic label, and files you move (mv) from your home directory keep the label they had there. nginx gets a 403 for both.

See the labels with ls -Z:

bash
ls -Zd /srv/www/example
ls -Z /srv/www/example

Tell SELinux that everything under /srv/www is web content, then apply the rule to the files already there:

bash
sudo semanage fcontext -a -t httpd_sys_content_t "/srv/www(/.*)?"
sudo restorecon -Rv /srv/www

semanage fcontext stores the rule, so future files and any later full relabel get it too. restorecon applies it now and prints each file it changes. Avoid chcon for this: it changes the label only until the next relabel, and then the 403s return.

If nginx must also write there (an uploads folder, a cache), give that one directory the writable type instead:

bash
sudo semanage fcontext -a -t httpd_sys_rw_content_t "/srv/www/example/uploads(/.*)?"
sudo restorecon -Rv /srv/www/example/uploads

Serving straight from a home directory is possible with the httpd_enable_homedirs boolean, but it opens every user's home to the web server's label. Moving the site to /srv/www is the cleaner fix.

Check it worked

bash
ls -Z /srv/www/example
curl -sI http://127.0.0.1/ | head -n 1

The files show httpd_sys_content_t, and the page answers 200 OK (as long as the normal Unix permissions let the nginx user read them).

Let nginx listen on another port

SELinux also labels ports, and nginx may only listen on ports labelled http_port_t. List them:

bash
sudo semanage port -l | grep -w http_port_t

To let nginx listen on a port that isn't in that list, add it:

bash
sudo semanage port -a -t http_port_t -p tcp 8081
sudo systemctl restart nginx

If another service's label already owns the port, semanage says so and moves it: for 8081, which belongs to transproxy_port_t, ours printed Port tcp/8081 already defined, modifying instead. -m in place of -a does the same explicitly. Opening the port to the internet is a separate step in the firewall: see configure firewalld.

Check it worked

bash
sudo semanage port -l | grep -w http_port_t
sudo ss -tlnp | grep ':8081'

The port appears in the list, and ss shows nginx listening on it.

When no boolean or label fits

Some setups need an access no switch covers, such as nginx connecting to a Unix socket your app created in an unusual place. Try the label and boolean fixes first; audit2why says when one applies. If none does, audit2allow can build a small local policy module from the denials:

bash
sudo ausearch -m avc -ts recent -c nginx | audit2allow -M nginx-local
cat nginx-local.te
sudo semodule -i nginx-local.pp

Read nginx-local.te before you install it: it allows exactly what was denied, and if the denials came from something you didn't expect, it allows that too. sudo semodule -r nginx-local removes the module.

To test whether SELinux is the only thing in the way without lowering it for the whole server, make just nginx's domain permissive, try the page, then switch it back:

bash
sudo semanage permissive -a httpd_t
sudo semanage permissive -d httpd_t

While it's permissive, denials are still logged but not enforced, so ausearch shows everything nginx would have been refused in one go.

Troubleshooting

Still denied, but ausearch shows nothing. Some denials are hidden by dontaudit rules. sudo semodule -DB rebuilds the policy with them logged, reproduce the error, read ausearch again, then sudo semodule -B to hide them again.

semanage: command not found. Install policycoreutils-python-utils with dnf, as in the first section.

The 403 stays after restorecon. SELinux may be fine and the Unix permissions wrong. namei -l /srv/www/example/index.html shows every directory on the path; the nginx user needs execute (x) on each directory and read on the file.

The 502 stays after setsebool. Check the app is actually running and listening on the address in proxy_pass (sudo ss -tlnp). The other causes of a 502 are in fix nginx 502 and 504 errors.

Someone set SELINUX=disabled earlier. Files created while SELinux was off have no labels. Set SELINUX=enforcing in /etc/selinux/config, then sudo touch /.autorelabel and reboot; the first boot relabels the whole disk and takes longer than usual.

Next steps

Doing it on Rack4

Rack4 offers AlmaLinux 10, AlmaLinux 9 and Rocky Linux 9 on every plan, from the projects' own cloud images with SELinux left enforcing. The nginx one-click app installs nginx on first boot on either family. If a change locks nginx out entirely, the panel's serial console still gets you a shell to undo it.

Ready when you are

Order a server