Rocky Linux 10 Tutorial / Configure an Nginx site with SELinux and firewalld
To configure an Nginx site on Rocky Linux 10, you need to separate site configuration and document directories, and check Nginx configuration, file permissions, SELinux context, and firewall in order. In this tutorial, we will maintain the existing default site on port 80 while adding a static site on port 18025, and compare responses locally and from other computers.
Scope of practice and checking existing sites
Nginx is a web server that receives requests and delivers files or proxies requests to other programs. Here, we only handle static sites that serve files directly. Running PHP, reverse proxy, and HTTPS certificates require separate configuration.
Perform the exercise on a separate Rocky Linux 10 virtual machine (VM) with a user that has sudo privileges and console access prepared. The source lab environment was Rocky Linux 10.2, Nginx 1.26.3-6.el10_2.7, SELinux Enforcing. In this environment, Apache was stopped and there was no existing web port listener. If there are any currently running services, you should check ports and configurations before installing and starting.
cat /etc/rocky-release
sudo ss -lntp
systemctl is-active nginx httpd
sudo firewall-cmd --get-active-zones
getenforce
sudo dnf install nginx policycoreutils-python-utils
rpm -q nginx
sudo nginx -t
If any service was not installed or was stopped in is-active, the exit code may not be 0. Read the status check results to make a judgment. If Apache is already using port 80, it will conflict with the default Nginx configuration, so do not start it as is.
In the Rocky package /etc/nginx/nginx.conf, include /etc/nginx/conf.d/*.conf; was included within the http section. Do not assume the sites-available structure of Debian or Ubuntu; check the actual package settings. sudo nginx -T outputs the entire settings to be applied, but do not share any secret values externally.
sudo bash <<'BASH'
set -eu
test ! -e /root/lf-course25-before
install -d -m 0700 /root/lf-course25-before
cp -a /etc/nginx /root/lf-course25-before/nginx
BASH
sudo nginx -t && sudo systemctl start nginx
curl -sS -o /tmp/lf25-default-before.html -w 'HTTP %{http_code}\n' http://127.0.0.1/
If the first check shows that a backup directory already exists, stop and check the previous exercise state. The above command is based on a new exercise environment with empty ports. The default site in this environment returned HTTP 200. Instead of replacing the default configuration file entirely, we will add a separate file.
Create the document directory and site configuration
The name to use in this exercise is course25.test, and the document directory is /srv/lf-web25. .test is a test name, and writing this name in server_name does not create a DNS record that links the domain name to an IP address. First, test by specifying the site name using curl's Host header.
sudo bash <<'BASH'
set -eu
test ! -e /srv/lf-web25
test ! -e /etc/nginx/conf.d/lf-course25.conf
LISTENERS=$(ss -H -lnt 'sport = :18025')
test -z "$LISTENERS"
install -d -m 0755 /srv/lf-web25
printf 'LF25 nginx site works\n' > /srv/lf-web25/index.html
chmod 0644 /srv/lf-web25/index.html
cat > /etc/nginx/conf.d/lf-course25.conf <<'EOF'
server {
listen 18025;
server_name course25.test;
root /srv/lf-web25;
index index.html;
access_log /var/log/nginx/lf-course25-access.log;
error_log /var/log/nginx/lf-course25-error.log;
location / {
try_files $uri $uri/ =404;
}
}
EOF
BASH
If there is an existing path or port 18025 is being used by another program, do not overwrite this example as it is. Directory 0755 grants permission to traverse the path and read the directory listing, while file 0644 allows reading contents. There is no need to change the owner of static files that Nginx only reads to the web server account or to open them with 0777. Higher-level paths must also have traverse permissions.
| Settings | Roles and Points to Check |
|---|---|
listen 18025 |
This is the TCP port that receives requests. Since an IPv4 address was not specified, this exercise receives requests from external interfaces as well. The external public range is limited by the firewall and the VM network. |
server_name |
This is the condition to select the server block that matches the name of requests coming to the same address and port. It is not a DNS registration or access authentication feature. |
root and index |
Sets the file root path and the default document for directory requests. Here, / requests are handled as index.html. |
try_files |
Finds the file or directory corresponding to the request URI, and returns 404 if not found. $uri is an Nginx variable, not a shell variable, so it is stored as-is in a heredoc with quotes. |
| Site-specific logs | Separates access and error log files to avoid mixing requests from different sites. |
Nginx's official introductory document explains configuration blocks and methods for serving static files. In try_files directive document , you can check the meaning of file search and the last processing step.
SELinux path contexts and port allowances
Even if general file permissions are correct, SELinux must allow access. SELinux compares the security contexts attached to processes and files with its policies. The Nginx process in this environment ran in the httpd_t domain, and the new /srv document was initially var_t.
ls -Zd /srv/lf-web25 /srv/lf-web25/index.html
sudo semanage fcontext -l -C
sudo semanage port -l | grep http_port_t
sudo semanage fcontext -a -t httpd_sys_content_t '/srv/lf-web25(/.*)?'
sudo restorecon -Rv /srv/lf-web25
sudo semanage port -a -t http_port_t -p tcp 18025
sudo nginx -t && sudo systemctl reload nginx
sudo ss -lntp 'sport = :18025'
semanage fcontext registers permanent rules to be applied to paths, and restorecon applies those rules to actual files. Simply registering the rules does not immediately change the context of existing files. httpd_sys_content_t is used for documents to be read on this static site, distinguishing them from paths that require write permissions for actions like uploads. For detailed principles, refer to Rocky Linux SELinux documentation .
Port 18025 was newly registered because it was not included in the allowed HTTP ports at the start of the practice. If the port is already registered for another type, do not automatically change it to -m after receiving an error. Check existing services first, then choose an available port or review the necessary policy scope. SELinux port allowance is separate from allowing network packets in firewalld.
In the source comparison, when only the port was allowed first and the document was var_t, HTTP 403 occurred, and the audit log recorded an AVC (Access Vector Cache) denial where httpd_t could not access the var_t file. After applying path rules and restorecon, it changed to 200. SELinux was not turned off, nor was the entire service set to permissive.
Comparison of local response with response from another computer
curl -sS -H 'Host: course25.test' -w 'HTTP %{http_code}\n' http://127.0.0.1:18025/
curl -sS -o /dev/null -H 'Host: course25.test' -w 'HTTP %{http_code}\n' http://127.0.0.1:18025/missing
For the first request, LF25 nginx site works and 200 are expected, and a non-existing path is expected to return 404. Do not just look at the number 200; also check whether it is the expected site's content. Local success is evidence that Nginx and document access is possible, but it does not mean that the external network path was successful.
Check the zone that the external interface belongs to and allow only the practice ports for that zone. The public below is the value checked on this VM, so change it to the actual value in other environments. Do not modify the existing SSH allowance rules.
sudo firewall-cmd --get-active-zones
sudo firewall-cmd --zone=public --add-port=18025/tcp
sudo firewall-cmd --zone=public --query-port=18025/tcp
On another computer in the same network, make the request using the actual server IPv4 address. The following IP is an example, so do not use it as is. In Windows, you can use curl.exe to distinguish it from PowerShell aliases.
curl --max-time 5 -sS -H 'Host: course25.test' http://192.0.2.10:18025/
The NAT (Network Address Translation) environment of VirtualBox does not allow direct access to the guest address from the host. By adding a practical rule in VM settings: Network → Adapter 1 → Advanced → Port Forwarding with TCP, Host IP 127.0.0.1, Host Port 18025, and Guest Port 18025, you can check http://127.0.0.1:18025/ from the host. If you leave the Host IP empty, the exposure range may increase, so it is specified for local practice. Guest firewall allowance is still required.
In the source practice, requests were made to the VM through localhost NAT forwarding from curl in Windows. Before allowing the firewall, it timed out with no response for 4 seconds, and after allowing the runtime port, the same content and 200 were confirmed. This does not test internet public access or actual DNS query success.
sudo firewall-cmd --permanent --zone=public --add-port=18025/tcp
sudo firewall-cmd --permanent --zone=public --query-port=18025/tcp
sudo firewall-cmd --zone=public --query-port=18025/tcp
Both queries must be yes to reflect the current and saved settings. You do not need to reload the entire firewall to separately add current allowance and permanent allowance. At this stage, the reboot persistence test was not performed. The distinction between the two settings is also explained in firewalld official port allowance documentation . The http service definition of standard HTTP generally means port 80, so do not confuse it with this non-standard port allowance.
Diagnose by distinguishing between 403, 404, and configuration errors
If the connection is refused or times out, first check the listener, address and VM network, and the actual zone. If an HTTP response arrives, check the site that handled that request and the logs. Do not conclude that it is a SELinux issue based solely on a 403.
sudo nginx -t
sudo systemctl status nginx --no-pager
sudo journalctl -u nginx -b -n 30 --no-pager
sudo tail -n 20 /var/log/nginx/lf-course25-access.log
sudo tail -n 20 /var/log/nginx/lf-course25-error.log
namei -l /srv/lf-web25/index.html
ls -Z /srv/lf-web25/index.html
sudo ausearch --input-logs -m AVC -c nginx -ts recent -i
In practice, even if the correct SELinux context is maintained, changing the file to root-owned 0600 resulted in a 403, and reverting it to 0644 returned 200. The Permission denied in the error log can appear from both regular permissions and SELinux, so correlate AVC events at the failed request time with path permissions. Do not determine a specific cause based solely on the fact that there is no log. A 404 for a non-existent path is the intended result of this try_files.
After modifying the settings, connect verification success and application as in sudo nginx -t && sudo systemctl reload nginx. In practice, when a non-existent directive was added to a file, the configuration check failed with exit code 1, but the existing Nginx process that had not yet applied it continued to return 200 with the same PID. Immediately reverted the incorrect file back to its original content and rechecked. Do not judge the stored configuration as normal just because it returns the previous response.
Also, for this 18025 instance, there was only one server block, so even when requesting via Host: another.test, the same site returned 200. This is because if there is no matching name, the default server for that address and port may be selected. server_name is not an access restriction device, and if you want to reject unknown names, you need to design a separate default server and explicit response policy. For detailed rules on name selection, refer to Nginx server names document .
Verify the existing site and remove the lab configuration
After the new site is set up, also check the original port 80 default site. When comparing it with the initially saved response, the body should be the same to conclude that the current static default page has not been changed. Since dynamic responses of actual operating sites can vary over time, the core functions, status, and content should be compared separately.
curl -sS -o /tmp/lf25-default-after.html -w 'HTTP %{http_code}\n' http://127.0.0.1/
cmp /tmp/lf25-default-before.html /tmp/lf25-default-after.html
sudo diff -u /root/lf-course25-before/nginx/nginx.conf /etc/nginx/nginx.conf
In the three recorded source comparisons, the default site all returned 200 and the response bodies were the same. Existing configuration files, including nginx.conf, were retained. If this server will continue to be used, maintain only the necessary sites, and when automatic start after reboot is required, select sudo systemctl enable nginx and then separately verify the reboot.
When removing the practice, first move the site files you added to a backup location, then check and reload. The following commands apply only to the files and rules newly created in this course. Do not delete any existing path or port rules you reused.
sudo mv /etc/nginx/conf.d/lf-course25.conf /root/lf-course25-before/site.conf
sudo nginx -t && sudo systemctl reload nginx
sudo firewall-cmd --zone=public --remove-port=18025/tcp
sudo firewall-cmd --permanent --zone=public --remove-port=18025/tcp
sudo semanage fcontext -d '/srv/lf-web25(/.*)?'
sudo semanage port -d -p tcp 18025
sudo rm -- /srv/lf-web25/index.html
sudo rmdir /srv/lf-web25
After the settings are applied, check that listener 18025 has disappeared and delete the port forwarding rules added to the VM. Back up the practice logs if necessary. In the source VM, new logs were stored, and site files, documents, policies, firewall, and NAT rules were cleaned up before stopping Nginx. The existing settings and firewall were compared to the original state, while SSH, firewalld active status, and SELinux Enforcing were maintained. The Nginx package was left for subsequent practices.




