zoukankan      html  css  js  c++  java
  • Nginx配置静态资源文件404 Not Found问题解决方法

    在使用nginx做静态资源服务器时,配置完成后通过浏览器访问一直报404 Not Found错误,本人nginx配置信息如下:

    location /images/ {  
         root /mnt/upload/files;  
    }

    所有文件存放在/mnt/upload/files

    分析:

    发现是配置的问题,配置静态路径有两种方式,之前配置的是直接在URL里写根目录,而现在配置是一个有前缀的URL,所以报404 Not Found错误了。

    root配置会在配置的目录后跟上URL,组成对应的文件路径,即想访问的地址是:

    https://blog.yoodb.com/images/a.png

    nginx根据配置走的文件路径是

    /mnt/upload/files/images/a.png

    而我需要的是

    /mnt/upload/files/a.png

    而Nginx提供了另外一个静态路径配置:alias配置

    官方root配置

    Sets the root directory for requests. For example, with the following configuration
    location /i/ {
        root /data/w3;
    }
    The /data/w3/i/top.gif file will be sent in response to the “/i/top.gif” request

    官方alias配置

    Defines a replacement for the specified location. For example, with the following configuration
    location /i/ {
        alias /data/w3/images/;
    }
    on request of “/i/top.gif”, the file /data/w3/images/top.gif will be sent.

    当访问/i/top.gif时,root是去/data/w3/i/top.gif请求文件,alias是去/data/w3/images/top.gif请求,也就是说

    root响应的路径:配置的路径+完整访问路径(完整的location配置路径+静态文件)

    alias响应的路径:配置路径+静态文件(去除location中配置的路径)

    解决办法:

    location /images/ {  
         alias /mnt/upload/files/;  
    }

    注意:使用alias时目录名后面一定要加“/”;一般情况下,在location/中配置root,在location /*中配置alias

  • 相关阅读:
    ajax的优缺点
    Service层事务不能回滚的解决方式
    java---springMVC与strutsMVC的区别
    java----监听器的作用
    ActionContextCleanUp
    Struts2请求处理流程及源码分析
    Struts2的拦截器是如何使用AOP工作的
    SSH三大框架的知识题
    java--Struts中请求的过程
    java----Servlet的生命周期
  • 原文地址:https://www.cnblogs.com/ruiy/p/12581600.html
Copyright © 2011-2022 走看看