Nginx中关于root与alias的区别
server {
listen 80;
autoindex off;
server_name image.test.com;
access_log c:/access.log combined;
index index.html index.htm index.jsp index.php;
#error_page 404 /404.html;
if ( $query_string ~* ".*[\;'\<\>].*" ){
return 404;
}
location ~ /(mmall_fe|mmall_admin_fe)/dist/view/* {
deny all;
}
location /image {
alias D:\ftp\image;
add_header Access-Control-Allow-Origin *;
}
}
Case 1. 当请求image.test.com/image/1.jpg 时
alias D:\ftp\image; 文件将会被映射成D:\ftp\image\1.jpg
root D:\ftp\image; 文件将会被映射成D:\ftp\image\image\1.jpg.
错误日志:2017/07/30 15:29:10 [error] 8276#7424: *108 CreateFile() "D:\ftpfile\image/image/1.jpg" failed (3: The system cannot find the path specified), client: 127.0.0.1, server: image.imooc.com, request: "GET /image/1.jpg HTTP/1.1", host: "image.imooc.com"
Case 2. 当请求image.test.com/image/img/1.jpg 时
alias D:\ftp\image; 文件将会被映射成D:\ftp\image\img\1.jpg
错误日志:2017/07/30 15:17:53 [error] 9120#1920: *99 CreateFile() "D:\ftpfile\image/img/1.jpg" failed (3: The system cannot find the path specified), client: 127.0.0.1, server: image.imooc.com, request: "GET /image/img/1.jpg HTTP/1.1", host: "image.imooc.com"
root D:\ftp\image; 文件将会被映射成D:\ftp\image\image\img\1.jpg.
错误日志:2017/07/30 15:25:15 [error] 7604#5164: *103 CreateFile() "D:\ftpfile\image/image/img/1.jpg" failed (3: The system cannot find the path specified), client: 127.0.0.1, server: image.imooc.com, request: "GET /image/img/1.jpg HTTP/1.1", host: "image.imooc.com"
Case 3. 当请求image.test.com/img/image/img/1.jpg 时
alias D:\ftp\image; 文件将会被映射成${NGINX_HOME}\html\img\image\img\1.jpg
错误日志:2017/07/30 15:19:21 [error] 9120#1920: *101 CreateFile() "D:\software\nginx-1.10.2/html/img/image/img/1.jpg" failed (3: The system cannot find the path specified), client: 127.0.0.1, server: image.imooc.com, request: "GET /img/image/img/1.jpg HTTP/1.1", host: "image.imooc.com"
root D:\ftp\image; 文件将会被映射成${NGINX_HOME}\html\img\image\img\1.jpg
错误日志:2017/07/30 15:29:51 [error] 8276#7424: *108 CreateFile() "D:\software\nginx-1.10.2/html/img/image/img/1.jpg" failed (3: The system cannot find the path specified), client: 127.0.0.1, server: image.imooc.com, request: "GET /img/image/img/1.jpg HTTP/1.1", host: "image.imooc.com"
总结:
1. 首先对URL进行映射,如果成功,对location中的路径进行重定向。如果失败,重定向到默认的root指定的路径,即html文件夹。
2. location配置成alias,进行简单字符串替换。location配置成root,只代表根路径,实际还需要拼接上请求路径。