Nginx之location若干注意事项

38 篇文章 6 订阅
7 篇文章 0 订阅

一. root与alias的区别

Nginx中可以通过root和alias两个属性指定web资源文件的路径,但二者的配置范围和解释方式是有区别的。
配置范围的差异很明确,即alias属性只能用于location配置段,而root属性还可以用于server、http等配置段。
但root属性和alias属性的解释方式的差异则有些隐蔽,理解有误容易掉坑,造成404错误等。

[root]
语法:root path
默认值:root html
配置段:http、server、location、if

[alias]
语法:alias path
配置段:location

示例:

server {
	listen 80;
	server_name www.dancen.com;
	root /home/site;

	location /.well-known {
		root /home/dancen;
	}

	location /src {
		alias /mnt/volume/site/;
		
		location ~* \.(htm|html)$ {
			expires 300s;	
		}
		
		location ~* \.(jpg|jpeg|png|gif|svg|webp|bmp|ico)$ {
			expires 360d;
		}
	}
	
	location /service {
		proxy_redirect off;
		proxy_pass http://127.0.0.1:8080;
		proxy_http_version 1.1;
		proxy_set_header Upgrade $http_upgrade;
		proxy_set_header Connection "upgrade";
		proxy_set_header Host $http_host;
	}
}

示例解释:
示例中,location属性将域名下不同的url引导到了不同的位置。

  1. http://www.dancen.com/test
    没有匹配任何已定义的location,
    此时Nginx将被引导至server>root属性定义的/home/site目录寻找文件,例如对于链接:
    http://www.dancen.com/test/file.jpg
    其对应的文件为:
    /home/site/test/file.jpg

  2. http://www.dancen.com/.well-known
    匹配:
    location /.well-known { root /home/dancen;}
    此时Nginx将被引导至location>root属性定义的/home/dancen目录寻找文件,例如对于链接:
    http://www.dancen.com/.well-known/file.jpg
    其对应的文件为:
    /home/dancen/.well-known/file.jpg

  3. http://www.dancen.com/src
    匹配:
    location /src { alias /mnt/volume/site/;}
    此时Nginx将被引导至location>alias属性定义的/mnt/volume/site目录寻找文件,例如对于链接:
    http://www.dancen.com/src/file.jpg
    其对应的文件为:
    /mnt/volume/site/file.jpg

此处可以看到root属性和alias属性的作用非常相似,都是重新定义文件路径,例如location属性中root/alias属性定义的文件路径将会覆盖server属性中定义的文件路径。
那么,二者的差别在哪里呢?假设这里将alias改为root,那么,Nginx将被引导至location>root属性定义的/mnt/volume/site目录寻找文件,同样的链接:
http://www.dancen.com/src/file.jpg
其对应的文件为:
/mnt/volume/site/src/file.jpg

仔细对比可以看到,二者对应的文件不同了:
root属性:文件路径=root路径+location路径+url相对路径
alias属性:文件路径=alias路径+url相对路径

从字面上理解,root属性指定的文件路径是根目录,负责将链接引导至根目录,然后寻找由location属性值和url相对路径结合起来指定的文件。alias属性指定的是location属性值的别名,alias属性值会替代location属性值,负责将链接引导至别名目录,然后寻找由url相对路径指定的文件。

  1. http://www.dancen.com/service
    匹配location /service
    此时Nginx将链接转发至本机的8080端口,即Nginx作为代理服务器使用。

二. location的嵌套使用

正如上面的例子,location属性也可以定义在location属性中,从而实现location属性的嵌套使用。例如,第一层location用于请求链接的分类,第二层location则用于给不同类型的资源文件定义不同的过期时间,在某些使用场景下,这是非常有用的。

示例:

location /src {
		alias /mnt/volume/site/;
		
		location ~* \.(htm|html)$ {
			expires 300s;	
		}
		
		location ~* \.(jpg|jpeg|png|gif|svg|webp|bmp|ico)$ {
			expires 360d;
		}
	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
nginx中,可以使用location块来指定不同的URL路径的配置。location块可以进行嵌套使用,以满足复杂的URL匹配需求。 例如,在给定的示例中,可以看到location块的嵌套使用。最外层的location块指定了路径为/src的请求的配置,其中使用了alias属性来指定了别名目录。而在/src的location块内部,又使用了两个子location块,分别匹配了.html和图片文件的请求,并进行了相应的处理,如设置了过期时间。 此外,location块的嵌套还可以用于配置反向代理。在示例中,可以看到location块内部的/service路径匹配了请求,并将请求转发至本机的8080端口,实现了Nginx作为代理服务器的功能。 因此,通过使用location块的嵌套,可以实现更灵活和复杂的URL路径配置,以满足不同的需求。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [利用nginx如何匹配多个条件](https://download.csdn.net/download/weixin_38617846/14102585)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [Nginxlocation若干注意事项](https://blog.csdn.net/Dancen/article/details/120771876)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值