nginx php root,nginx关于root与alias的区别

这篇文章主要介绍了关于nginx关于root与alias的区别,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下

结论

配置demo:location xxx {

root yyy

}

浏览器访问 xxx,实际访问的是 yyy/xxx

浏览器访问 xxx/abc.html,实际访问的是 yyy/xxx/abc.html

浏览器访问 xxx/ccc/abc.html,实际访问的是 yyy/xxx/ccc/abc.html

结论: root属性,会把root的值(这里是yyy)加入到访问路径(locaition)之前

配置demo:locaiton xxx {

# alias必须以 / 结束,否则无效

alias yyy/

}

浏览器访问 xxx,实际访问的是 yyy

浏览器访问 xxx/abc.html,实际访问的是 yyy/abc.html

浏览器访问 xxx/ccc/abc.html,实际访问的是 yyy/ccc/abc.html

结论:alias属性,会把alias的值(这里是yyy)替代访问路径匹配的部分(这里是xxx)

示例

nginx的目录结构如下:nginx/

-html/

-index.html

-logs/

- access.log

-conf/

-nginx.conf

1) 这种配置,http://localhost:8086/access.log,能看到 nginx/logs/access.log,但就别指望能访问 html目录下的文档了server {

listen 8086;

server_name localhost;

location / {

root logs;

}

}

2) 这种配置,访问 http://localhost:8086/log/access.log,能看到 nginx/logs/access.log;

访问 http://localhost:8086/, 能看到 nginx/html/index.htmlserver {

listen 8086;

server_name localhost;

location / {

root html;

index index.html index.htm;

}

# 配置成 location /log/ 或 location /log 都可以

location /log/ {

# 不能写成logs, 必须已 / 结束

alias logs/;

# 以下配置没用也可以,只是方便你输入 localhost:8086/log/ 后能,看到nginx/logs/目录下的所有文件

autoindex on;

}

}

3) 这种配置,访问 http://localhost:8086/logs/access.log,能看到 nginx/logs/access.log;

访问 http://localhost:8086/, 能看到 nginx/html/index.htmlserver {

listen 8086;

server_name localhost;

# http://localhost:8086/ 访问的是

# nginx/html/ (然后会自动显示 index.html 或 index.htm,如果存在这两个文件之一)

# 啰嗦的注释: nginx/html(html是root的值)/(/是location的值)

location / {

root html;

index index.html index.htm;

}

# http://localhost:8086/logs/ 访问的是

# nginx/./logs/

# .是root的值,logs是location的值

# 请与第4种错误配置进行比较,深入理解root属性

location /logs/ {

# 写成./也可以

root .;

}

}

4) 错误的配置server {

listen 8086;

server_name localhost;

location / {

root html;

index index.html index.htm;

}

# 这样子配置是错的, 请与第三种配置比较一下

# 关键点:root属性会把root的值加入到最终路径之前

# 即: http://localhost:8086/logs/access.log访问的是:

# nginx/logs/logs/access.log

# 因为: nginx/logs(root的值)/logs(locaition的值)/access.log,

location /logs/ {

root /logs/;

}

}

节选:https://www.cnblogs.com/zhang... 这段话:

root属性指定的值是要加入到最终路径的,所以访问的位置变成了 root的值/locaiton的值。而我不想把访问的URI加入到路径中。所以就需要使用alias属性,其会抛弃URI,直接访问alias指定的位置

以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!

相关推荐:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值