nginx那点事儿——nginx中关于root、alias及localtion、proxy_pass后面是否有/的区别

nginx那点事儿——nginx中关于root、alias及localtion、proxy_pass后面是否有/的区别


一. nginx root与alias区别

root和alias都用来指定页面路径,但用法不同

  1. 使用位置不同

    [root]

    语法:root path

    默认值:root html

    配置段:http、server、location、if

    [alias]

    语法:alias path

    配置段:location

  2. 在 location 中使用时,匹配规则不一样

    使用root时,访问的url是root定义的目录加上location匹配的路径

    location /assets/ {
    
    root /git/shortUrl/dist/;
    
    }
    
    访问 http://domain/assets/a.js 时, 实际访问:http://domain/git/shortUrl/dist/assets/a.js
    

    使用alias时,访问的url直接是alias所定义的路径

    location /assets/ {
    
    alias /git/shortUrl/dist/;
    
    }
    
    访问 http://domain/assets/a.js 时,实际访问:http://domain/git/shortUrl/dist/a.js
    

二. root 或 alias 或 location 或 proxy_pass 后面加和不加 / 有啥区别

  1. root与alias后面定义的目录 加不加 / 都一样

    server {
    
      listen      8082;
    
      server_name localhost;
    
      location ^~ /root/ {
    
        root    /data/www/root/;
    
        index   index.html index.htm;
    
      }
    
      location ^~ /alias/ {
    
        alias   /data/www/alias/;
    
        index   index.html index.htm;
    
      }
    
    }
    
  2. location后面匹配的uri加不加 / 匹配有所不同

    server {
    
      listen      8082;
    
      server_name localhost;
    
      location ^~ /alias/ {
    
        echo "WITH: /";
    
      }
    
      location ^~ /alias {
    
        echo "WITHOUT: /";
    
      }
    
    }
    

    结论:

    /alias/ 只能匹配到/alias/123 /alias/abc 这种格式的url

    /alias 不仅可以匹配带/alias/123 /alias/abc 这种, 还可以匹配到/alias123 /aliasabc

    如果两个规则同时存在,由于匹配精确度越高优先级越高的原因,/alias 匹配不到 /alias/123

  3. proxy_pass后面加不加 / 匹配有所不同

    echo 'proxy test' >/date/www/proxy_pass/proxy/index.html
    
    echo 'test' > /date/www/proxy_pass/index.html
    
    server {
    
      listen        7000;
    
      server_name   localhost;
    
      location / {
    
        root  /data/www/proxy_pass;
    
      }
    
    }
    

    代理不带项目名称,没有 /

    server {
    		
    	listen      7001;
    	
    	  server_name locahost;
    	
    	  location /proxy/ {
    	
    	    proxy_pass  http://127.0.0.1:7000;
    	
    	  }
    }
    

    代理不带项目名称,但是有 /

    server {
    
      listen      7002;
    
      server_name locahost;
    
      location /proxy/ {
    
        proxy_pass  http://127.0.0.1:7000/;
    
      }
    
    }
    

    没有/ 访问到 proxy test页面 有/访问到test页面

    结论:

    proxy_pass 后面不带 /,我们自定义的路径名会当成url的一部分拼接到后端url里。 7001端口实际访问http://127.0.0.1:7000/proxy/index.html

    proxy_pass 后面带 /,我们的自定义的路径名就不会被视作url的一部分去拼接到后端url。 7002端口实际访问http://127.0.0.1:7000/index.html

    如果是带 / 这种情况,location后面只能用 =、^~、/ 匹配方式,也就是精确匹配、开头匹配及通配,不能使用~正则匹配这种方式,会报错:nginx: [emerg] "proxy_pass" cannot have URI part in location given by regular expression, or inside named location, or inside "if" statement, or inside "limit_except" block in /usr/local/nginx/conf/nginx.conf:69

  • 4
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

偷学技术的梁胖胖yo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值