`
ibadboy
  • 浏览: 82083 次
  • 性别: Icon_minigender_1
  • 来自: 江苏
社区版块
存档分类
最新评论

Nginx Location和rewrite配置总结

阅读更多
语法规则: location [=|~|~*|^~] /uri/ { … }
= 开头表示精确匹配

^~ 开头表示uri以某个常规字符串开头,理解为匹配 url路径即可。nginx不对url做编码,因此请求为/static/20%/aa,可以被规则^~ /static/ /aa匹配到(注意是空格)。

~ 开头表示区分大小写的正则匹配

~*  开头表示不区分大小写的正则匹配

!~和!~*分别为区分大小写不匹配及不区分大小写不匹配 的正则

/ 通用匹配,任何请求都会匹配到。

多个location配置的情况下匹配顺序为(参考资料而来,还未实际验证,试试就知道了,不必拘泥,仅供参考):

首先匹配 =,其次匹配^~, 其次是按文件中顺序的正则匹配,最后是交给 / 通用匹配。当有匹配成功时候,停止匹配,按当前匹配规则处理请求。

例子,有如下匹配规则:


location = / {
   #规则A
}
location = /login {
   #规则B
}
location ^~ /static/ {
   #规则C
}
location ~ \.(gif|jpg|png|js|css)$ {
   #规则D
}
location ~* \.png$ {
   #规则E
}
location !~ \.xhtml$ {
   #规则F
}
location !~* \.xhtml$ {
   #规则G
}
location / {
   #规则H
}
那么产生的效果如下:

访问根目录/, 比如http://localhost/ 将匹配规则A

访问 http://localhost/login 将匹配规则B,http://localhost/register 则匹配规则H

访问 http://localhost/static/a.html 将匹配规则C

访问 http://localhost/a.gif, http://localhost/b.jpg 将匹配规则D和规则E,但是规则D顺序优先,规则E不起作用, 而 http://localhost/static/c.png 则优先匹配到 规则C

访问 http://localhost/a.PNG 则匹配规则E, 而不会匹配规则D,因为规则E不区分大小写。

访问 http://localhost/a.xhtml 不会匹配规则F和规则G,http://localhost/a.XHTML不会匹配规则G,因为不区分大小写。规则F,规则G属于排除法,符合匹配规则但是不会匹配到,所以想想看实际应用中哪里会用到。

访问 http://localhost/category/id/1111 则最终匹配到规则H,因为以上规则都不匹配,这个时候应该是nginx转发请求给后端应用服务器,比如FastCGI(php),tomcat(jsp),nginx作为方向代理服务器存在。

所以实际使用中,个人觉得至少有三个匹配规则定义,如下:

#直接匹配网站根,通过域名访问网站首页比较频繁,使用这个会加速处理,官网如是说。
#这里是直接转发给后端应用服务器了,也可以是一个静态首页
# 第一个必选规则
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/
}


未试验过的其他信息:
三、ReWrite语法
last – 基本上都用这个Flag。
break – 中止Rewirte,不在继续匹配
redirect – 返回临时重定向的HTTP状态302
permanent – 返回永久重定向的HTTP状态301
1、下面是可以用来判断的表达式:
-f和!-f用来判断是否存在文件
-d和!-d用来判断是否存在目录
-e和!-e用来判断是否存在文件或目录
-x和!-x用来判断文件是否可执行
2、下面是可以用作判断的全局变量
引用
例:http://localhost:88/test1/test2/test.php
$host:localhost
$server_port:88
重点解释$request_url和$url,之前在网上搜索大部分都是错误的,正确如下:
$request_uri:/test1/test2/test.php
官方解释:
引用
$request_uri
This variable is equal to the *original* request URI as received from the client including the args. It cannot be modified. Look at $uri for the post-rewrite/altered URI. Does not include host name. Example: "/foo/bar.php?arg=baz"
这个变量等于从客户端发送来的原生请求URI,包括参数。它不可以进行修改。$uri变量反映的是重写后/改变的URI。不包括主机名。例如:"/foo/bar.php?arg=baz"

$uri
This variable is the current request URI, without any arguments (see $args for those). This variable will reflect any modifications done so far by internal redirects or the index module. Note this may be different from $request_uri, as $request_uri is what was originally sent by the browser before any such modifications. Does not include the protocol or host name. Example: /foo/bar.html
这个变量指当前的请求URI,不包括任何参数(见$args)。这个变量反映任何内部重定向或index模块所做的修改。注意,这和$request_uri不同,因$request_uri是浏览器发起的不做任何修改的原生URI。不包括协议及主机名。例如:"/foo/bar.html"

$document_uri
The same as $uri.
同$uri.


$document_uri:/test1/test2/test.php
$document_root:D:\nginx/html
$request_filename:D:\nginx/html/test1/test2/test.php

四、Redirect语法
server {
listen 80;
server_name start.igrow.cn;
index index.html index.php;
root html;
if ($http_host !~ “^star\.igrow\.cn$&quot {
rewrite ^(.*) http://star.igrow.cn$1 redirect;
}
}
五、防盗链location ~* \.(gif|jpg|swf)$ {
valid_referers none blocked start.igrow.cn sta.igrow.cn;
if ($invalid_referer) {
rewrite ^/ http://$host/logo.png;
}
}
六、根据文件类型设置过期时间
location ~* \.(js|css|jpg|jpeg|gif|png|swf)$ {
if (-f $request_filename) {
expires 1h;
break;
}
}
七、禁止访问某个目录
location ~* \.(txt|doc)${
root /data/www/wwwroot/linuxtone/test;
deny all;
}



++ 一些可用的全局变量
$args
$content_length
$content_type
$document_root
$document_uri
$host
$http_user_agent
$http_cookie
$limit_rate
$request_body_file
$request_method
$remote_addr
$remote_port
$remote_user
$request_filename
$request_uri
$query_string
$scheme
$server_protocol
$server_addr
$server_name
$server_port
$uri
分享到:
评论

相关推荐

    nginx配置location总结及rewrite规则写法

    nginx配置location总结及rewrite规则写法

    nginx配置location总结location正则写法及rewrite规则写法

    本文详细讲述了Nginx location正则写法,Nginx 的Rewrite规则以及Nginx.conf中if指令与全局变量

    详解nginx中location、rewrite用法总结

    主要介绍了详解nginx中location、rewrite用法总结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

    详解nginx配置location总结及rewrite规则写法

    本篇文章主要介绍了详解nginx配置location总结及rewrite规则写法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

    nginx中location中关于proxy_pass和rewrite的应用.rar

    nginx中location中关于proxy_pass和rewrite的应用.rar

    nginx搭建配置详细说明

    3.4. nginx重要指令之location 4. nginx中的rewrite 4.1. 什么是rewrite 4.2. rewrite的命令的作用域和优先级 4.3. if指令 4.3.1. if指令的语法 4.3.2. if指令中使用的逻辑运算符 4.3.3. If指令中可以使用的...

    详解nginx rewrite和根据url参数location

    最近项目中涉及到旧老项目迁移,需要在nginx上做些配置,所以简单学习了下,好记性不如烂笔头,先记下来。 rewrite 首先查看下nginx是否支持rewrite: ./nginx -V 不支持说明安装nginx时候缺少pcre,需要重新安装...

    Nginx常用Rewrite伪静态法则

    Nginx常用Rewrite伪静态法则 信赖此刻大部门用Linux VPS的伴侣都在利用这个敏捷传布的Nginx,本日就清算一下最常见的PHP法式的Rewrite(伪静态法则)。 Wordpress: location / { index index.html index.php; if ...

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

    主要介绍了nginx location中uri的截取的实现方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

    Web服务器三剑客运维配置实战 Nginx+JVM+Tomcat+HTTP协议.zip

    ├─2.13 Nginx进阶基础-常见配置-location.mp4 ├─3.01 Nginx进阶配置提升-预定义变量及日志详解.mp4 ├─3.02 Nginx进阶配置提升-日志切割及升级.mp4 ├─3.03 Nginx进阶-配置提升-Nginx错误页面.mp4 ├─3.04 ...

    Nginx本地目录映射实现代码实例

    比如想通过浏览器http://huoche.7234.cn/images/jb51/4n5r2x2bwa2.jpg访问到系统目录/image_data/2016/04/29/10/abc.jpg需要在nginx.conf中对应的server {}下添加location规则,配置如下: location /image/ { ...

    Nginx配置的rewrite编写时last与break的区别分析

    在使用nginx配置rewrite中经常会遇到有的地方用last并不能工作,换成break就可以,其中的原理是对于根目录的理解有所区别,按我的测试结果大致是这样的。 location / { proxy_pass http://test; alias /home/...

    nginx配置教程之add_header的坑详解

    它的处理阶段比 location 处理晚,虽然可以写在 location 中,但如果 rewrite 别的 location,那么上一个 location 中尚未处理的 add_header 就会丢失。比如: location = /a { add_header a

    nginx rewrite 伪静态配置参数详细说明

    * last 相当于Apache里的[L]标记,表示完成rewrite * break 终止匹配, 不再匹配后面的规则 * redirect 返回302临时重定向 地址栏会显示跳转后的地址 * permanent 返回301永久重定向 地址栏会显示跳转后的地

    nginx反向代理导致session失效的问题解决

    两边通过同一个nginx进行反向代理,nginx配置大致如下, location /health/ { proxy_pass http://192.168.40.159:8081/health/; #无问题的配置 } location /health-dev/ { proxy_pass ...

    nginx基础实例培训视频.zip

    03-nginx虚拟主机配置 04-nginx日志管理 05-nginx定时任务完成日志切割 06-Location详解之精准匹配 07-Location之正则匹配 08-Location总结图解 09-nginx Rewrite语法详解 10-编译PHP并与nginx整合 11-安装ecshop 12...

    Nginx 安装与配置规则入门详解

    默认的配置文件地址在 /usr/local/etc/nginx/nginx.conf nginx 默认用的 8080 端口,如果发现端口被占用了(通过 $lsof -i:8080查看端口占用情况),可以杀掉使用该端口的进程($kill 进程PID)。或者修改 nginx 的...

Global site tag (gtag.js) - Google Analytics