博客
关于我
nginx中配置root和alias的区别
阅读量:791 次
发布时间:2023-02-15

本文共 1514 字,大约阅读时间需要 5 分钟。

初识:

Nginx中的rootalias都可以用来代理静态资源,但它们的使用场景和效果有所不同。理解这两者的区别对于优化服务器配置至关重要。

例如,在以下配置中:

location / { root html; index index.html index.htm;}

表示当访问服务器的根路径/时,Nginx会代理服务器实际路径html目录下的文件,默认访问indexindex.htmlindex.htm文件。

nginx在location中配置root

location模块中使用root时,访问路径的组合方式与alias不同。例如:

location /yunweijia/ { root html/ceshi/;}

这里的root指定了服务器实际路径html/ceshi/,而location指定了Nginx处理请求的路径/yunweijia/。因此,访问/yunweijia/index.html会实际转发到html/ceshi/yunweijia/index.html

为了验证这一点,可以创建对应的文件目录:

mkdir -pv html/ceshi/yunweijiamkdir: created directory 'html/ceshi'mkdir: created directory 'html/ceshi/yunweijia'

然后将内容写入html/ceshi/yunweijia/index.html文件中:

echo "my name is yunweijia" > html/ceshi/yunweijia/index.html

最后,使用curl验证访问效果:

curl http://10.0.0.20/yunweijia/index.html

访问结果应为my name is yunweijia,说明root配置正确。

nginx在location中配置alias

alias的配置方式与root有所不同。例如:

location /yunweijia_1/ { alias html/ceshi/yunweijia_1/;}

这里的alias表示当访问路径为/yunweijia_1/时,Nginx会将请求转发到html/ceshi/yunweijia_1/目录下,而与location指定的路径/yunweijia_1/无关。

为验证这一点,同样需要创建对应的文件目录:

mkdir -pv html/ceshi/yunweijia_1mkdir: created directory 'html/ceshi/yunweijia_1'

将内容写入html/ceshi/yunweijia_1/index.html文件中:

echo 'my name is yunweijia_1' > html/ceshi/yunweijia_1/index.html

再次使用curl验证:

curl http://10.0.0.20/yunweijia_1/index.html

访问结果应为my name is yunweijia_1,说明alias配置正确。

总结:

rootalias在Nginx配置中各有不同:

1. 当使用root时,访问路径是root指定的路径加上location指定的路径。 2. 当使用alias时,访问路径完全由alias指定的路径决定,与location无关。

选择root还是alias,取决于你的具体需求。如果需要将多个路径映射到同一个物理目录下,alias可能更为合适。而如果需要将访问路径与物理目录一一对应,root则是更好的选择。

转载地址:http://gqcfk.baihongyu.com/

你可能感兴趣的文章
Net操作Excel(终极方法NPOI)
查看>>
Net操作配置文件(Web.config|App.config)通用类
查看>>
net网络查看其参数state_dict,data,named_parameters
查看>>
Net连接mysql的公共Helper类MySqlHelper.cs带MySql.Data.dll下载
查看>>
NeurIPS(神经信息处理系统大会)-ChatGPT4o作答
查看>>
neuroph轻量级神经网络框架
查看>>
Neutron系列 : Neutron OVS OpenFlow 流表 和 L2 Population(7)
查看>>
new Blob()实现不同类型的文件下载功能
查看>>
New Concept English three (35)
查看>>
NEW DATE()之参数传递
查看>>
New Journey--工作五年所思所感小记
查看>>
new Queue(REGISTER_DELAY_QUEUE, true, false, false, params)
查看>>
New Relic——手机应用app开发达人的福利立即就到啦!
查看>>
new work
查看>>
new 一个button 然后dispose,最后这个button是null吗???
查看>>
NewspaceGPT的故事续写能力太强了
查看>>
NewspaceGPT绘制时序图
查看>>
NewspaceGPT绘制类图
查看>>
new一个对象的过程
查看>>
new和delete用法小结
查看>>