nginx location root alias proxy_pass

root与alias主要区别在于nginx如何解释location后面的uri,这会使两者分别以不同的方式将请求映射到服务器文件上。
root的处理结果是:root路径+location路径
alias的处理结果是: 使用alias路径替换location路径

alias是一个目录别名的定义,root则是最上层目录的定义。

root实例

location ^~ /test/ {
     root /www/root/html/;
}

如果一个请求的URI是/test/a.html时,web服务器将会返回服务器上的/www/root/html/test/a.html的文件。

alias实例

location ^~ /test/ {
 alias /www/root/html/new_test/;
}

如果一个请求的URI是/test/a.html时,web服务器将会返回服务器上的/www/root/html/new_test/a.html的文件。注意这里是new_test,因为alias会把location后面配置的路径丢弃掉,把当前匹配到的目录指向到指定的目录。

注意:

  1. 使用alias时,目录名后面一定要加"/"。
  2. alias在使用正则匹配时,必须捕捉要匹配的内容并在指定的内容处使用。
  3. alias只能位于location块中。(root可以不放在location中)

proxy_pass

在nginx中配置proxy_pass时,如果在proxy_pass后面的url加/,相当于是绝对根路径,则nginx不会把location中匹配的路径部分代理走;如果没有/,则会把匹配的路径部分给代理走

# 第一种
location  /abc
    {
        proxy_pass http://xxx.xxx.xxx.xxx:83/;
        proxy_set_header Host   $host;
        proxy_set_header X-Real-IP      $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
 
# 结论:会被代理到http://xxx.xxx.xxx.xxx/index.html 这个url 
 
# 第二种(相对于第一种,最后少一个 /)
location  /abc
    {
        proxy_pass http://xxx.xxx.xxx.xxx:83;
        proxy_set_header Host   $host;
        proxy_set_header X-Real-IP      $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
 
# 结论:会被代理到http://xxx.xxx.xxx.xxx/abc/index.html 这个url
 
第三种:
location  /abc
    {
        proxy_pass http://xxx.xxx.xxx.xxx:83/linux/;
        proxy_set_header Host   $host;
        proxy_set_header X-Real-IP      $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
 
# 结论:会被代理到http://xxx.xxx.xxx.xxx/linux/index.html 这个url。

下面我们验证一下:

环境准备:nginx的配置如下

主配置文件:

server {
    listen       80;
    server_name  localhost;
 
    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;
 
  
    location  /abc
    {
        proxy_pass http://xxx.xxx.xxx.xxx:83/;
        proxy_set_header Host   $host;
        proxy_set_header X-Real-IP      $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

副配置文件,上面是代理到83端口了,我们看一下83端口的配置为文件,之前做其他实验,现在我们就在这个上面简单修改下。

[root@master conf.d]# cat 83.conf 
server
{
   listen 83;
   server_name xxx.xxx.xxx.xxx;
   root /usr/share/nginx/83;
}

查看静态文件地址:

[root@master nginx]# tree 83/
83/
├── abc
│   └── index.html
├── index.html
└── linux
    └── index.html
 
[root@master nginx]# cat 83/index.html 
83
 
[root@master nginx]# cat 83/abc/index.html 
83  abc
 
[root@master nginx]# cat 83/linux/index.html 
linux

第一种:当我们http://xxx.xxx.xxx.xxx/abc/index.html时候,页面出现的是83

第二种:当我们http://xxx.xxx.xxx.xxx/abc/index.html时候,页面出现的是83 abc的内容

第三种:当我们http://xxx.xxx.xxx.xxx/abc/index.html时候,页面出现的是linux的内容

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值