设为首页 收藏本站
查看: 1025|回复: 0

[经验分享] Nginx的location详解

[复制链接]

尚未签到

发表于 2018-11-15 09:22:26 | 显示全部楼层 |阅读模式
  location的作用是根据用户请求的URI来执行不同的应用。
  官方提供的常见的location匹配语法:
  location [ = | ~ | ~* | ^~ ] uri {
  ... ...
  }
  URI部分是关键,这个URI可以是普通的字符串地址路径,或者是正则表达式,匹配成功则执行后面大括号里的相关指令。
  正则表达式的前面还可以有"~"或"~*"等特殊字符。
  对location语法列表说明:
  +--------+----------------------------+--------------+---------------------------------+
  |location |          [=|~|~*^~|@]          |         uri         |                       {...}                      |
  +--------+----------------------------+--------------+---------------------------------+
  |  指令     |   匹配标识                           |匹配的网站地址|    匹配URI后要执行的配置段    |
  +--------+----------------------------+--------------+---------------------------------+
  ~用于区分大小写(大小写敏感)的匹配;
  ~*用于不区分大小写的匹配;
  !对匹配结果进行取反;
  ^~用来在进行常规的字符串匹配检查之后,不做正则表达式的检查,即如果最明确的那个字符串匹配的location配置有此前缀,那么不做正则表达式的匹配。
  location匹配示例:
  location = / {          #当用户请求/时,将会匹配。
  [ configueration A ]
  }
  location / {            #当用户请求/index.html时,将会匹配。
  [ configueration B ]
  }
  location /document/ {   #当用户请求/document/doc.html时,将会匹配。
  [ configueration C ]
  }
  location ^~/images/ {   #当用户请求/images/1.gif时,将会匹配。
  [ configueration D ]
  }
  location ~* \.(gif|jpg|jpeg)$ {    #当用户请求/document/1.jpg时,将会匹配。
  [ configueration E ]
  }
  location匹配实战:
  server {
  listen       192.168.30.3;
  server_name  www.smartbro.com smart.com;
  location / {
  root   html/www;
  index  index.html index.htm;
  }
  location = / {
  root html/www;
  index test01.html;
  }
  location /doc/ {
  root html/www;
  index test02.html;
  }
  location ^~ /images/ {
  root html/www;
  index test03.html;
  }
  access_log logs/access_www.log main;
  }
  echo 'test01' > /application/nginx/html/www/test01.html    #创建文件
  echo 'test02' > /application/nginx/html/www/test02.html
  echo 'test03' > /application/nginx/html/www/test03.html
  mkdir /application/nginx/html/www/doc    #创建目录
  mv /application/nginx/html/www/test02.html /application/nginx/html/www/doc       #移动文件
  ll /application/nginx/html/www/doc
  total 4.0K
  -rw-r--r-- 1 root root 7 Aug 11 17:01 test02.html    #创建目录
  mkdir /application/nginx/html/www/images
  mv /application/nginx/html/www/test03.html /application/nginx/html/www/images/     #移动文件
  ll /application/nginx/html/www/images/
  total 4.0K
  -rw-r--r-- 1 root root 7 Aug 11 17:22 test03.html
  /application/nginx/sbin/nginx -t       #检查语法
  nginx: the configuration file /application/nginx-1.13.4//conf/nginx.conf syntax is ok
  nginx: configuration file /application/nginx-1.13.4//conf/nginx.conf test is successful

  /application/nginx/sbin/nginx -s>  测试访问:
  curl http://192.168.30.3
  test01
  curl http://192.168.30.3/doc/
  test02
  curl http://192.168.30.3/images/
  test03
  --------------------------------------------------------------------------------------------------
  Nginx rewrite:
  --------------------------------------------------------------------------------------------------
  主要实现URL地址的重写。Nginx软件的rewrite功能由pcre软件的支持,就是通过Perl兼容正则表达式语法进行规则匹配。
  默认的参数编译的时候会安装支持rewrite的模块,但是必须要有pcre的支持。
  rewrite 语法:
  rewrite regex replacement [flag];
  默认值:none
  应用位置:server location if
  rewrite是实现URL重写的关键指令。
  根据正则表达式的部分定向到placement部分,结束时flag标记。
  regex常用表达式说明:
  +---------------------------------------------------------------------------------------------------------------------+
  |  \    |将后面接着的字符标记为特殊字符或一个原义字符或一个向后引用                                                                   |
  +---------------------------------------------------------------------------------------------------------------------+
  |  ^    |匹配输入字符串的起始位置,如果设置了RegExp对象的Multiline属性,^也匹配“\n”或“\r”之后的位置    |
  +---------------------------------------------------------------------------------------------------------------------+
  |  $    |匹配输入字符串的结束位置,如果设置了RegExp对象的Multiline属性,$也匹配“\n”或“\r”之前的位置     |
  +---------------------------------------------------------------------------------------------------------------------+
  |  *    |匹配前面的字符零次或多次                                                                                                                             |
  +---------------------------------------------------------------------------------------------------------------------+
  |  +    |匹配前面的字符一次或多次                                                                                                                            |
  +---------------------------------------------------------------------------------------------------------------------+
  |  ?      |匹配前面的字符零次或一次,例如do(es)?既可以匹配do也可以匹配does中的do,等价于{0,1}。当该字          |
  |          |符紧跟在任何一个其他的限制符(*  +?  {n}  {n,m})后面时,匹配模式是非贪婪模式的,非贪婪模式                  |
  |    |会尽可能少的匹配所搜索的字符串,而默认的贪婪模式则会尽可能多的匹配所搜索的字符串,例如对于           |
  |    |字符串“oooo”,使用“o+?”会匹配单个字符o,而使用“o+”将会匹配所有的o                                      |
  +---------------------------------------------------------------------------------------------------------------------+
  |  .       |匹配除去“\n”之外的任何单个字符,要匹配包括“\n”在内的任何字符,请使用“[.\n]”这样的模式           |
  +---------------------------------------------------------------------------------------------------------------------+
  |(pattern)  |匹配括号内的pattern,并可以在后面获取对应的匹配,常用$0...$9属性获取小括号中匹配的内容            |
  |                |要匹配圆括号,使用“\(\)”                                                                                                                  |
  +---------------------------------------------------------------------------------------------------------------------+
  rewrite指令flag参数标记的说明:
  +--------------------------------------------------------------------------------------------------+
  |   last   |   本条规则匹配完成后,继续向下匹配新的location URI规则                                           |
  +--------------------------------------------------------------------------------------------------+
  |   break  |   本条规则匹配及完成终止,不在继续匹配后面的任何规则                                            |
  +--------------------------------------------------------------------------------------------------+
  | redirect |   返回302临时重定向,浏览器地址栏会显示跳转后的URL地址                                     |
  +--------------------------------------------------------------------------------------------------+
  | permanent|   返回301永久重定向,浏览器地址栏会显示跳转后的URL地址                                 |
  +--------------------------------------------------------------------------------------------------+
  在flag标记中,last和break用来实现URL重写,浏览器地址栏的URL地址不变,但在服务器访问的应用程序和路径发生了变化。
  redirect和permanent用来实现URL跳转,浏览器地址栏会显示跳转后的URL地址。
  last和break标记实现的功能差不多一样,但是在使用alias指令时必须使用last标记,使用proxy_pass指令时要使用break标记。
  last标记在本条rewrite规则执行完毕后,会对其所在的server{...}标签重新发起请求,而break标记会在本条规则匹配完成,终止匹配。
  Nginx rewrite的企业应用场景:
  --为了让搜索引擎收录网站的内容,并让用户体验更好,企业会将动态URL地址伪静态处理,以提供服务。
  --网站换成新的域名后,让旧的域名访问跳转到新的域名上,例如京东的360buy换成jd.com。
  --根据特殊变量、目录、客户端的信息进行URL跳转。
  Nginx rewrite 301 跳转:
  除了使用别名的方式访问同一个网站,还可使用301跳转的方式实现。
  vim /application/nginx/extra/www.conf
  server {
  listen       192.168.30.3;
  server_name  smartbro.com;
  location / {
  root   html/www;
  index  index.html index.htm;
  }
  rewrite ^/(.*) http://www.smartbro.com/$1 permanent;  #当访问smartbro.com下面的所有内容的时候都会跳转到www.smartbro.com
  access_log logs/access_www.log main;
  }
  实现不同的域名的URI跳转:
  vim /application/nginx/extra/www.conf
  server {
  listen       192.168.30.3;
  server_name  www.smartbro.com;
  location / {
  root   html/www;
  index  index.html index.htm;
  }
  if ( $http_host ~* "^(.*)\.smartbro\.com$" ) {      #设置跳转语句,不区分大小写的正则匹配
  set $domain $1;
  rewrite ^(.*) http://www.smartbri.com/$domain/index.html break;
  }
  access_log logs/access_www.log main;
  }
  /application/nginx/sbin/nginx -t
  nginx: the configuration file /application/nginx-1.13.4//conf/nginx.conf syntax is ok
  nginx: configuration file /application/nginx-1.13.4//conf/nginx.conf test is successful

  /application/nginx/sbin/nginx -s>  添加hosts解析:
  vim /etc/hosts
  192.168.10.3 www.smartbro.com bbs.smartbro.com pan.smartbro.com smartbro.com
  使用浏览器访问测试:
  curl http://smartbro.com
  Welcome to pan.smartbro.com
  curl http://www.smartbro.com
  Welcome to pan.smartbro.com


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-635259-1-1.html 上篇帖子: nginx 301重定向几种写法 下篇帖子: agentzh 的 Nginx 教程(版本 2015.03.19) 第一篇
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表