nginx的Location的总结以及rewrite规则的总结
Location的语法:
location 有”定位”的意思, 根据Uri来进行不同的定位.
在虚拟主机的配置中,是必不可少的,location可以把网站的不同部分,定位到不同的处理方式上.
比如, 碰到.php, 如何调用PHP解释器? --这时就需要location
location 的语法:
location [=|~|~*|^~] patt {
}
中括号可以不写任何参数,此时称为一般匹配
也可以写参数,因此,大类型可以分为3种
location = patt {} [精准匹配]
location patt{} [一般匹配]
location ~ patt{} [正则匹配]
如何发挥作用?
首先看有没有精准匹配,如果有,则停止匹配过程.
location = patt {
config A
}
如果 $uri == patt,匹配成功,使用configA
location = / {
root /var/www/html/;
index index.htm index.html;
} location / {
root /usr/local/nginx/html;
index index.html index.htm;
} 如果访问 http://xxx.com/
定位流程是
: 精准匹配中 ”/” ,得到index页为 index.htm
: 再次访问 /index.htm , 此次内部转跳uri已经是”/index.htm” ,
根目录为/usr/local/nginx/html
: 最终结果,访问了 /usr/local/nginx/html/index.htm
再来看,正则也来参与.
location / {
root /usr/local/nginx/html;
index index.html index.htm;
} location ~ image {
root /var/www/image;
index index.html;
} 如果我们访问 http://xx.com/image/logo.png
此时, “/” 与”/image/logo.png” 匹配
同时,”image”正则 与”image/logo.png”也能匹配,谁发挥作用?
正则表达式的成果将会使用. 图片真正会访问 /var/www/image/logo.png
我们接下来再看一下下面的这个例子:
location / {
root /usr/local/nginx/html;
index index.html index.htm;
} location /foo {
root /var/www/html;
index index.html;
}
我们访问 http://xxx.com/foo
对于uri “/foo”, 两个location的patt,都能匹配他们
即 ‘/’能从左前缀匹配 ‘/foo’, ‘/foo’也能左前缀匹配’/foo’,
此时, 真正访问 /var/www/html/index.html
原因:’/foo’匹配的更长,因此使用之.;
rewrite 重写
下面看一下具体的语法:
重写中用到的指令
if (条件) {} 设定条件,再进行重写
set #设置变量
return #返回状态码
break #跳出rewrite
rewrite #重写 If 语法格式
If 空格 (条件) {
重写模式
} 条件又怎么写?
答:3种写法
: “=”来判断相等, 用于字符串比较
: “~” 用正则来匹配(此处的正则区分大小写)
~* 不区分大小写的正则
: -f -d -e来判断是否为文件,为目录,是否存在.
我们看一下下面的具体的例子:
if ($remote_addr = 192.168.1.100) {
return ;
} //判断是不是IE的用户
If ($http_user_agent ~ MSIE) {
rewrite ^.*$ /ie.htm;
break; #(不break会循环重定向)
} if (!-e $document_root$fastcgi_script_name) {
rewrite ^.*$ /.html
break;
}
注, 此处还要加break,
以 xx.com/dsafsd.html这个不存在页面为例,
我们观察访问日志, 日志中显示的访问路径,依然是GET /dsafsd.html HTTP/1.1
提示: 服务器内部的rewrite和302跳转不一样.
跳转的话URL都变了,变成重新http请求404.html, 而内部rewrite, 上下文没变,
就是说 fastcgi_script_name 仍然是 dsafsd.html,因此 会循环重定向.
set 是设置变量用的, 可以用来达到多条件判断时作标志用.
达到apache下的 rewrite_condition的效果
我们来看一下set变量的一些用法:
if ($http_user_agent ~* msie) {
set $isie ;
} if ($fastcgi_script_name = ie.html) {
set $isie ;
} if ($isie ) {
rewrite ^.*$ ie.html;
}
此时我们不需要加break进行
nginx的Location的总结以及rewrite规则的总结的更多相关文章
- nginx+tomcat集群配置(4)--rewrite规则和多应用根目录设定思路
前言: nginx中有一块很重要的概念, 就是rewrite规则. 它会对URL进行修改, 然后进行内部的重定向. rewrite授予了nginx更多的自由, 使得后级服务的接入更加地方便. 本文将简 ...
- Nginx 配置 location 以及 return、rewrite 和 try_files 指令
正则表达式 Nginx 内置的全局变量 location 前缀字符串及优先级 示例 location 匹配原则 if 和 break 指令 if break return.rewrite 和 try_ ...
- nginx配置location项的URL匹配规则
Localtion URL的正则匹配规则 示例 location / { try_files $uri @apache; } #所有的路径都是/开头,表示匹配所有 location @apache { ...
- Nginx配置location总结及rewrite规则写法
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 32.0px "Helvetica Neue"; color: #323333 } p. ...
- rewrite规则写法及nginx配置location总结
rewrite只能放在server{},location{},if{}中,并且只能对域名后边的除去传递的参数外的字符串起作用. 例如http://seanlook.com/a/we/index.php ...
- 转:Nginx 配置 location 总结及 rewrite 规则写法
转: http://www.linuxidc.com/Linux/2015-06/119398.htm 1. location正则写法 一个示例: location =/{ # 精确匹配 / ,主机名 ...
- 【转】nginx配置location总结及rewrite规则写法
原文:http://seanlook.com/2015/05/17/nginx-location-rewrite/ 1. location正则写法 location = / { # 精确匹配 / ,主 ...
- nginx配置 location及rewrite规则详解
1. location正则写法 语法规则: location [=|~|~*|^~] /uri/ { … } = 开头表示精确匹配 ^~ 开头表示uri以某个常规字符串开头,理解为匹配 url ...
- nginx配置location总结及rewrite规则写法(转)
一个示例: location = / { # 精确匹配 / ,主机名后面不能带任何字符串 [ configuration A ] } location / { # 因为所有的地址都以 / 开头,所以这 ...
随机推荐
- MVC 3.0 上传多张图片到服务器
View关键代码: @using (Html.BeginForm("Create", "Activity", FormMethod.Post, new { en ...
- 基于MPI的并行计算—矩阵向量乘
以前没接触过MPI编程,对并行计算也没什么了解.朋友的期末课程作业让我帮忙写一写,哎,实现结果很一般啊.最终也没完整完成任务,惭愧惭愧. 问题大概是利用MPI完成矩阵和向量相乘.输入:Am×n,Bn× ...
- JS几种table切换
1.使用className <!doctype html> <html lang="en"> <head> <meta charset=& ...
- mm/memory
/* * linux/mm/memory.c * * Copyright (C) 1991, 1992 Linus Torvalds */ /* * demand-loading started ...
- CE5 WiFi开关
WinCE的WiFi开关要依赖于WiFi模块的休眠功能还有模块厂商提供的驱动里面有支持电源管理功能.这里面用的是SDIO86861模块,WinCE5的系统,S3C2416的CPU. 头文件要包括:#i ...
- Openvas 使用
最新版的kali没有安装,配好源,就可以安装. 一. 简介: Nessus是其中一个最流行的和有强力的漏洞扫描器,尤其是对UNIX系统.它最初是自由和开放源码,但他们在2005年关闭了源代码,在200 ...
- C++访问托管类(C#类库)
1.新建C# 类库项目Airth,新建类 using System; using System.Collections.Generic; using System.Linq; using System ...
- 如何在IIS 7.5中部署Asp.Net MVC 5的网站
0 Sign in to vote 系统是 windwos 2008 已经安装.Net 4.0 和 .Net 4.5 已经安装MVC4 的需要文件,MVC5 找不见下载地方,求各位大哥告知一下在哪里可 ...
- ASP.NET SignalR 与 LayIM2.0 配合轻松实现Web聊天室 实战系列(不断更新中)
项目简介 利用ASP.NET SignalR技术与Layim前端im框架实现的一个简单的web聊天室,包括单聊,群聊,加好友,加群,好友搜索,管理,群组管理,好友权限设置等功能.涉及技术: Elast ...
- ROC曲线
1.混淆矩阵(confusion matrix) 针对预测值和真实值之间的关系,我们可以将样本分为四个部分,分别是: 真正例(True Positive,TP):预测值和真实值都为1 ...