nginx 静态目录配置规则
1、子目录匹配
如下配置
location / {
root /data/www;
}
访问http://127.0.0.1/时,配匹配/data/www
访问http://127.0.0.1/images时,配匹配/data/www/images
访问http://127.0.0.1/images/1.jpg时,配匹配/data/www/images/1.jpg
也就是说,地址栏里"/"后的路径是直接匹配目录data/www/下的路径
location /images/ {
root /data/www;
}
访问http://127.0.0.1/images时,配匹配/data/www/images
也就是说,地址栏里/images,直接匹配了/data/www的子目录.
经常出问题的是,location里的url随意配了一个名字,如/xxx,但是对应的/data/www目录
下并没有该/data/www/xxx子目录,一访问就404
2、重复路径匹配规则
server {
location / {
root /data/www;
}
location /images/ {
root /data;
}
}
访问URL http://localhost/images/example.png,将会匹配第二个/images/规则,
虽然也可以匹配location /规则,但nginx默认会选择最长前缀去匹配当前URL,也就是
第二个配置会生效,访问/data/images/目录,而不是/data/www/images/目录
server {
listen 8888;
server_name localhost;
location / {
root E:/nginx-1.13.6/html/dist;
try_files $uri $uri/ /index.html;
index index.html index.htm;
}
location /server/ {
proxy_pass http://xxxxxxx:8080/;
}
}
浙公网安备 33010602011771号