www.cnblogs.com/ruiyqinrui

开源、架构、Linux C/C++/python AI BI 运维开发自动化运维。 春风桃李花 秋雨梧桐叶。“力尽不知热 但惜夏日长”。夏不惜,秋不获。@ruiY--秦瑞

python爬虫,C编程,嵌入式开发.hadoop大数据,桉树,onenebula云计算架构.linux运维及驱动开发.

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

在使用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

posted on 2020-03-27 15:10  秦瑞It行程实录  阅读(18923)  评论(0编辑  收藏  举报
www.cnblogs.com/ruiyqinrui