最近在使用Nginx配置一个图片静态服务器来通过http请求访问图片,在配置的时候,发现遇到访问有404的问题,结果发现是在配置路径的时候,和路径属性是root还是alias的有关。
nginx配置文件
server {
listen 88;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location /images/ {
alias /var/www/app/static/;
autoindex_localtime on; # 列表目录显示本地时间
autoindex on;
}
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
}
下面就是root和alias的配置的区别
如果我们是在location中配置子目录/static/ 并且 root 后面加上文件地址
location /static/ {
root /var/www/app/static/;
autoindex on;
}
这个情况解析的文件地址就会是 /var/www/app/static/static
如果我们是在location中配置子目录/static/ 并且 alias 后面加上文件地址
location /static/ {
alias /var/www/app/static/;
autoindex on;
}
这个情况解析的文件地址就会是 /var/www/app/static/
如果我们location配置的是根目录,使用root和alias效果是一样的,如果我们配置的location是子目录,这个时候我们就要考虑是使用root还是alias了。