nginx中location、rewrite用法总结
一、location用法总结
location可以把不同方式的请求,定位到不同的处理方式上.
1.location的用法
location ~* /js/.*/\.js
以 = 开头,表示精确匹配;如只匹配根目录结尾的请求,后面不能带任何字符串。
以^~ 开头,表示uri以某个常规字符串开头,不是正则匹配
以~ 开头,表示区分大小写的正则匹配;
以~* 开头,表示不区分大小写的正则匹配
以/ 开头,通用匹配, 如果没有其它匹配,任何请求都会匹配到
location 的匹配顺序是“先匹配正则,再匹配普通”。
矫正: location 的匹配顺序其实是“先匹配普通,再匹配正则”。我这么说,大家一定会反驳我,因为按“先匹配普通,再匹配正则”解释不了大家平时习惯的按“先匹配正则,再匹配普通”的实践经验。这里我只能暂时解释下,造成这种误解的原因是:正则匹配会覆盖普通匹配。
2.location用法举例
# 精确匹配 / ,主机名后面不能带任何字符串
location = / {
[ configuration A ]
}

2.# 所有的地址都以 / 开头,所以这条规则将最后匹配到默认请求
# 但是正则和最长字符串会优先匹配
location / {
[ configuration B ]
}
例:
location / {
proxy_pass http://server_pools;
}
#这条规则只有其他不符合要求才能匹配到;将是最后匹配到的,匹配度最低,上面实现的功能是:比如网站是www.blog.com;后面什么都不输入的时候,
其他的规则也不匹配的时候,最后交给负载均衡池的服务器
3.# 匹配任何以 /documents/ 开头的地址,匹配符合以后,还要继续往下搜索
# 只有后面的正则表达式没有匹配到时,这一条才会采用这一条
location /documents/ {
[ configuration C ]
}
例:
location /static/
{
rewrite ^ http://www.abc.com ;
}
#上面实现的功能:假设网站域名为www.blog.com;那么配置上面的功能是输入www.blog.com/static/时,不管static后面是什么页面(页面也可以不存在),
那么最终会同样跳转到www.abc.com这个网站。
4.# 匹配任何以 /documents/ 开头的地址,匹配符合以后,还要继续往下搜索
# 只有后面的正则表达式没有匹配到时,这一条才会采用这一条
location ~ /documents/Abc {
[ configuration CC ]
}
5.# 匹配任何以 /images/ 开头的地址,匹配符合以后,停止往下搜索正则,采用这一条。
location ^~ /images/ {
[ configuration D ]
}
6.# 匹配所有以 gif,jpg或jpeg 结尾的请求
# 然而,所有请求 /images/ 下的图片会被 config D 处理,因为 ^~ 到达不了这一条正则
location ~* \.(gif|jpg|jpeg)$ {
[ configuration E ]
}
例:
7.# 字符匹配到 /images/,继续往下,会发现 ^~ 存在
location /images/ {
[ configuration F ]
}
8.# 最长字符匹配到 /images/abc,继续往下,会发现 ^~ 存在
# F与G的放置顺序是没有关系的
location /images/abc {
[ configuration G ]
}
9.# 只有去掉 config D 才有效:先最长匹配 config G 开头的地址,继续往下搜索,匹配到这一条正则,采用
location ~ /images/abc/ {
[ configuration H ]
}
按照上面的location写法,以下的匹配示例成立:
/ -> config A
精确完全匹配,即使/index.html也匹配不了
/downloads/download.html -> config B
匹配B以后,往下没有任何匹配,采用B
/images/.gif -> configuration D
匹配到F,往下匹配到D,停止往下
/images/abc/def -> config D
最长匹配到G,往下匹配D,停止往下
你可以看到 任何以/images/开头的都会匹配到D并停止,FG写在这里是没有任何意义的,H是永远轮不到的,这里只是为了说明匹配顺序
/documents/document.html -> config C
匹配到C,往下没有任何匹配,采用C
/documents/.jpg -> configuration E
匹配到C,往下正则匹配到E
/documents/Abc.jpg -> config CC
最长匹配到C,往下正则顺序匹配到CC,不会往下到E
3、实际使用建议
#直接匹配网站根,通过域名访问网站首页比较频繁,使用这个会加速处理,官网如是说。
#这里是直接转发给后端应用服务器了,也可以是一个静态首页
# 第一个必选规则location = / {
proxy_pass http://tomcat:8080/index
}
# 第二个必选规则是处理静态文件请求,这是nginx作为http服务器的强项
# 有两种配置模式,目录匹配或后缀匹配,任选其一或搭配使用
location ^~ /static/ {
root /webroot/static/;
}
location ~* \.(gif|jpg|jpeg|png|css|js|ico)$ {
root /webroot/res/;
}
#第三个规则就是通用规则,用来转发动态请求到后端应用服务器
#非静态文件请求就默认是动态请求,自己根据实际把握#毕竟目前的一些框架的流行,带.php,.jsp后缀的情况很少了
location / {
proxy_pass http://tomcat:8080/
}
http://tengine.taobao.org/book/chapter_02.html
http://nginx.org/en/docs/http/ngx_http_rewrite_module.html
二、Rewrite用法总结
1.rewrite的定义
rewrite只能放在 server{}, location{}, if{}中,并且只能对域名后边的除去传递的参数外的字符串起作用。
例如 http://seanlook.com/a/we/index.php?id=1&u=str 只对/a/we/index.php重写。
2.rewirte的 语法
执行server块的rewrite指令
执行location匹配
执行选定的location中的rewrite指令
flag标志位
- last : 相当于Apache的[L]标记,表示完成rewrite
- break : 停止执行当前虚拟主机的后续rewrite指令集
- redirect : 返回302临时重定向,地址栏会显示跳转后的地址
- permanent : 返回301永久重定向,地址栏会显示跳转后的地址
- last一般写在server和if中,而break一般使用在location中
- last不终止重写后的url匹配,即新的url会再从server走一遍匹配流程,而break终止重写后的匹配
- break和last都能组织继续执行后面的rewrite指令
3.rewrite常用正则
- . : 匹配除换行符以外的任意字符
- ? : 重复0次或1次
- + : 重复1次或更多次
- * : 重复0次或更多次
- \d :匹配数字
- ^ : 匹配字符串的开始
- $ : 匹配字符串的结束
- {n} : 重复n次
- {n,} : 重复n次或更多次
- [c] : 匹配单个字符c
- [a-z] : 匹配a-z小写字母的任意一个
rewrite实例
1 例1:
2 http {
3 # 定义image日志格式
4 log_format imagelog '[$time_local] ' $image_file ' ' $image_type ' ' $body_bytes_sent ' ' $status;
5 # 开启重写日志
6 rewrite_log on;
7
8 server {
9 root /home/www;
10
11 location / {
12 # 重写规则信息
13 error_log logs/rewrite.log notice;
14 # 注意这里要用‘’单引号引起来,避免{}
15 rewrite '^/images/([a-z]{2})/([a-z0-9]{5})/(.*)\.(png|jpg|gif)$' /data?file=$3.$4;
16 # 注意不能在上面这条规则后面加上“last”参数,否则下面的set指令不会执行
17 set $image_file $3;
18 set $image_type $4;
19 }
20
21 location /data {
22 # 指定针对图片的日志格式,来分析图片类型和大小
23 access_log logs/images.log mian;
24 root /data/images;
25 # 应用前面定义的变量。判断首先文件在不在,不在再判断目录在不在,如果还不在就跳转到最后一个url里
26 try_files /$arg_file /image404.html;
27 }
28 location = /image404.html {
29 # 图片不存在返回特定的信息
30 return 404 "image not found\n";
31 }
32 }
33
34 对形如/images/ef/uh7b3/test.png的请求,重写到/data?file=test.png,于是匹配到location /data,先看/data/images/test.png文件存不存在,如果存在则正常响应,如果不存在则重写tryfiles到新的image404 location,直接返回404状态码。
35
36 例2:
37 rewrite ^/images/(.*)_(\d+)x(\d+)\.(png|jpg|gif)$ /resizer/$1.$4?width=$2&height=$3? last;
38 对形如/images/bla_500x400.jpg的文件请求,重写到/resizer/bla.jpg?width=500&height=400地址,并会继续尝试匹配location。
if指令与全局变量
if判断指令语法
当表达式只是一个变量时,如果值为空或任何以0开头的字符串都会当做false
直接比较变量和内容时,使用=或!=
~ 正则表达式匹配
~* 不区分大小写的匹配
!~ 区分大小写的不匹配
-f和!-f 用来判断是否存在文件
-d和!-d 用来判断是否存在目录
-e和!-e 用来判断是否存在文件或目录
-x和!-x 用来判断文件是否可执行
如果用户设备为IE浏览器的时候,重定向
if ($http_user_agent ~ MSIE) {
rewrite ^(.*)$ /msie/$ break;
} //如果UA包含"MSIE",rewrite请求到/msid/目录下 if ($http_cookie ~* "id=([^;]+)(?:;|$)") {
set $id $;
} //如果cookie匹配正则,设置变量$id等于正则引用部分 if ($request_method = POST) {
return ;
} //如果提交方法为POST,则返回状态405(Method not allowed)。return不能返回301,302 if ($slow) {
limit_rate 10k;
} //限速,$slow可以通过 set 指令设置 if (!-f $request_filename){
break;
proxy_pass http://127.0.0.1;
} //如果请求的文件名不存在,则反向代理到localhost 。这里的break也是停止rewrite检查 if ($args ~ post=){
rewrite ^ http://example.com/ permanent;
} //如果query string中包含"post=140",永久重定向到example.com location ~* \.(gif|jpg|png|swf|flv)$ {
valid_referers none blocked www.jefflei.comwww.leizhenfang.com;
if ($invalid_referer) {
return ;
} //防盗链
}
全局变量
- $args : #这个变量等于请求行中的参数,同$query_string
- $content_length : 请求头中的Content-length字段。
- $content_type : 请求头中的Content-Type字段。
- $document_root : 当前请求在root指令中指定的值。
- $host : 请求主机头字段,否则为服务器名称。
- $http_user_agent : 客户端agent信息
- $http_cookie : 客户端cookie信息
- $limit_rate : 这个变量可以限制连接速率。
- $request_method : 客户端请求的动作,通常为GET或POST。
- $remote_addr : 客户端的IP地址。
- $remote_port : 客户端的端口。
- $remote_user : 已经经过Auth Basic Module验证的用户名。
- $request_filename : 当前请求的文件路径,由root或alias指令与URI请求生成。
- $scheme : HTTP方法(如http,https)。
- $server_protocol : 请求使用的协议,通常是HTTP/1.0或HTTP/1.1。
- $server_addr : 服务器地址,在完成一次系统调用后可以确定这个值。
- $server_name : 服务器名称。
- $server_port : 请求到达服务器的端口号。
- $request_uri : 包含请求参数的原始URI,不包含主机名,如:”/foo/bar.php?arg=baz”。
- $uri : 不带请求参数的当前URI,$uri不包含主机名,如”/foo/bar.html”。
- $document_uri : 与$uri相同。
例:
http://localhost:88/test1/test2/test.php
$host:localhost
$server_port:
$request_uri:http://localhost:88/test1/test2/test.php
$document_uri:/test1/test2/test.php
$document_root:/var/www/html
$request_filename:/var/www/html/test1/test2/test.php
nginx中location、rewrite用法总结的更多相关文章
- nginx 中location和root
nginx 中location和root,你确定真的明白他们关系? 2016-01-17 14:48 3774人阅读 评论(1) 收藏 举报 分类: linux(17) 版权声明:本文为博主原创文 ...
- 17.Nginx 重写(location rewrite)
Nginx 重写(location / rewrite) 目录 Nginx 重写(location / rewrite) 常见的nginx正则表达式 location lication的分类 loca ...
- Nginx 中 location 的匹配顺序
nginx中location的匹配模式有以下几种: 精确匹配:以=开头,只有完全匹配才能生效,例子location = /uri 非正则匹配:以^~开头,^表示非.~表示正则,例子location ^ ...
- nginx中location的顺序(优先级)及rewrite规则写法
一.location正则写法 一个示例: location = / { # 精确匹配 / ,主机名后面不能带任何字符串 [ configuration A ] } location / { # 因为所 ...
- Nginx中location匹配及rewrite重写
目录 一.常用的Nginx正则表达式 二.location 2.1.location三类匹配类型 2.2.常用的匹配规则 2.3.location优先级 2.3.1.举例说明 2.4.实际网站使用中, ...
- Nginx中的Rewrite的重定向配置与实践
阅读目录 一:理解地址重写 与 地址转发的含义. 二:理解 Rewrite指令 使用 三:理解if指令 四:理解防盗链及nginx配置 简介:Rewrite是Nginx服务器提供的一个重要的功能, ...
- Nginx中的rewrite指令
转自:http://www.76ku.cn/articles/archives/317 rewite.在server块下,会优先执行rewrite部分,然后才会去匹配location块server中的 ...
- Nginx中的rewrite指令(break,last,redirect,permanent)
rewite 在server块下,会优先执行rewrite部分,然后才会去匹配location块 server中的rewrite break和last没什么区别,都会去匹配location,所以没必要 ...
- nginx 中location和root、alias
nginx指定文件路径有两种方式root和alias,这两者的用法区别 root与alias主要区别在于nginx如何解释location后面的uri,这会使两者分别以不同的方式将请求映射到服务器文件 ...
随机推荐
- Python+Django(Python Web项目初体验)
参考:https://blog.csdn.net/qq_34081993/article/details/79229784 Django是一个开放源代码的Web应用框架,由Python写成. 安装Dj ...
- html 内联函数宽度设置
width and/or height in tables are not standard anymore; as Ianzz says, they are depreciated. Instead ...
- 高可用(HA)架构
http://aokunsang.iteye.com/blog/2053719 浅谈web应用的负载均衡.集群.高可用(HA)解决方案 http://zhuanlan.51cto.com/art/ ...
- js数组获取相同元素个数,归档排序
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 【视频】ffmpeg mov mp4 m3u8 ts
1.https://ffmpeg.zeranoe.com/builds/ 2.https://blog.csdn.net/psh18513234633/article/details/79312607 ...
- FFMPEG AVRational
FFMPEG的很多结构中有AVRational time_base;这样的一个成员,它是AVRational结构的 typedef struct AVRational{ int num; ///< ...
- U3D调用7z解压文件
using UnityEngine; using System; using System.IO; using System.Diagnostics; public class Test : Mono ...
- Host 'xxx.xx.xxx.xxx' is not allowed to connect to this MySQL server
改表法.可能是你的帐号不允许从远程登陆,只能在localhost.这个时候只要在localhost的那台电脑,登入mysql后,更改 "mysql" 数据库里的 "use ...
- PCL—低层次视觉—关键点检测(Harris)
除去NARF这种和特征检测联系比较紧密的方法外,一般来说特征检测都会对曲率变化比较剧烈的点更敏感.Harris算法是图像检测识别算法中非常重要的一个算法,其对物体姿态变化鲁棒性好,对旋转不敏感,可以很 ...
- POP3协议分析
http://m.blog.csdn.net/bripengandre/article/details/2192111 POP3协议分析 第1章. POP3概述 POP3全称为Post Off ...