Nginx加载静态资源

igxiaoshan Lv5

加载静态图片

编辑 nginx.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
 server {
listen 80;
server_name localhost;

location /image {
root /mydata/nginx/dist;
}

location /my_image {
alias /mydata/nginx/dist/picture/;
}

}

访问: http://127.0.0.1/image/test.jpg

就会加载/mydata/nginx/dist/picture/image/test.jpg这张图

加载静态文件的两种方式

  • root
1
2
3
root加载原理:url拼接root中的目录加载资源
step1 # 拿到url中的 image/test.jpg
step2 # 用root中的mydata/nginx/dist/picture/ + image/test.jpg
  • alias
1
alias加载原理: 把location中出现在alias中的字符串去掉 ,拼接到alias
此页目录
Nginx加载静态资源