解决访问 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/

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

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

cd /usr/local/nginx/
mkdir html

cd /usr/local/nginx/html

touch index.html

# 在文件内加入 html 代码
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

        location / {
            root   /root/c_nginx_module/html; # 改这里
            index  index.html index.htm;
        }

然后重启就可以了

更多推荐