未找到nginx位置404

未找到nginx位置404,nginx,location,http-status-code-404,nginx-location,nginx-config,Nginx,Location,Http Status Code 404,Nginx Location,Nginx Config,这是我的nginx配置文件 在default.conf上,第一个位置用于访问/usr/share/nginx/html目录,在我访问时它是正常的。 但是,当我为目录/usr/share/nginx/public directory添加一个新位置时,nginx会在我访问时返回404页 那么,怎么了?我是否误用了nginx的指令 /etc/nginx/nginx.conf user nginx; worker_processes 1; error_log /var/log/nginx/er

这是我的nginx配置文件

在default.conf上,第一个位置用于访问/usr/share/nginx/html目录,在我访问时它是正常的。 但是,当我为目录/usr/share/nginx/public directory添加一个新位置时,nginx会在我访问时返回404页

那么,怎么了?我是否误用了nginx的指令

/etc/nginx/nginx.conf

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


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

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}


/etc/nginx/conf.d/default.conf
server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/log/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    location ^~ /test/ {
        root /usr/share/nginx/public;
        index index.html index.htm;
    }
    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

以下错误块(在您的情况下)

正在告诉nginx在文件夹(root)/usr/share/nginx/public中查找目录“test”。如果该根目录中没有“test”文件夹,它将返回404。为了解决您的问题,我建议您尝试使用别名而不是。像这样:

 location ^~ /test/ {
     alias /usr/share/nginx/public;
     index index.html index.htm;
 }
另外,仅仅是为了好玩,索引指令通常可以设置,这样您就不必一直重写它。。。就这样,

 server {
     listen       80;
     server_name  localhost;

     root   /usr/share/nginx/html;
     index  index.html index.htm;

     error_page   500 502 503 504  /50x.html;

     location / { }

     location ~^/test/ {
         alias /usr/share/nginx/public;
     }

     location = /50x.html {
         root   /usr/share/nginx/html;
     }
 }
有一件事你也应该考虑…位置块越“精确”,它在配置中应该驻留的位置就越高。像这样
location=/50x.html
。在一个完美的世界中,这将被设置在顶部,就在常规服务器块设置之后

希望有帮助。

由root指令引起的错误 使用alias指令修复 其他改进 额外提示:可以设置index指令,这样您就不必重新编写它

server {
    listen       80;
    server_name  localhost;

    root   /usr/share/nginx/html;
    index  index.html index.htm;

    error_page   500 502 503 504  /50x.html;

    location / { }

    location ~^/test/ {
        alias /usr/share/nginx/public;
    }

    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}
nginx部分地根据配置中的位置匹配位置块。理想情况下,你会把你现在拥有的东西颠倒过来。nginx配置中的位置块将更高。为此,location=/50x.html也将向上移动。秩序是

  • 精确匹配=
  • 正向匹配^~/
  • 区分大小写的正则表达式~/
  • 不区分大小写的正则表达式~*
  • 路径匹配/

  • 更多关于。此外,您还可以随时查看官方文档。位置块的nginx文档

    在我的例子中,如果您托管多个站点,请检查
    服务器名称
    ,它可能指向错误。看看吧。

    当你的应用程序是vuejs时,你需要这样写,可以防止404,注意双重/测试/

       location ^~/test/ {
           alias /usr/local/soft/vuejs/;
           try_files $uri $uri/ /test/index.html;
        }
    

    你是说
    http://47.91.152.99/test/
    (带尾随的
    /
    )?文件是否位于
    /usr/share/nginx/public/test/index.html
    ?是的,带有尾随符/。index.html在目录中。
    index.html
    在哪个目录中?您的问题暗示了
    /usr/share/nginx/public
    ,但您的配置文件使用了
    /usr/share/nginx/public/test
    ,它不起作用。我仍然得到404 not found响应。访问url时,我希望nginx server访问根目录/usr/share/nginx/public。它将匹配以/images/(location/开头的请求,也匹配此类请求,但前缀较短)。服务器块的结果配置应该如下所示:server{location/{root/data/www;}location/images/{root/data;}}}在/usr/share/nginx/public下创建测试目录后,响应正常。谢谢你的回复。版主评论:“这篇编辑是针对文章作者的,作为编辑毫无意义。它应该作为评论或回答来写。”评论不够长。你更喜欢什么?
     location ^~ /test/ {
         alias /usr/share/nginx/public;
         index index.html index.htm;
     }
    
    server {
        listen       80;
        server_name  localhost;
    
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    
        error_page   500 502 503 504  /50x.html;
    
        location / { }
    
        location ~^/test/ {
            alias /usr/share/nginx/public;
        }
    
        location = /50x.html {
            root   /usr/share/nginx/html;
        }
    }
    
       location ^~/test/ {
           alias /usr/local/soft/vuejs/;
           try_files $uri $uri/ /test/index.html;
        }