nginx location中uri的截取的实现方法

 更新时间:2019年04月12日 14:51:25   作者:全栈运维  
这篇文章主要介绍了nginx location中uri的截取的实现方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

说明:

location 中的 root 和 alias

  • root 指令只是将搜索的根设置为 root 设定的目录,即不会截断 uri,而是使用原始 uri 跳转该目录下查找文件
  • aias 指令则会截断匹配的 uri,然后使用 alias 设定的路径加上剩余的 uri 作为子路径进行查找

location 中的 proxy_pass 的 uri

如果 proxy_pass 的 url 不带 uri

  • 如果尾部是"/",则会截断匹配的uri
  • 如果尾部不是"/",则不会截断匹配的uri

如果proxy_pass的url带uri,则会截断匹配的uri

Examples

location中的 root

root@pts/1 $ ls -ld /data/web/lctest*|awk '{print $NF}'
/data/web/lctest
/data/web/lctest2
/data/web/lctest3
/data/web/lctest4


location /lctest {
  root /data/web/;
}

location /lctest2/ {
  root /data/web/;
}
location /lctest3 {
  root /data/web;
}
location /lctest4/ {
  root /data/web;
}

curl 测试结果如下

备注: 浏览器输入的时候最后面不添加 / , 会自动补上,但是curl 不行

root@pts/1 $ curl http://tapi.xxxx.com/lctest/
hello world

root@pts/1 $ curl http://tapi.xxxx.com/lctest2/
hello world
2

root@pts/1 $ curl http://tapi.xxxx.com/lctest3/
3
hello world

root@pts/1 $ curl http://tapi.xxxx.com/lctest4/
hello world
4

location alias

location /lctest5 {
  alias /data/web/;
}
location /lctest6/ {
  alias /data/web/;
}

location /lctest7 {
  alias /data/web;
}

## 403 /data/web forbidden
location /lctest8/ {
  alias /data/web;
}

curl 测试结果如下

curl 'http://tapi.kaishustory.com/lctest5/'
curl 'http://tapi.kaishustory.com/lctest6/'
curl 'http://tapi.kaishustory.com/lctest7/'
结果都是 /data/web/index.html的输出

root@pts/1 $ curl 'http://tapi.kaishustory.com/lctest8/'
<html>
<head><title>403 Forbidden</title></head>
<body bgcolor="white">
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx</center>
</body>
</html>

location proxy_pass

#--------proxy_pass配置---------------------
location /t1/ { proxy_pass http://servers; }  #正常,不截断
location /t2/ { proxy_pass http://servers/; }  #正常,截断
location /t3 { proxy_pass http://servers; }  #正常,不截断
location /t4 { proxy_pass http://servers/; }  #正常,截断
location /t5/ { proxy_pass http://servers/test/; }  #正常,截断
location /t6/ { proxy_pass http://servers/test; }  #缺"/",截断
location /t7 { proxy_pass http://servers/test/; }  #含"//",截断
location /t8 { proxy_pass http://servers/test; }  #正常,截断

测试脚本

for i in $(seq 8)
do
  url=http://tapi.xxxx.com/t$i/doc/index.html
  echo "-----------$url-----------"
  curl url
done

测试结果

----------http://tapi.xxxx.com/t1/doc/index.html------------
/t1/doc/index.html

----------http://tapi.xxxx.com/t2/doc/index.html------------
/doc/index.html

----------http://tapi.xxxx.com/t3/doc/index.html------------
/t3/doc/index.html

----------http://tapi.xxxx.com/t4/doc/index.html------------
/doc/index.html

----------http://tapi.xxxx.com/t5/doc/index.html------------
/test/doc/index.html

----------http://tapi.xxxx.com/t6/doc/index.html------------
/testdoc/index.html

----------http://tapi.xxxx.com/t7/doc/index.html------------
/test//doc/index.html

----------http://tapi.xxxx.com/t8/doc/index.html------------
/test/doc/index.html

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Nginx安装及配置详细分析

    Nginx安装及配置详细分析

    这篇文章主要介绍了Nginx在各种系统和环境中的安装及配置详细分析。
    2017-11-11
  • 详解Nginx 对访问量的控制

    详解Nginx 对访问量的控制

    本文详解的介绍了 Nginx 的 ngx_http_limit_conn_module 和 ngx_http_limit_req_module 模块,对请求访问量进行控制。非常具有实用价值,需要的朋友可以参考下
    2018-12-12
  • 利用nginx与ffmpeg搭建流媒体服务器过程详解

    利用nginx与ffmpeg搭建流媒体服务器过程详解

    这篇文章主要给大家介绍了利用nginx与ffmpeg搭建流媒体服务器的全过程,文中介绍的很详细,对大家具有一定的参考价值,需要的朋友们下面来一起看看吧。
    2017-03-03
  • Linux下nginx编译安装教程和编译参数详解

    Linux下nginx编译安装教程和编译参数详解

    这篇文章主要介绍了Linux下nginx编译安装教程和编译参数详解,需要的朋友可以参考下
    2014-04-04
  • Nginx定时切割日志实现详解

    Nginx定时切割日志实现详解

    这篇文章主要介绍了Nginx定时切割日志详解,Nginx日志默认情况下写入到一个文件中,为了区分各个域下的日志,我们一般会分开存储。即时这样,文件也会变的越来越大,非常不方便查看分析。下面来聊聊以日期来分隔Nginx日志,需要的朋友可以参考下
    2019-07-07
  • nginx+uwsgi启动Django项目的详细步骤

    nginx+uwsgi启动Django项目的详细步骤

    nginx+uwsgi+django是我们常用的django部署方式。这篇文章主要介绍了nginx+uwsgi启动Django项目的详细步骤,非常具有实用价值,需要的朋友可以参考下
    2018-10-10
  • 浅析nginx 客户端返回499的错误码的问题

    浅析nginx 客户端返回499的错误码的问题

    我们服务器客户端一直有返回错误码499的日志,以前觉得比例不高,就没有仔细查过,最近有领导问这个问题,为什么耗时只有0.0几秒,为啥还499了?最近几天就把这个问题跟踪定位了一下,这里做个记录,对nginx返回499错误码相关知识感兴趣的朋友一起看看吧
    2022-10-10
  • nginx反向代理时如何保持长连接

    nginx反向代理时如何保持长连接

    如果我们使用了nginx去作为反向代理或者负载均衡,从客户端过来的长连接请求就会被转换成短连接发送给服务器端。为了支持长连接,我们需要在nginx服务器上做一些配置。
    2020-10-10
  • Nginx反爬虫策略,防止UA抓取网站

    Nginx反爬虫策略,防止UA抓取网站

    目前网络上的爬虫非常多,有对网站收录有益的,比如百度蜘蛛(Baiduspider),也有不但不遵守robots规则对服务器造成压力,还不能为网站带来流量的无用爬虫,为防止网站有可能会被别人爬,通过配置Nginx, 我们可以拦截大部分爬虫
    2020-09-09
  • Nginx实现负载均衡和反向代理的方法

    Nginx实现负载均衡和反向代理的方法

    Nginx是由俄罗斯人研发的,应对Rambler的网站,并且2004年发布的第一个版本,Nginx功能丰富,可作为HTTP服务器,也可作为反向代理服务器,邮件服务器,本文给大家介绍了Nginx实现负载均衡和反向代理的方法,需要的朋友可以参考下
    2024-02-02

最新评论