[转]Nginx location 和 rewrite retry

作者:sealinger 发布时间:April 28, 2011 分类:混口饭吃 No Comments

原文地址:绍兴数码商城的空间

2011-04-07 23:24

nginx的rewrite有个很奇特的特性 — rewrite后的url会再次进行rewrite检查,最多重试10次,10次后还没有终止的话就会返回HTTP 500。

用过nginx的朋友都知道location区块,location区块有点像Apache中的RewriteBase,但对于nginx来说location是控制的级别而已,里面的内容不仅仅是rewrite.

这里必须稍微先讲一点location的知识.location是nginx用来处理对同一个server不同的请求地址使用独立的配置的方式。

举例:

location  = / {
  ....配置A
}
 
location  / {
  ....配置B
}
 
location ^~ /images/ {
  ....配置C
}
 
location ~* \.(gif|jpg|jpeg)$ {
  ....配置D
}

访问 / 会使用配置A
访问 /documents/document.html 会使用配置B
访问 /images/1.gif 会使用配置C
访问 /documents/1.jpg 会使用配置D

如何判断命中哪个location暂且按下不婊, 我们在实战篇再回头来看这个问题.

现在我们只需要明白一个情况: nginx可以有多个location并使用不同的配.

sever区块中如果有包含rewrite规则,则会最先执行,而且只会执行一次, 然后再判断命中哪个location的配置,再去执行该location中的rewrite, 当该location中的rewrite执行完毕时,rewrite并不会停止,而是根据rewrite过的URL再次判断location并执行其中的配置. 那么,这里就存在一个问题,如果rewrite写的不正确的话,是会在location区块间造成无限循环的.所以nginx才会加一个最多重试10次的上限. 比如这个例子

location /download/ {
  rewrite  ^(/download/.*)/media/(.*)\..*$  $1/mp3/$2.mp3  last;
}

如果请求为 /download/eva/media/op1.mp3 则请求被rewrite到 /download/eva/mp3/op1.mp3。

结果rewrite的结果重新命中了location /download/ 虽然这次并没有命中rewrite规则的正则表达式,但因为缺少终止rewrite的标志,其仍会不停重试download中rewrite规则直到达到10次上限返回HTTP 500。

认真的朋友这时就会问了,上面的rewrite规则不是有标志位last么? last不是终止rewrite的意思么?

说到这里我就要抱怨下了,网上能找到关于nginx rewrite的文章中80%对last标志的解释都是

   last – 基本上都用这个Flag

……这他妈坑爹呢!!! 什么叫基本上都用? 什么是不基本的情况? =皿=

阅读剩余部分...

人生就是折腾(WordPress-to-Typecho)

作者:sealinger 发布时间:April 24, 2011 分类:默认分类 4 Comments

花了两天时间,把Blog系统从WordPress换到Typecho了,挺折腾人的:

1)先安装Typecho-0.8,安装WordPress-to-Typecho插件;
2)再安装WordPress-2.7;
3)从WordPress-3.0导出文章,再从WordPress-2.7导入;
4)再从Typecho中用wordpress-to-typecho插件导入WordPress-2.7的数据库;
5)更新数据库,做些附件路径的修改等;
6)研究Nginx Rewrite,做伪静态化;
7)手工修改下文章的分类。

总算完工,又重新开始。。。

考桩四人组,全部通过,凯旋归来

作者:sealinger 发布时间:April 18, 2011 分类:简单生活 3 Comments

今天下午考桩,等待时间太长(三个小时),导致人容易紧张,直到考完那一刻都没感觉到放松。。。

不过我们都是实力派,毕竟练得很熟了,全部通过。

我中途居然熄火了,是真的紧张啊。。。有木有!!

总之又搞定一门,V!

全球(含香港、澳门)旅游订房专业网站-agoda

作者:sealinger 发布时间:April 2, 2011 分类:简单生活 No Comments

http://www.agoda.com/  (全球主页)

http://www.agoda.com.cn/  (中文主页)

专业驴友同事介绍的,是全球最大的订房网站,他去泰国就是在这里订的。感觉比国内那些旅游网站好多了,前两天刚订了香港的酒店,各种价位都有(貌似该网站和酒店有合作优惠的,跟酒店当天价格不同),且有不少驴友写的酒店点评,供选择参考。

真诚介绍给旅游新手使用(绝非广告,是就好了。。。)。

了解sed的工作原理(pattern space 和 hold space)

作者:sealinger 发布时间:February 27, 2011 分类:混口饭吃 5 Comments

sed是一个非交互式的流编辑器(stream editor)。所谓非交互式,是指使用sed只能在命令行下输入编辑命令来编辑文本,然后在屏幕上查看输出;而所谓流编辑器,是指sed每次只从文件(或输入)读入一行,然后对该行进行指定的处理,并将结果输出到屏幕(除非取消了屏幕输出又没有显式地使用打印命令),接着读入下一行。整个文件像流水一样被逐行处理然后逐行输出。

sed一次处理一行内容。处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区(pattern space)中的内容,处理完成后,把缓冲区(pattern space)的内容送往屏幕。接着清空缓冲区(pattern space),处理下一行,这样不断重复,直到文件末尾。

pattern space(模式空间)相当于车间sed把流内容在这里处理
hold space(保留空间)相当于仓库,加工的半成品在这里临时储存(当然加工完的成品也在这里存储)

How sed Works:


先读入一行,去掉尾部换行符,存入pattern space,执行编辑命令。
处理完毕,除非加了-n参数,把现在的pattern space打印出来,在后边打印曾去掉的换行符。
把pattern space内容给hold space,把pattern space置空。
接着读下一行,处理下一行。

一种非平凡情况,一个文件仅一行,尾部没换行,sed只打印,不会尾部加换行,但若在尾部又附加了输出,他会再补上那个换行。

经典实例解释:


下面的解释小而简洁,但是可以将它作为一个准则,帮助你理解sed命令。

SED在哪里缓存数据

SED维护两个数据缓冲区:主动模式空间(pattern space)和辅助保留空间(hold space)。在“通常”操作中,SED从输入流读取一行存入pattern space,这里就是文本编辑操作发生的地方。hold space最初是空的,但也有在pattern space和hold space直接移动数据的命令。

这里,我们用SED的“x”命令来做一个小实验:

'x'  - 交换pattern space和hold space的内容

一个文件包含三行:

#cat file
line1
line2
line3
#

用SED x 命令操作后:
#sed 'x' file



line1
line2
#

解释:

#sed 'x' file 
        <-- 第一行是空的,因为hold space和pattern space交换了内容,记住最初的时候hold space是空的;在处理完第一行后,现在hold space的内容是line1。
line1    <-- 第二行输出是line1,现在hold space的内容是line2,and so on a so forth . ^_^
line2
#

------------------

操作pattern space和hold space的命令:


$ man sed
       d      Delete pattern space.  Start next cycle.
              删除pattern space的内容,开始下一个循环.

       h H    Copy/append pattern space to hold space.
              复制/追加pattern space的内容到hold space.
       g G    Copy/append hold space to pattern space.
              复制/追加hold space的内容到pattern space.
       x      Exchange the contents of the hold and pattern spaces.
              交换hold space和pattern space的内容.

课后理解:


1)交换第1行和第2行的内容


$ sed -n '1{h;n;x;H;x};p' filename

2)用sed实现tac的功能


$ sed -n -e '1!G;h;$p' filename

$ sed -e '1!G;h;$!d' filename

这2种写法都相当于tac filename。

---------------------------------

引用资料:

# info sed

File: sed.info,  Node: Execution Cycle,  Next: Addresses,  Up: sed Programs

How `sed' Works
===============

   `sed' maintains two data buffers: the active _pattern_ space, and
the auxiliary _hold_ space. Both are initially empty.

   `sed' operates by performing the following cycle on each lines of
input: first, `sed' reads one line from the input stream, removes any
trailing newline, and places it in the pattern space.  Then commands
are executed; each command can have an address associated to it:
addresses are a kind of condition code, and a command is only executed
if the condition is verified before the command is to be executed.

   When the end of the script is reached, unless the `-n' option is in
use, the contents of pattern space are printed out to the output
stream, adding back the trailing newline if it was removed.(1) Then the
next cycle starts for the next input line.

   Unless special commands (like `D') are used, the pattern space is
deleted between two cycles. The hold space, on the other hand, keeps
its data between cycles (see commands `h', `H', `x', `g', `G' to move
data between both buffers).


 

-------------------

following explaination could be small but concise , can treat it as a guideline when you study all the sed commands.

Where SED buffers data
SED maintains two data buffers: the active pattern space, and the auxiliary hold space. In "normal" operation, SED reads in one line from the input stream and places it in the pattern space. This pattern space is where text manipulations occur. The hold space is initially empty, but there are commands for moving data between the pattern and hold spaces.

right, a small practice here for SED command "x" :
'x'  - Exchange the contents of the hold and pattern spaces.

say a file contains following 3 lines ,
#cat file
line1
line2
line3
#
by applying 'x' command, the output is as following :
#sed 'x' file

line1
line2
#
explain :

            <-- first line is empty , because hold space and pattern space exchange the contents , do remember initially the hold space is empty , now hold space contains line1 after first line data manipulation .
line1     <- second output is line1, now hold space contains line2 , and so on a so forth . ^_^
line2