Nginx配置root、alias、try_files、proxy_pass指令分析

一、root和alias

在Nginx中,rootalias指令都可以用来指定Web服务器中的文件根目录

root指令指定的是服务器根目录,是用于处理HTTP请求时所使用的默认根目录。例如,若root /var/www/html;,则访问http://example.com/index.html会定位到/var/www/html/index.html文件。

alias指令指定的是实际文件存储位置,它指定的路径是将URL的字符串替换为实际路径。例如,若alias /data/www/;,则访问http://example.com/static/index.html会定位到/data/www/static/index.html文件。

location命中后

如果是root,会把请求url的 ip/域名+port替换为root指定的目录,访问资源

如果是alias,会把请求url的ip/域名+port+匹配到的路径替换为alias指定的目录,访问资源

server {
    listen       80;
    location / {
        root /data;
        index index.html index.htm;
        try_files $uri $uri/ /index.html;
    }
}

#结果
访问路径:http://127.0.0.1:80/index.html  实际访问/data/index.html
访问路径:http://127.0.0.1:80/a/index.html  实际访问/data/a/index.html

--------------------------------------------------------------------
location /a {
  root /data;
  index index.html index.htm;
  try_files $uri $uri/ /index.html;
}


#结果
访问路径:http://127.0.0.1:80/a/index.html  实际访问/data/a/index.html
访问路径:http://127.0.0.1:80/aaab/index.html  实际访问/data/aaab/index.html

root指令实际访问的文件路径是root路径+location路径

 我个人建议alias指令后面的路径都加上/,因为大多数我们配置的都是指定到固定文件夹,除非你的location匹配的是固定的文件,那么你alias也可以指定固定文件路径

#配置指定的文件路径

server {
    listen       80;
    location /index.html {
      alias /data/index.html;
    }
}


#结果
访问路径:http://127.0.0.1:80/index.html  实际访问/data/index.html
访问其他地址都会报`404 Not Found`

-----------------------------------------------------------------------

server {
    listen       80;
    location /a {
        alias /data/;
        index index.html index.htm;
        try_files $uri $uri/ /index.html;
    }
}


#结果
访问路径:http://192.168.137.110:8060/a/index.html  实际访问/data/index.html
访问路径:http://192.168.137.110:8060/a/a/index.html  实际访问/data/a/index.html

alias指令会把location上配置的值去掉,然后把后缀拼接到alias指令后面,就像上面访问/a/index.html,而location是/a,最终的访问文件地址是/data/index.html。

二、 try_files指令

按指定的file顺序查找存在的文件,并使用第一个找到的文件进行请求处理。

server {
    listen       80;
    location /a/ {
        root /data/;
        index index.html index.htm;
        try_files $uri $uri/ /index.html;
    }
}


#比如请求http://192.168.137.110:8060/a/index.html

$uri

查找文件/data/a/index.html

$uri/

查找文件夹/data/a/index.html/下的index.html文件

/index.html

请求http://192.168.137.110:8060/index.html路径

为什么要加try_files指令,因为路由由前端控制了,页面刷新的时候路由重新到后端,找不到该文件,我们就把它重定向到index.html文件,交给前端解析。

server {
    listen       80;
    location /mobile/ {
        alias /data-mobile/;
        index index.html index.htm;
        try_files $uri $uri/ /mobile/index.html;
    }
    location /pc/ {
        alias /data-pc/;
        index index.html index.htm;
        try_files $uri $uri/ /pc/index.html;
    }
}

三、proxy_pass指令

该指令用于反向代理使用

 

server {
    listen       80;
    location /apis-config1/ {
        proxy_pass http://192.168.137.2:5021/;
    }
}

访问`http://192.168.137.110:8060/apis-config1/abc`    实际访问的是`http://192.168.137.2:5021/abc`


---------------------------------------------------------------

server {
    listen       80;
    location /apis-config2/ {
        proxy_pass http://192.168.137.2:5021;
    }
}


访问`http://192.168.137.110:8060/apis-config2/abc`    实际访问的是`http://192.168.137.2:5021/apis-config2/abc`

--------------------------------------------------------------

server {
    listen       80;
    location /apis-config3/ {
        proxy_pass http://192.168.137.2:5021/apis/;
    }
}


访问`http://192.168.137.110:8060/apis-config3/abc`    实际访问的是`http://192.168.137.2:5021/apis/abc`

--------------------------------------------------------------

server {
    listen       80;
    location /apis-config4-1/ {
        proxy_pass http://192.168.137.2:5021/apis;
    }

    location /apis-config4-2 {
        proxy_pass http://192.168.137.2:5021/apis;
    }
}


访问`http://192.168.137.110:8060/apis-config4-1/abc`    实际访问的是`http://192.168.137.2:5021/apisabc`

访问`http://192.168.137.110:8060/apis-config4-2/abc`    实际访问的是`http://192.168.137.2:5021/apis/abc`

--------------------------------------------------------------

server {
    listen       80;
    location /apis-config5-1 {
        proxy_pass http://192.168.137.2:5021/;
    }

    location /apis-config5-2 {
        proxy_pass http://192.168.137.2:5021;
    }
}


访问`http://192.168.137.110:8060/apis-config5-1/abc`    实际访问的是`http://192.168.137.2:5021//abc`

访问`http://192.168.137.110:8060/apis-config5-2/abc`    实际访问的是`http://192.168.137.2:5021/apis-config5-2/abc`

对于这个没啥好说的,按需选取配置,不过我个人推荐都加上/符号,就按照案例一配置就能不变应万变。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值