nginx静态文件映射root和alias

本文探讨了使用Nginx作为图片服务器时遇到的404错误问题,并详细对比了root与alias配置的区别,最终给出了正确的配置方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用nginx做图片服务器,用root配置图片目录后期望通过image.xxx.xxx/image/xxx.jpg访问却一直报404错误,但是可以通过image.xxx.xxx/xxx.jpg却可以访问

server {
        listen       80;
        server_name  image.xxx.xxx;

        location ^~ /image {
            root /storage/images;
        }

   }

 最后发现配置静态路径的两种方式。之前静态的都是直接在URL里写根目录,所以一直没发现。加了一个有前缀的URL,就出现404问题。

查找原因

root 配置的意思是,会在root配置的目录后跟上URL,组成对应的文件路径。

例如URL http://image.xxx.xxx/image/xxx.png

最终去寻找的文件路径是/storage/images/image/xxx.png

而实际图片位置是/storage/images/xxx.png 导致图片404 

而通过http://image.xxx.xxx/xxx.png 却能实际找到图片/storage/images/xxx.png

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

alias与root区别

官方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中配置的路径) 

修改后的配置

server {
        listen       80;
        server_name  image.xxx.xxx;

        location ^~ /image {
            alias /storage/images/;
        }

   }

需要注意的是alia配置的目录后必须加 / 

通常情况下 location / 配置中用 root, location /other 使用alias

### NGINX 静态文件映射配置教程 NGINX 是一个高性能的 Web 服务器,常用于提供静态文件的服务。以下是关于如何在 NGINX 中配置静态文件映射的具体方法。 #### 1. 基本概念 - **root**: 定义的是文件系统的根目录,所有的 URI 将基于这个根目录进行查找。 - **alias**: 可以用来将某个 URI 映射到不同的物理路径上,而不改变 URL 的外观。 #### 2. 配置示例 ##### 示例 1:简单静态文件映射 假设有一个网站需要访问 `/webstudent/student` 目录中的静态文件,那么可以在 NGINX 配置中这样写: ```nginx server { listen 80; server_name example.com; location /webstudent/student/ { root /usr/local/nginx/; index index.html; } } ``` 在这个例子中,当用户访问 `http://example.com/webstudent/student/` 时,NGINX 会在 `/usr/local/nginx/webstudent/student/` 目录下寻找 `index.html` 文件[^1]。 ##### 示例 2:复杂路径重写 如果希望访问 `http://ip/image/2016/04/29/10/abc.jpg` 能够指向系统目录 `/image_data/2016/04/29/10/abc.jpg`,可以使用以下配置: ```nginx location /image/ { root /; rewrite ^/image/(.*)$ /image_data/$1 break; } ``` 这里的关键在于 `rewrite` 指令,它会把请求的 URI 改变后再交给后面的指令处理[^2]。 ##### 示例 3:避免多余的层次结构 有时会出现由于错误配置而导致额外的目录层级问题。例如下面这种配置会导致实际路径多一层 `tongsongzj`: ```nginx location /tongsongzj/ { root /usr/share/nginx/h5; index index.html; } ``` 为了修正这个问题,应该改用 `alias` 来代替 `root`: ```nginx location /tongsongzj/ { alias /usr/share/nginx/h5/; index index.html; } ``` 如此一来,预期路径就会变成 `/usr/share/nginx/h5/index.html`,而非原来的错误路径[^3]。 ##### 示例 4:完整的静态文件服务器配置 对于想要搭建一个完全开放的静态文件共享站点的情况,可以参考如下完整配置: ```nginx server { listen 80; server_name files.example.com; charset utf-8; root /data/share; location / { autoindex on; # 开启索引功能 autoindex_exact_size off; # 不精确计算文件大小 autoindex_localtime on; # 展示本地时间 auth_basic "Restricted Content"; auth_basic_user_file /etc/nginx/.htpasswd; # 设置基本认证 } } ``` 这段代码设置了基本的身份验证,并开启了目录浏览的功能,方便管理大量静态资源[^4]。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值