为什么nginx位置会返回404错误?

时间:2017-10-20 08:39:57

标签: nginx

这是我的/usr/local/nginx/conf/nginx.conf:

server {
        listen       8080;
        server_name  localhost;

        location / {
            root   html;
            index  index.html index.htm;
        }

        location /test{
                root /var/www/test;
                index index.html index.htm;
        }

当我输入时:http://localhost:8080/:我收到了nginx欢迎页面:好

当我输入http://localhost:8080/test时:我收到了404错误 (我在/ var / www / test中创建了index.html和index.htm)

PS我确实重新载入:

/usr/local/nginx/sbin/nginx -s reload

2 个答案:

答案 0 :(得分:1)

/test删除为root指令。您只需指出/var/www

即可
server {
    listen       8080;
    server_name  localhost;

    location / {
        root   html;
        index  index.html index.htm;
    }

    location /test {
        root /var/www;
        index index.html index.htm;
    }

答案 1 :(得分:0)

这里的问题是,nginx 在我们提供的根目录中搜索我们添加为位置的文件夹,在这种情况下,位置 \test 和根目录 /var/www/test。由于 /test 不在 /var/www/test 中,因此显示错误。如果改用 /var/www,它将正常工作,因为 nginx 使用 root + location。

要确认,您遇到了同样的问题,请检查 nginx 错误日志。如果您没有更改日志位置,那么以下命令将完成这项工作。

sudo tail -n 20 /var/log/nginx/error.log

如果它显示如下消息,您可以确认问题相同。

[error] 7480#7480: *1 open() "/var/www/test/test" failed (2: No such file or directory), client: XXX.XXX.XX.XX, server: , request: "GET /app HTTP/1.1", host: "XX.XX.XXX.XX"

要解决此问题,请更改以下代码,


server {

    listen       8080;
    server_name  localhost;

    location / {
        root   html;
        index  index.html index.htm;
    }

    location /test {
        root /var/www;
        index index.html index.htm;
    }

修改后重启nginx服务

sudo systemctl restart nginx