【问题标题】:404 Not Found when using Nginx Web Server使用 Nginx Web 服务器时找不到 404
【发布时间】:2016-10-24 19:46:01
【问题描述】:

在浏览器上运行我的 IP 地址时,屏幕底部出现 404 Not Found with nginx/1.8.0。我知道这是一个经常被问到的问题,因为我已经看到了多篇关于这个问题的帖子,并且在过去的 4-5 个小时里我一直在浏览这些帖子,但由于某种原因仍然无法解决这个问题。所以现在我知道必须编辑 nginx.conf 文件以反映 HTML 文件的正确路径。这是我的 nginx.conf 文件默认的样子:

worker_processes  2;

pid        logs/nginx.pid;
error_log syslog:server=unix:/dev/log,facility=local7,tag=nginx,severity=error;

events {
worker_connections  1024;
}


http {
include       mime.types;
default_type  application/octet-stream;

access_log syslog:server=unix:/dev/log,facility=local7,tag=nginx,severity=info combined;

sendfile        on;
keepalive_timeout  65;


server {
    listen       80;
    server_name  localhost;

    location / {
     root   html;

    index  index.html index.htm;
        autoindex on;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }
}
}

所以在查看 .conf 文件后,我意识到 root /path/ 可能是导致问题的原因。所以我所做的是转到目录中的 index.html 文件并在其上运行 pwd 并用此 pwd 值替换路径。然后我再次开始构建/制作 nginx,我也重新启动了 nginx。但是,这仍然导致 404 Not Found 错误,所以我不确定我是否误解了 root 的含义或是否存在其他问题。我也知道从 Apache 切换到 Nginx 可能会出现一些问题,如果处理不当,这可能会导致 404 Not Found 错误,尽管我无法获得太多信息以及如何检查这是否真的适用我的情况与否。谁能指出我如何解决这个问题的正确方向?我一直在通过采购(而不是通过调用sudo apt-get install)在 Ubuntu 上编译 Nginx,因为这是我被指示要做的。我知道 Nginx 正在运行,因为当我告诉它停止时,出现“无法连接:Firefox 无法在...处建立与服务器的连接”,我只是陷入了这个奇怪的问题。

如果提供的信息不足,请告诉我,如果是,我应该添加哪些信息以使帖子更清晰!谢谢!

【问题讨论】:

  • 您是否尝试将location / { root html; ... 中的html 替换为您的index.html 文件所在目录的路径(例如:/home/user/Documents/MyProject)?
  • 是的,我所做的就是转到目录中的 index.html 文件并在其上运行 pwd 并用此 pwd 值替换旧路径。基本上看起来像location / { root /home/parallels/Desktop/MyProject/nginx/html;
  • 另外我不知道这是否要编辑,但有一个名为 nginx.conf.default 的文件看起来就像 nginx.conf。唯一的区别是每次我构建/制作 nginx 时,这个 nginx.conf.default 都会回到它的默认值(所以即使我改变了路径,如果我想制作新的二进制文件并构建 nginx,我的改变也会消失)。你认为我应该花更多时间关注这个 nginx.conf.default 文件并在那里进行更改吗?
  • 我认为,你应该配置Nginx.conf文件,而不是Nginx.conf.default
  • 好吧,我就是这么想的。是的,不幸的是,我必须在线克隆一个 repo 并利用它,而那个 repo 是 Nginx 1.8。我查看了您提到的链接,但似乎正确答案中的任何建议都不适用,因为它们对我来说都不是问题。不过感谢您的链接!

标签: apache ubuntu nginx path http-status-code-404


【解决方案1】:

确保您的 nginx.conf 文件看起来有点如下所示:

worker_processes  1;

events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        location / {
            root   /home/user_name/Documents/your_proj; # You need to provide here the path of your "index.html" file
            index  index.html index.htm;
        }
}

如上配置所示,您的目录/home/user_name/Documents/your_proj 下必须有一个index.htmlindex.htm 文件。

【讨论】:

  • 嗯,我有类似的东西,因为我的 index.html 位于我在root 旁边提供的路径中。如果重要的话,我使用的是绝对路径,而不是相对路径。 server_name 能有所作为吗? Mines 只是声明 localhost,我不太确定它表示什么。
  • 是的,有一个127.0.0.1 localhost。我只是很困惑为什么会显示这个错误......它一定是非常愚蠢但我找不到它。
  • 我现在只使用一个 html 文件。我必须使用 nginx 构建 FAST CGI,但现在我无法正确运行 nginx,所以还没有查看 FAST CGI 部分。
  • 我尝试从您的“您应该检查这是否适合您:”中复制粘贴,但它仍然导致相同的 404 Not Found。这可能与PHP有关吗?在研究这个问题时,这似乎是一个常见的事情,但我使用的是 index.html,一个 html 文件,所以觉得它不太适用。
  • 检查 NginX 以查看它使用的配置。首先停止 NginX nginx -s stop 然后运行 ​​nginx -T > nginxT-showConfigs.txt 然后在文本编辑器中打开该 txt 文件,如 nano nginxT-showConfigs.txt 以查看它读取的每个文件,以及每个文件的内容,按照它们被读取的顺序。注意:-t 测试 nginx.conf,但 -T (capitol) 输出配置,然后测试 nginx.conf。
猜你喜欢
  • 2021-02-12
  • 1970-01-01
  • 2020-09-25
  • 1970-01-01
  • 1970-01-01
  • 2017-05-16
  • 2016-03-17
  • 2017-09-19
  • 2017-08-02
相关资源
最近更新 更多