Question

How to fix 404 error nginx and Ubuntu 20 LTS

Posted October 3, 2020 181 views
NginxUbuntu 20.04

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.

×
1 answer

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).

/--- 
   |---etc--------------------------------------------
   |---home                                          |---nginx
   |---root--------------------------                |---NetworkManager
   |---var--------                   |---sites       |---cron.d
   |---...       |---www             |---snap        |---cron.daily
                 |---log             |---var         |---cron.hourly

To list the content of directory sites, using absolute path, you could run the command like

ls -la /root/sites/

where the path consists of

  • / - root directory
  • root/ - home directory of user root; it is not root directory ;P
  • sites/ - directory sites

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:

root@WebServer:~#

In the above diagram, your actual position is highlighted.

To list the content of directory sites, your command with relative path should look like:

ls -la sites/

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:

user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
  worker_connections 768;
  # multi_accept on;
}

http {

 ##
 # Basic Settings
 ##

 sendfile on;
 tcp_nopush on;
 tcp_nodelay on;
 keepalive_timeout 65;
 types_hash_max_size 2048;
 # server_tokens off;

 # server_names_hash_bucket_size 64;
 # server_name_in_redirect off;

 include /etc/nginx/mime.types;
 default_type application/octet-stream;

  ##
  # SSL Settings
  ##

  ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
  ssl_prefer_server_ciphers on;

  ##
  # Logging Settings
  ##

  access_log /var/log/nginx/access.log;
  error_log /var/log/nginx/error.log;

  ##
  # Gzip Settings
  ##

  gzip on;
  # gzip_vary on;
  # gzip_proxied any;
  # gzip_comp_level 6;
  # gzip_buffers 16 8k;
  # gzip_http_version 1.1;
  # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

  ##
  # Virtual Host Configs
  ##

  include /etc/nginx/conf.d/*.conf;
  include /etc/nginx/sites-enabled/*;
}
by Brian Boucheron
When you first create a new Ubuntu 20.04 server, you should perform some important configuration steps as part of the basic setup. These steps will increase the security and usability of your server, and will give you a solid foundation for subsequent actions.
  • Wow! Thank you for explaining all that to me. It wasn’t explained in the nginx course about how we aren’t in the root directory when we login as root. That makes a ton of difference. As for setting up a web site, I was actually using this instance to perform the configurations recommended in the course. It’s basically a learning droplet. So I tried moving the sites/demo folder to the actual root directory and it worked that it found the files but did not solve the unauthorized part. I did restore the original config file and the nginx server is up and running again. THANK YOU!

Submit an Answer