Welcome! Log In Create A New Profile

Advanced

404 on all files after change of root dir

Posted by jks 
jks
404 on all files after change of root dir
January 22, 2024 04:17PM
I am just setting up nginx on OpenBSD 7.4.
After pkg_add nginx runs fine from the default root dir (/var/www/htdocs) but cannot find any files after changing the root to /data/www.
Does anybody have a hint, why the root-change does not work? /var and /data are different drives, but mounted with the same settings.

Here is my nginx.conf:
http {
include mime.types;
default_type application/octet-stream;
index index.html index.htm;

server {
listen 80;
server_name aretha;
#root /var/www/htdocs;
root /data/www;
}
}

after "doas rcctl restart nginx"
I run curl and get:
curl http://aretha
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.24.0</center>
</body>
</html>

the index.html file is present and has the right permissions:
>ls -la /data/www/
drwxr-xr-x 3 www www 512 Jan 21 15:26 .
drwxr-xr-x 4 root wheel 512 Jan 21 14:43 ..
-rw-r--r-- 1 www www 87 Jan 21 15:09 index.html

while with root set to /var/www/htdocs I get: the index.html file:
curl http://aretha
<html>
<head>
<title>Test</title>
</head>
<body>
<h1>Test</h1>
</body>
</html>
Re: 404 on all files after change of root dir
February 08, 2024 07:21PM
The root cause of the issue is likely that NGINX, running in a chroot environment confined to /var/www on OpenBSD, cannot access directories outside of its chroot jail, such as /data/www. To resolve this, either move the document root back inside the chroot directory (e.g., under /var/www), or adjust the chroot settings for NGINX, ensuring all required files are accessible within the new chroot environment.
Re: 404 on all files after change of root dir
February 08, 2024 07:22PM
To fix the issue of NGINX not serving files from /data/www due to the chroot environment on OpenBSD, you have a couple of options:

Option 1: Move the Document Root Inside the Chroot Directory
Move Your Web Content: Move your web content from /data/www to a directory inside the default chroot directory of NGINX, which is /var/www. For example, you could create a new directory /var/www/data-www and move your files there.

sh
Copy code
doas mkdir /var/www/data-www
doas cp -R /data/www/* /var/www/data-www/
Update NGINX Configuration: Update the root directive in your nginx.conf to point to the new location inside the chroot.

nginx
Copy code
server {
listen 80;
server_name aretha;
root /var/www/data-www;
}
Restart NGINX: Apply the changes by restarting NGINX.

sh
Copy code
doas rcctl restart nginx
Option 2: Modify the Chroot Location for NGINX (Advanced)
This approach involves changing the chroot environment for NGINX to include your desired document root. This method is more complex and can have security implications.

Adjust NGINX's Chroot in rc.d Script: Edit the NGINX service script in /etc/rc.d/nginx (or wherever OpenBSD places the rc scripts for services) to change the chroot directory. This might involve modifying a line that sets the chroot environment for NGINX, but be aware that this could potentially expose your system to greater security risks.

Ensure Required Files Are Accessible: Make sure all files needed by NGINX, including configuration files, log directories, and any libraries or binaries it depends on, are accessible within the new chroot path. You might need to copy or link these files into the new chroot environment.

Update NGINX Configuration: Ensure your nginx.conf file is updated and accessible within the new chroot location, and that it points to the correct root directory.

Restart NGINX: Apply the changes by restarting NGINX.

sh
Copy code
doas rcctl restart nginx
Recommendation
For most users, Option 1 is recommended due to its simplicity and reduced security risk. Modifying the chroot environment as described in Option 2 should only be done by advanced users who are familiar with the security implications and who have a specific need to serve files from outside the default chroot directory.
Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 143
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready