I was following along in a class in nginx when we got to creating a virtual host. They are using digital ocean in the tutorials, and I’m doing the same (I’ve been for years). I logged into my server and from the root directory created the sites/demo folder and put my html file in there.
I’m getting a 404 error that the file doesn’t exist, and it DOES EXIST. But I’m also getting some problems in the terminal when I try to list the contents of the folders:
root@WebServer:~# ls
sites snap var
root@WebServer:~# ls -l
total 12
drw-r--r-- 3 root root 4096 Oct 3 00:21 sites
drwxr-xr-x 3 root root 4096 Oct 2 22:40 snap
drw-r--r-- 3 root root 4096 Oct 2 23:08 var
root@WebServer:~# ls -l /sites/
ls: cannot access '/sites/': No such file or directory
root@WebServer:~# ls -l /sites
ls: cannot access '/sites': No such file or directory
root@WebServer:~# ls -l sites/
total 4
drw-r--r-- 2 root root 4096 Oct 3 00:22 demo
root@WebServer:~# cd sites
root@WebServer:~/sites# ls -l
total 4
drw-r--r-- 2 root root 4096 Oct 3 00:22 demo
root@WebServer:~/sites# ls -l /demo/
ls: cannot access '/demo/': No such file or directory
root@WebServer:~/sites# ls -l demo
total 4
-rw-r--r-- 1 root root 89 Oct 3 00:22 index.html
root@WebServer:~/sites# cd demo
root@WebServer:~/sites/demo# ls -l
total 4
-rw-r--r-- 1 root root 89 Oct 3 00:22 index.html
root@WebServer:~/sites/demo# cd ~
root@WebServer:~# ls -l sites/demo/
total 4
-rw-r--r-- 1 root root 89 Oct 3 00:22 index.html
root@WebServer:~#
My /etc/nginx/nginx.conf file:
events {}
http {
include /etc/nginx/mime.types;
server {
listen 80;
server_name 134.122.23.34;
root /sites/demo;
}
}
My error log for nginx:
2020/10/03 14:14:29 [error] 13135#13135: *1 "/sites/demo/index.html" is not found (2: No such file or directory), client: 71.224.30.124, server: 134.122.23.34, request: "GET / HTTP/1.1", host: "134.122.23.34"
2020/10/03 14:15:39 [error] 13161#13161: *2 "/sites/demo/index.html" is not found (2: No such file or directory), client: 71.224.30.124, server: 134.122.23.34, request: "GET / HTTP/1.1", host: "134.122.23.34"
2020/10/03 14:20:53 [error] 13161#13161: *3 "/sites/demo/index.html" is not found (2: No such file or directory), client: 83.97.20.29, server: 134.122.23.34, request: "GET / HTTP/1.0"
My access log:
71.224.30.124 - - [03/Oct/2020:14:14:29 +0000] "GET / HTTP/1.1" 404 153 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0 Safari/605.1.15"
71.224.30.124 - - [03/Oct/2020:14:15:39 +0000] "GET / HTTP/1.1" 404 153 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0 Safari/605.1.15"
83.97.20.29 - - [03/Oct/2020:14:20:53 +0000] "GET / HTTP/1.0" 404 153 "-" "-"
Can someone please help me understand why it isn’t seeing the files? I also tried creating a /var/www folder and got the same issues.
These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.
Hi, Let me explain the listing issue first. To list content of the particular directory you need to use a command (e.g. ls) and path what leads you to that directory. There are two approaches to building directory path: absolute and relative. Absolute path always leads from the beginning of the directory structure (directory tree). This is the left side on the diagram below, with the slash symbol /. It may be confusing but it is called root directory (in that case - system’s root directory).
To list the content of directory sites, using absolute path, you could run the command like
where the path consists of
The different approach to absolute path is relative path. You build it from the place you actually are in. In your case, you are logged into your Ubuntu as a user root, and you are in the root’s home directory. In the example below, the word root stands for your username, and tilde symbol ~ stands for user’s home directory:
In the above diagram, your actual position is highlighted.
To list the content of directory sites, your command with relative path should look like:
There is one big advantage of absolute path over relative path. Wherever you are in the directory structure at the moment, the absolute path will work.
Nginx issue Regarding my above explanation, your server block’s root directory is /root/sites/demo/. But looking at your configuration, correcting that path will not get your website working. What I can suggest is:
1. Restore original content of the file /etc/nginx/nginx.conf. I included that content at the end of my answer.
2. Configure your droplet to not use the user root for daily tasks. You can follow this tutorial: https://www.digitalocean.com/community/tutorials/initial-server-setup-with-ubuntu-20-04
3. Configure nginx and create your first website following this tutorial (from step 5): https://www.digitalocean.com/community/tutorials/how-to-install-nginx-on-ubuntu-20-04
Here is the original content of /etc/nginx/nginx.conf file: