nginx中root与alias指令的区别
				
									
					
					
						|  | 
							admin 2025年6月30日 13:56
								本文热度 1292 | 
					
				 
				区别
1. root 设置请求的根目录,请求的路径为:root 指定的根目录拼接 location 匹配到的 uri  。
2. alias 请求的路径为:alias 中指定的目录替换 location 匹配规则 。
如以下配置中,请求 http://xxx.xxx.xxx.xxx/root_test/1.txt 会查找 /opt/web/hello/root_test/1.txt;
请求 http://xxx.xxx.xxx.xxx/alias_test/1.txt 会查找 /opt/web/hello/1.txt。
location /root_test/ {
 default_type text/plain;
 root /opt/web/hello/;
 return 200 '$request_filename';
}
location /alias_test/ {
 default_type text/plain;
 alias /opt/web/hello/;
 return 200 '$request_filename';
}
测试
# 修改配置后重新加载
vi /usr/local/nginx/conf/nginx.conf
/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx -s reload
# 测试root与alias的区别
curl http://127.0.0.1/root_test/1.txt
curl http://127.0.0.1/root_test/1.jpg
curl http://127.0.0.1/root_test/1.html
curl http://127.0.0.1/alias_test/1.txt
curl http://127.0.0.1/alias_test/1.jpg
curl http://127.0.0.1/alias_test/1.html
该文章在 2025/7/1 22:40:28 编辑过