Nginx虚拟目录alias和root目录
来自ling
nginx是通过alias设置虚拟目录,在nginx的配置中,alias目录和root目录是有区别的: 1)alias指定的目录是准确的,即location匹配访问的path目录下的文件直接是在alias目录下查找的;
2)root指定的目录是location匹配访问的path目录的上一级目录,这个path目录一定要是真实存在root指定目录下的;
3)使用alias标签的目录块中不能使用rewrite的break(具体原因不明);另外,alias指定的目录后面必须要加上"/"符号!!
4)alias虚拟目录配置中,location匹配的path目录如果后面不带"/",那么访问的url地址中这个path目录后面加不加"/"不影响访问,访问时它会自动加上"/";
但是如果location匹配的path目录后面加上"/",那么访问的url地址中这个path目录必须要加上"/",访问时它不会自动加上"/"。如果不加上"/",访问就会失败!
5)root目录配置中,location匹配的path目录后面带不带"/",都不会影响访问。
所以,一般情况下,在nginx配置中的良好习惯是:
1)在location /中配置root目录;
2)在location /path中配置alias虚拟目录。
server {
listen 80;
server_name www.wangshibo.com;
index index.html index.php index.htm;
access_log /usr/local/nginx/logs/image.log;
location / {
root /var/www/html;
}
location /haha { //匹配的path目录haha不需要真实存在alias指定的目录中
alias /var/www/html/ops/; //后面的"/"符号一定要带上
rewrite ^/opp/hen.php(.*)$ /opp/hen.php?s=$1 last;
# rewrite ^/opp/(.*)$ /opp/hen.php?s=$1 last;
}
location /wang { //匹配的path目录wang一定要真实存在root指定的目录中(就/var/www/html下一定要有wang目录存在)
root /var/www/html;
}
}
server {
listen 80;
server_name www.kevin.com;
access_log /data/nginx/logs/www.kevin.com-access.log main;
error_log /data/nginx/logs/www.kevin.com-error.log;
location / {
root /data/web/kevin;
index index.php index.html index.htm;
}
location /document/ {
alias /data/web/document/;
}
}