nginx 静态目录配置规则

经常配了nginx静态目录,死活访问不了,每次访问404.查看文档后,发现nginx配置静态目录使
用以下规则

假如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/目录


文章转自:http://powertech.iteye.com/blog/2180942

猜你喜欢

转载自assen.iteye.com/blog/2356503