nginx中root和alias指令的解释

1 基本信息

功能均为将url映射为文件路径,返回静态文件内容

格式

alias path
root path

2 区别

  • root会映射完整url,会将location匹配的部分,追加到path后面,即,root指定web的家目录,在定义location的时候,文件的绝对路径等于 root+location

  • alias:定义路径别名,会把访问的路径重新定义到其指定的路径,文档映射的另一种机制

  • alias会出现在location上下文中,root可以出现在http,server,location,if in location

  • alias无默认值,root默认值为root html

3 示例

[root@centos8 conf.d]#cat /apps/nginx/conf.d/root_alias.conf
server {
    server_name path.test.com;
    root /data/nginx;
    error_log logs/myerror.log info;
    location /root {
        root /data/nginx/html;   # root指令会将 location 中的 /root 追加到 /data/nginx/html 路径后面,所以路径是:/data/nginx/html/root
    }

    location /alias {
        alias /data/nginx/html;   # alias指令会使用 /data/nginx/html 替换掉 location 中定义的 /alias 路径
    }

    location ~ /root/(\w+\.txt) {
        root /data/nginx/html/first/$1;  # 实际访问的是 /data/nginx/html/first/1.txt/root/1.txt
    }

    location ~ /alias/(\w+\.txt){
        alias /data/nginx/html/first/$1;  # alias指令会使用 /data/nginx/html/first/$1 替换掉 /alias/(\w+\.txt)
    }

    location /RealPath/ {
        alias /data/nginx/html/realpath/;
        return 200 '$request_filename:$document_root:$realpath_root\n';
    }
}
[root@centos8 conf.d]#

# 配置验证
[root@centos8 conf.d]#nginx -t
nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /apps/nginx/conf/nginx.conf test is successful
[root@centos8 conf.d]#
[root@centos8 conf.d]#nginx -s reload
[root@centos8 conf.d]#mkdir /data/nginx/html/first -pv
mkdir: created directory '/data/nginx/html'
mkdir: created directory '/data/nginx/html/first'
[root@centos8 conf.d]#echo "This index.html test page" > /data/nginx/html/index.html
[root@centos8 conf.d]#echo "This is a 1.txt" > /data/nginx/html/first/1.txt
[root@centos8 conf.d]#cat /data/nginx/html/first/1.txt
This is a 1.txt
[root@centos8 conf.d]#cat /data/nginx/html/index.html
This index.html test page

测试1

[root@centos8 conf.d]#curl path.test.com/root/
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.18.0</center>
</body>
</html>
[root@centos8 conf.d]#


# 与第一个匹配 location /root
# 因为是root指令,所以/data/nginx/html后面又加上了location中的root.因为后面有反斜杠,所以加上了index.html
# 所以实际访问的是 /data/nginx/html/root/index.html,而这个路径是不存在的

测试2

[root@centos8 conf.d]#curl path.test.com/root/1.txt
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.18.0</center>
</body>
</html>
[root@centos8 conf.d]#

# 与location ~ /root/(\w+\.txt) 匹配
# 因为是root指令,会在/data/nginx/html/first/1.txt,后面加上匹配到的root/1.txt
# 实际访问的地址
/data/nginx/html/first/1.txt/root/1.txt,而这个路径也是不存在的

测试3

[root@centos8 conf.d]#curl path.test.com/alias/
This index.html test page
[root@centos8 conf.d]#

# 匹配到了 location /alias 这个匹配项
# alias 指令会使用 /data/nginx/html 替换掉 /alias,所以 访问了 /data/nginx/html/index.html 得到了默认的首页

测试4

[root@centos8 conf.d]#curl path.test.com/alias/1.txt
This is a 1.txt
[root@centos8 conf.d]#

# 匹配到了 location ~ /alias/(\w+\.txt)这个匹配项
# alias 指令会使用 /data/nginx/html/first/$1 替换掉 /alias/1.txt,所以访问到了/data/nginx/html/first/1.txt
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在Nginxrootalias是两个用于定义服务器文件路径的指令,它们的作用略有不同。 root指令用于定义服务器文件系统根目录的路径,例如: ``` server { listen 80; server_name example.com; root /var/www/example.com; ... } ``` 在上面的例子root指令定义了服务器上example.com域名的根目录是/var/www/example.com。如果用户请求example.com/abc.html,Nginx会在文件系统寻找/var/www/example.com/abc.html文件来返回给用户。 而alias指令则用于将一个URL路径映射到服务器文件系统的某个具体文件或目录,例如: ``` location /images/ { alias /var/www/images/; ... } ``` 在上面的例子alias指令定义了当用户请求/images/路径时,Nginx会将该请求映射到服务器文件系统的/var/www/images/目录下,并返回该目录对应的文件或子目录。 因此,rootalias的区别在于它们分别用于定义不同的文件路径映射方式,root指令定义了服务器的根目录路径,而alias指令则可以将URL路径映射到任意文件或目录。 ### 回答2: 在Nginxrootalias都是用于指定服务器文件目录的指令,但它们有着不同的作用及使用场景。 首先,root指令是用于指定服务器的根目录。当客户端请求的URL路径与root配置指令相匹配时,Nginx服务器将会在root目录下寻找对应的文件。例如,当root /usr/share/nginx/html;时,访问http://example.com/index.html将会去到/usr/share/nginx/html/index.html。 不过,当URL路径带有一些前缀时,root指令就显得有些不够灵活了。例如,当访问http://example.com/blog/index.html时,此时root /usr/share/nginx/html;就无法确切地定位到实际的index.html文件,因为它只能去/usr/share/nginx/html/目录下寻找文件,而无法解释"/blog"前缀。这时候,就需要使用到alias指令alias指令是用于指定服务器文件目录及URL路径间的映射关系,能够将URL路径特定的部分映射到其他位置,同时保留其他部分的路径。例如,当alias /data/nginx/;时,访问http://example.com/files/picture.jpg将会被映射到/data/nginx/files/picture.jpg。 相比而言,虽然root指令简单易用,但它无法支持URL路径的重写,因此在实际的开发alias指令可能更为常用。而使用时,则需在两个指令之间进行明确的区分和合理配置。 ### 回答3: Nginx是一款常用的Web服务器,它有两个重要的配置指令rootalias。这两个指令作用是为请求的URL(统一资源定位符)指定相应的文件路径。下面我将详细解释这两个指令的区别。 root指令root指令指定了请求URL的根路径,它会将请求与根目录进行匹配,并找到对应的文件来响应请求。root指令是与location块一起使用的,其的location块指定了请求URL的匹配规则。 例如,如果我们在Nginx配置文件使用以下指令: location /images/ { root /var/www; } 那么当用户请求URL为http://example.com/images/logo.png 时,Nginx会在/var/www/images/下查找logo.png文件,并将其返回给用户。 alias指令alias指令root指令有些类似,也是将请求URL与文件路径进行匹配。不同之处在于,alias不会将请求URL的部分路径作为文件路径的一部分,而是将请求URL的部分路径替换为指定的文件路径。 例如,我们在Nginx配置文件使用以下指令: location /images/ { alias /var/www/images/; } 当用户请求URL为http://example.com/images/logo.png 时,Nginx会在/var/www/images/下查找logo.png文件,并将其返回给用户。这里的区别在于,如果我们使用root指令Nginx会在/var/www/images/images/下查找logo.png文件。 总结: root指令alias指令都是与location块一起使用,用于将请求URL与文件路径进行匹配。root指令将请求URL的路径与根目录进行匹配,而alias指令将请求URL的路径部分替换为指定的文件路径。理解这两个指令的区别非常重要,可以根据实际需求合理地配置Nginx,提高Web服务器的性能和安全性。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值