root 与alias指令
Syntax: alias path; Default: — Context: location Syntax: root path; Default: root html; Context: http, server, location, if in location
功能:将URI映射为文件路径,以静态文件返回
差别:root 会将完整的URI映射进文件路径中;alias只会将location后面的文件映射进location中
示例
server { server_name root.com; error_log logs/root_error.log info; access_log logs/root.log main; location /root { root html; #表示映射本地html/root } location /alias { alias html; #访问root.com/alias 命中本地/data/web/html } location ~ /root/(w+.txt) { root html/first/$1;#访问这个root.com/root/1.txt,命中本地的/data/web/html/first/1.txt/root/1.txt } location ~ /alias/(w+.txt) { alias html/first/$1; # 访问这个curl root.com/alias/1.txt,命中本地的/data/web/html/first/1.txt } }
日志
[root@python vhast]# curl root.com/root <html> <head><title>404 Not Found</title></head> <body> <center><h1>404 Not Found</h1></center> <hr><center>nginx/1.15.9</center> </body> </html> [root@python vhast]# curl root.com/root/1.txt <html> <head><title>404 Not Found</title></head> <body> <center><h1>404 Not Found</h1></center> <hr><center>nginx/1.15.9</center> </body> </html> [root@python vhast]# curl root.com/alias <html> <head><title>301 Moved Permanently</title></head> <body> <center><h1>301 Moved Permanently</h1></center> <hr><center>nginx/1.15.9</center> </body> </html> [root@python vhast]# curl root.com/alias/1.txt first 日志 192.168.183.4 - - [10/Jul/2019:16:25:01 +0800] "GET /root HTTP/1.1" 404 153 "-" "curl/7.29.0" "-" "/root" "-" "/data/web/html" "/data/web/html/root" 192.168.183.4 - - [10/Jul/2019:16:30:16 +0800] "GET /root/1.txt HTTP/1.1" 404 153 "-" "curl/7.29.0" "-" "/root/1.txt" "-" "/data/web/html/first/1.txt" "/data/web/html/first/1.txt/root/1.txt" 192.168.183.4 - - [10/Jul/2019:16:31:51 +0800] "GET /alias HTTP/1.1" 301 169 "-" "curl/7.29.0" "-" "/alias" "-" "/data/web/html" "/data/web/html" 192.168.183.4 - - [10/Jul/2019:16:32:09 +0800] "GET /alias/1.txt HTTP/1.1" 200 6 "-" "curl/7.29.0" "-" "/alias/1.txt" "-" "/data/web/html/first/1.txt" "/data/web/html/first/1.txt
三个变量
request_filename :待访问文件的完整路径
document_root : 由URI和root/alis规则生成的文件夹路径
realpath_root:将document_root中的软连接等换成真实路径
[root@python vhast]# cat root.conf server { server_name root.com; error_log logs/root_error.log info; access_log logs/root.log main; location /root { root html; #表示映射本地html/root } location /alias { alias html; #访问root.com/alias 命中本地/data/web/html } location ~ /root/(w+.txt) { root html/first/$1;#访问这个root.com/root/1.txt,命中本地的/data/web/html/first/1.txt/root/1.txt } location ~ /alias/(w+.txt) { alias html/first/$1; # 访问这个curl root.com/alias/1.txt,命中本地的/data/web/html/first/1.txt } location /RealPath/ { alias html/realpath/; return 200 "$request_filename:$document_root:$realpath_root! "; } }
测试
root@python vhast]# curl root.com/RealPath/ /data/web/html/realpath/:/data/web/html/realpath/:!