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

为什么nginx位置返回404错误?,nginx,Nginx,这里是my/usr/local/nginx/conf/nginx.conf: server { listen 8080; server_name localhost; location / { root html; index index.html index.htm; } location /test{ root

这里是my/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;
        }
当我输入::时,我得到了nginx欢迎页面:好

当我输入:我得到一个404错误 (我在/var/www/test中创建了index.html和index.htm)

PS我确实重新加载了:

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

删除根指令的
/test
。您只需指出
/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;
    }

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

要确认,您面临同样的问题,请查看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

我当然重新装弹了。您能告诉我更多关于权限和所有者的信息吗?NGinx服务器正在与哪个用户一起运行?NGinx正在与root用户一起运行。nginx似乎还可以:当我将端口8080更改为8089时,我需要转到lcoalhost:8089。奇怪的是:当我对指令“location/”进行注释时,我仍然可以转到/locationit,看起来nginx只路由/。无论我放置什么,比如location/zaza,转到/zaza都会给我404错误