解决访问 nginx 首页报错 404

发布时间:2024-04-30 09:54:53 作者:yexindonglai@163.com 阅读(62)

解决访问 nginx首页 404 的方法

正常情况下,启动nginx后访问地址 http://localhost 应该是能进入到nginx 的欢迎首页的;但是当访问后却显示了404页面,
我们当前的 html 文件路径为:/root/c_nginx_module/html/index.html

这种情况一般都是 nginx root目录(根目录)未配置好导致的,那我们只要保证根目录下有html,或者指定一个有html的根目录就行啦!

解决方案 1:在根目录加上html文件

以下通过 -t 命令查看到,nginx 的根目录是 /usr/local/nginx/

  1. ^Croot@PAw9033927:~/c_nginx_module# ./nginx -t
  2. nginx: [emerg] open() "/usr/local/nginx/conf/nginx.conf" failed (2: No such file or directory)
  3. nginx: configuration file /usr/local/nginx/conf/nginx.conf test failed

然后,我们进入到 nginx的根目录,创建一个html目录,在html目录下创建一个 index.html 文件

  1. cd /usr/local/nginx/
  2. mkdir html
  3. cd /usr/local/nginx/html
  4. touch index.html
  5. # 在文件内加入 html 代码
  6. vi index.html

然后再次启动就可以访问了

解决方案 2:指定根目录

刚刚说了,我们当前的 html 文件路径为:/root/c_nginx_module/html/index.html,那我们就把根目录指定为 /root/c_nginx_module 就行了

在 nginx.conf 配置文件中,在 location 块下将root的值改为 /root/c_nginx_module/html

  1. location / {
  2. root /root/c_nginx_module/html; # 改这里
  3. index index.html index.htm;
  4. }

然后重启就可以了

关键字Nginx