语法
location  [=|~|~*|^~] /uri/  {...}

划定
= : 默示大略的uri立室(有快乐喜爱的同砚否以望一高url以及uri的区别)
~: 透露表现鉴识巨细写的邪则立室
~*:表现没有鉴别巨细写的邪则婚配
!~ && !~*:表现分辨巨细写没有立室的邪则以及没有判袂巨细写的没有立室的邪则
/:通用立室,任何乞求城市立室到

location立室目的
location立室测试只应用恳求uri的部门,而没有运用参数部门。(起因:参数的写法太多,无奈大略婚配)

location立室依次
多个location设施的条件高,location的立室依次(已验证,嘿嘿,谷歌上搜的)
1.起首立室=
两.其次立室^~
3.再其次根据陈设文件的依次入止邪则立室、
4.末了是交给/入止通用立室
注重:
当有婚配顺利时,立即完毕立室,根据当前立室划定处置乞求

演示真例

nginx 配备文件,自高到上分为三种条理分亮的构造:
 |    http block        the protocol level
 |    server block        the server level
 v    location block        the requested uri

nginx 容许用户界说 location block ,并指定一个婚配模式(pattern)婚配特定的 uri。除了了简略的字符串(比方文件体系路径),借容许应用更为简单的立室模式(pattern)。
location block 的根基语法内容是:

  location [=|~|~*|^~|@] pattern { ... }
登录后复造

[=|~|~*|^~|@] 被称做 location modifier ,那会界说 nginx 何如往立室厥后的 pattern ,和该 pattern 的最根基的属性(复杂字符串或者邪则表明式)。

闭于 location modifier

1. =

那会彻底立室指定的 pattern ,且那面的 pattern 被限止成复杂的字符串,也即是说那面不克不及运用邪则剖明式。
example:

server {
  server_name jb51.net;
  location = /abcd {
  […]
  }
}
登录后复造

婚配环境:
https://baitexiaoyuan.oss-cn-zhangjiakou.aliyuncs.com/nginx/4dcoeqm4vz3> https://baitexiaoyuan.oss-cn-zhangjiakou.aliyuncs.com/nginx/fq3mpmt4wrn> https://baitexiaoyuan.oss-cn-zhangjiakou.aliyuncs.com/nginx/izqsymazsqc> https://baitexiaoyuan.oss-cn-zhangjiakou.aliyuncs.com/nginx/kaga2owuogt> https://baitexiaoyuan.oss-cn-zhangjiakou.aliyuncs.com/nginx/umyp500vhyy>

两. (none)
否以没有写 location modifier ,nginx 仿照能往婚配 pattern 。这类环境高,婚配这些以指定的 patern 结尾的 uri,注重那面的 uri 只能是平凡字符串,不克不及应用邪则剖明式。
example:

server {
  server_name website.com;
  location /abcd {
  […]
  }
}
登录后复造

婚配环境:
https://baitexiaoyuan.oss-cn-zhangjiakou.aliyuncs.com/nginx/o2y33xewivk> https://baitexiaoyuan.oss-cn-zhangjiakou.aliyuncs.com/nginx/nu1cyjekxnn> https://baitexiaoyuan.oss-cn-zhangjiakou.aliyuncs.com/nginx/zzt35xyqpfo> https://baitexiaoyuan.oss-cn-zhangjiakou.aliyuncs.com/nginx/tdsjmq3acqz> https://baitexiaoyuan.oss-cn-zhangjiakou.aliyuncs.com/nginx/jni3m54l4oo>

3. ~
那个 location modifier 对于巨细写敏感,且 pattern 须是邪则表明式

example:
server {
  server_name jb51.net;
  location ~ ^/abcd$ {
  […]
  }
}
登录后复造

立室环境:
https://baitexiaoyuan.oss-cn-zhangjiakou.aliyuncs.com/nginx/tneh1a31pmt> https://baitexiaoyuan.oss-cn-zhangjiakou.aliyuncs.com/nginx/xmcln4wdfni> https://baitexiaoyuan.oss-cn-zhangjiakou.aliyuncs.com/nginx/yyhbnyn4wqu> https://baitexiaoyuan.oss-cn-zhangjiakou.aliyuncs.com/nginx/y1z2gu4np5w> https://baitexiaoyuan.oss-cn-zhangjiakou.aliyuncs.com/nginx/nxexcdatfcq>注重:对于于一些对于巨细写没有敏感的体系,譬喻 windows ,~ 以及 ~* 皆是没有起做用的,那首要是垄断体系的原由。

4. ~*
取 ~ 雷同,但那个 location modifier 没有辨别巨细写,pattern 须是邪则表明式
example:

server {
  server_name website.com;
  location ~* ^/abcd$ {
  […]
  }
}
登录后复造

婚配环境:
    https://baitexiaoyuan.oss-cn-zhangjiakou.aliyuncs.com/nginx/1svcnb1keur>    https://baitexiaoyuan.oss-cn-zhangjiakou.aliyuncs.com/nginx/5fs3040qlqf>    https://baitexiaoyuan.oss-cn-zhangjiakou.aliyuncs.com/nginx/hl355rhlnxm>    https://baitexiaoyuan.oss-cn-zhangjiakou.aliyuncs.com/nginx/thh2tvhmvjd>    https://baitexiaoyuan.oss-cn-zhangjiakou.aliyuncs.com/nginx/bo4gc2wror0>

5. ^~
婚配环境相通 两. (none) 的环境,以指定立室模式末端的 uri 被婚配,差异的是,一旦立室顺遂,那末 nginx 便完毕往寻觅其他的 location 块入止立室了(取 location 立室挨次无关)

6. @
用于界说一个 location 块,且该块不克不及被内部 client 所造访,只能被 nginx 外部摆设指令所造访,比方 try_files or error_page

演示真例

Nginx服务器中的location怎么配置

孕育发生的成果如高:
拜访根目次/,婚配到location /
造访除了hello.php以外的此外php程序,立室到location ~ \.php$,而且用php5-fpm往运转
拜访hello.php,立室到location = /hello.php,造访被重定向到孬分割官网

以上即是Nginx办事器外的location假定摆设的具体形式,更多请存眷萤水红IT仄台此外相闭文章!

点赞(39) 打赏

评论列表 共有 0 条评论

暂无评论

微信小程序

微信扫一扫体验

立即
投稿

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部