I am in the process of deploying my MERN app to a Digital Ocean droplet (Ubuntu 20.04 server).
I followed the steps in the following tutorial to install Nginx. [I have completed all the previous steps]
https://www.digitalocean.com/community/tutorials/how-to-install-nginx-on-ubuntu-20-04
When I visit http://134.122.112.22
, I see the Nginx landing page, as expected.
However, after setting up server blocks, when I visit http://sundaray.io
, I get the following error.
sundaray.io
is my domain name.
When I run the command /var/log/nginx/error.log
, I see the following:
How can I fix the error?
EDIT-1
SERVER BLOCK In order to create the server block, I executed the following commands in order:
mkdir -p /var/www/sundaray.io/html
nano /etc/nginx/sites-available/sundaray.io
Then, I pasted in the following configuration block.server {
listen 80;
listen [::]:80;
root /var/www/sundaray.io/html;
index index.html index.htm index.nginx-debian.html;
server_name sundaray.io www.sundaray.io;
location / {
try_files $uri $uri/ =404;
}
}
ERROR
Executing the command cat /var/log/nginx/error.log
gave me the following result:
EDIT-2
Executing chown -R nginx:nginx /var/www/sundaray.io/html
threw the following error:
EDIT-3
Executing ps -elf |grep nginx
gave the following result:
EDIT-4
When I executed the command ls /var/www/sundaray.io/html
, I got the following result:
chmod 777
is NEVER a good idea.NGINX operats under a runuser. How to check the runuser:
ps -elf |grep nginx
. Normaly it will be nginx
. Instead of 777
(Open for everyone) do chmod -R 755 /path/to/folder
and chown -R nginx:nginx /path/to/folder
But agree. For troubleshooting it can be used. But back to your problem.
The error is telling you nginx can not list the directory content. Which is the default behavior. Make sure
root /var/www/sundaray.io/html;
This path exists AND
index index.html index.htm index.nginx-debian.html;
there are one of these files located!
Without any of these files NGINX can't load the default index file on /
. Put something in /var/www/sundaray.io/html
like
printf "<html>\n<body>\n<h1>Hello from NGINX</h1>\n</body>\n</html>" > /var/www/sundaray.io/html/index.html && chown nginx:nginx /var/www/sundaray.io/html/index.html
. This should generate an index.html for you.
If you just want to test your server configuration without any files:
location / {
return 200 "Hello on $host$uri\n";
}