本文介绍了设置服务器块后,Nginx不再提供我的域名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将我的MERN应用程序部署到Digital Ocean Droplet(Ubuntu 20.04服务器)上.

我按照以下教程中的步骤安装Nginx.[我已经完成了前面的所有步骤]

但是,设置服务器块后,当我访问 http://sundaray.io 时,出现以下错误.

sundaray.io 是我的域名.

当我运行命令/var/log/nginx/error.log 时,我看到以下内容:

如何解决该错误?

EDIT-1

服务器块为了创建服务器块,我按顺序执行了以下命令:

  1. mkdir -p/var/www/sundaray.io/html
  2. nano/etc/nginx/sites-available/sundaray.io 然后,我粘贴了以下配置块.
 服务器{听80;听[::]:80;根目录/var/www/sundaray.io/html;index index.html index.htm index.nginx-debian.html;server_name sundaray.io www.sundaray.io;地点/{try_files $ uri $ uri/= 404;}} 

错误执行命令 cat/var/log/nginx/error.log 给我以下结果:

EDIT-2

执行 chown -R nginx:nginx/var/www/sundaray.io/html 会引发以下错误:

EDIT-3

执行 ps -elf | grep nginx 会得到以下结果:

EDIT-4 当我执行命令 ls/var/www/sundaray.io/html 时,得到以下结果:

解决方案

1. chmod 777 从来都不是个好主意.

NGINX在运行用户下运行.如何检查运行用户:

ps -elf | grep nginx .通常是 nginx .代替 777 (向所有人开放),执行 chmod -R 755/path/to/folder chown -R nginx:nginx/path/to/folder

但是同意.对于故障排除,可以使用它.但是回到您的问题.

2.目录列表已禁用.

错误告诉您nginx无法列出目录内容.这是默认行为.确保

  root/var/www/sundaray.io/html; 

此路径存在且

  index index.html index.htm index.nginx-debian.html; 

找到了这些文件之一!

没有这些文件,NGINX无法在/上加载默认的索引文件.在/var/www/sundaray.io/html 中放置一些内容,例如

printf< html> \ n< body> \ n< h1>来自Nginx//h1/\ n</body>/n</html>''>/var/www/sundaray.io/html/index.html&&chown nginx:nginx/var/www/sundaray.io/html/index.html .这应该为您生成一个index.html.

如果您只想测试没有任何文件的服务器配置:

 位置/{返回200"Hello on $ host $ uri \ n";} 

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 BLOCKIn order to create the server block, I executed the following commands in order:

  1. mkdir -p /var/www/sundaray.io/html
  2. nano /etc/nginx/sites-available/sundaray.ioThen, 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;
        }
}

ERRORExecuting 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-4When I executed the command ls /var/www/sundaray.io/html, I got the following result:

解决方案

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

2. Directory-Listing disabled.

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";
}

这篇关于设置服务器块后,Nginx不再提供我的域名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 10:51