栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

nginx那点事儿——nginx中关于root、alias及localtion、proxy

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

nginx那点事儿——nginx中关于root、alias及localtion、proxy

nginx那点事儿——nginx中关于root、alias及localtion、proxy_pass后面是否有/的区别
  • 一. nginx root与alias区别
  • 二. root 或 alias 或 location 或 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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/388591.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号