nginx rewrite rule configuration

Regular wording location

An example:

location  = / {
  # 精确匹配 / ,主机名后面不能带任何字符串
  [ configuration A ] 
}

location / {  # 因为所有的地址都以 / 开头,所以这条规则将匹配到所有请求  # 但是正则和最长字符串会优先匹配 [ configuration B ] } location /documents/ {  # 匹配任何以 /documents/ 开头的地址,匹配符合以后,还要继续往下搜索  # 只有后面的正则表达式没有匹配到时,这一条才会采用这一条 [ configuration C ] } location ~ /documents/Abc {  # 匹配任何以 /documents/ 开头的地址,匹配符合以后,还要继续往下搜索  # 只有后面的正则表达式没有匹配到时,这一条才会采用这一条 [ configuration CC ] } location ^~ /images/ {  # 匹配任何以 /images/ 开头的地址,匹配符合以后,停止往下搜索正则,采用这一条。 [ configuration D ] } location ~* \.(gif|jpg|jpeg)$ {  # 匹配所有以 gif,jpg或jpeg 结尾的请求  # 然而,所有请求 /images/ 下的图片会被 config D 处理,因为 ^~ 到达不了这一条正则 [ configuration E ] } location /images/ {  # 字符匹配到 /images/,继续往下,会发现 ^~ 存在 [ configuration F ] } location /images/abc {  # 最长字符匹配到 /images/abc,继续往下,会发现 ^~ 存在  # F与G的放置顺序是没有关系的 [ configuration G ] } location ~ /images/abc/ {  # 只有去掉 config D 才有效:先最长匹配 config G 开头的地址,继续往下搜索,匹配到这一条正则,采用 [ configuration H ] } location ~* /js/.*/\.js 
  • Is =beginning exact matching
    , such as the request A only matches the end of the root directory, the back can not take any string.
  • ^~ Uri represents the beginning starting with a conventional string, not a regular match
  • ~ Represents the beginning of a case-sensitive match regular;
  • ~ * Represents the beginning of a case-insensitive regular match
  • / Generic matches, if no other matches, any requests are matched to the

No priority order:
(= LOCATION)> (LOCATION full path)> (location ^ ~ Path)> (location ~, ~ * regular order)> (LOCATION initial path portion)> (/)

The above results matching
accordance with the above location wording, the following examples set up match:

  • / -> config A
    precisely exact match, even if not also match /index.html
  • /downloads/download.html -> config B
    after matching B, down to no match, the use of B
  • /images/1.gif -> configuration D
    match to F, the matching down to D, stops down
  • / images / abc / def -> config D
    longest match to G, down to match D, stop down
    you can see any at / images / D to the beginning of the match will be stopped and, FG write here is no sense the, H never fail to get to here only to illustrate the matching order
  • /documents/document.html -> config C
    match to C, there is no match down, using C
  • /documents/1.jpg -> configuration E
    matched C, down to match the regular E
  • /documents/Abc.jpg -> config CC
    longest match to C, down to match the regular sequence CC, not down to E

Practical recommendations

所以实际使用中,个人觉得至少有三个匹配规则定义,如下:
#直接匹配网站根,通过域名访问网站首页比较频繁,使用这个会加速处理,官网如是说。
#这里是直接转发给后端应用服务器了,也可以是一个静态首页 # 第一个必选规则 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 Rules

rewrite function is to use a global variable or variables provided by nginx set up their own, combined with regular expressions and flags achieve url rewrite and redirect. rewrite only on the server {}, location {}, if {} , and act only outside the string parameter passed behind domain removed, for example,  http://seanlook.com/a/we/index.php?id=1&u=str only /a/we/index.php rewriting. grammarrewrite regex replacement [flag];

If the parameter string relative to the domain name or function, can use global variables match, proxy_pass reverse proxy may be used.

Show look a bit like a rewrite function and location, can realize the jump, the main difference is that the rewrite is to change the access to resources within the same domain path, while the location is a kind of path do control access or reverse proxy, to other machines can proxy_pass . Lower rewrite many cases will be written in the location where their execution order is:

  1. Rewrite instruction block execution server
  2. Execution location match
  3. Perform the selected location in the rewrite instructions

If a step URI is rewritten, then re-execute cycle 1-3 until you find the file actually exists; cycle more than 10 times, 500 Internal Server Error error is returned.

flag flag

  • last : Apache corresponding to the [L] flag, indicating the completion rewrite
  • break : Follow-up rewrite instruction to stop execution of the current web hosting collection
  • redirect : Return to 302 temporary redirect, the address bar will show the address after the jump
  • permanent : Returns 301 permanent redirect, the address bar will show the address after the jump

302 reasons and because only returns a status code, there must be redirected URL, which is the return instruction can not be simply unable to return to 301 and 302 of the 301. Here the difference between last and break a little difficult to understand:

  1. last generally written in the server and if, rather break in general use in location
  2. last is not terminated after rewriting url match that the new url from the server will then move again matching process, and terminate the match after the break rewrite
  3. break and organizations can continue to rewrite last instruction behind

if the global variable instruction

if a determination instruction
syntax if(condition){...}for a given condition judgment conditions. If true, rewrite instructions within the braces will be executed, if the condition (conditon) may be any of the following elements:

  • When the expression is only one variable, if the value is null or in any string that begins with 0 as false will
  • When the direct comparison of variables and content, use =or!=
  • ~Regular expression matching, ~*insensitive matching, and !~case-sensitive mismatch

-fAnd !-fused to determine whether there is a file
-dand !-dused to determine whether there is a directory
-e, and !-eused to determine whether there is a file or directory
-x, and !-xused to determine whether an executable file

E.g:

if ($http_user_agent ~ MSIE) {
    rewrite ^(.*)$ /msie/$1 break; } //如果UA包含"MSIE",rewrite请求到/msid/目录下 if ($http_cookie ~* "id=([^;]+)(?:;|$)") { set $id $1; } //如果cookie匹配正则,设置变量$id等于正则引用部分 if ($request_method = POST) { return 405; } //如果提交方法为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=140){ rewrite ^ http://example.com/ permanent; } //如果query string中包含"post=140",永久重定向到example.com location ~* \.(gif|jpg|png|swf|flv)$ { valid_referers none blocked www.jefflei.com www.leizhenfang.com; if ($invalid_referer) { return 404; } //防盗链 } 

Global Variables
The following may be used as the global variable is judged if

  • $args : # This variable equal to the parameter line request, with$query_string
  • $content_length : Request Content-length header field.
  • $content_type : Request Content-Type header field.
  • $document_root : The value specified in the instruction root in the current request.
  • $host : Request Host header field, otherwise the server name.
  • $http_user_agent : The client agent information
  • $http_cookie : Client cookie information
  • $limit_rate : This variable can limit the connection rate.
  • $request_method : The client requests an operation, usually GET or POST.
  • $remote_addr : IP address of the client.
  • $remote_port : Port clients.
  • $remote_user : Username Auth Basic Module has passed validation.
  • $request_filename : The file path of the current request, and the request by the root URI or alias command generation.
  • $scheme : HTTP method (such as http, https).
  • $server_protocol : Protocol requests, usually HTTP / 1.0 or HTTP / 1.1.
  • $server_addr : Server address, after the completion of a system call can determine this value.
  • $server_name : name of server.
  • $server_port : Request arrives at the port number of the server.
  • $request_uri : Contains the parameters of the original request URI, does not include the host name, such as: "/ foo / bar.php arg = baz?".
  • $uri : Without the current request URI parameters, $ uri does not contain a host name, such as "/foo/bar.html".
  • $document_uri : Same as the $ uri.

例:http://localhost:88/test1/test2/test.php
$host:localhost
$server_port:88
$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

Common regular

  • . : Match any character except newline
  • ? : Repeat 0 or 1
  • + : Repeat 1 or more times
  • * : Repeat 0 or more times
  • \d : Match numbers
  • ^ : Matching string begins
  • $ : Introduction to match the string
  • {n} : N times
  • {n,} : Repeated n times or more
  • [c] : Matches any single character c
  • [a-z] : Match any lowercase letters az a

Parentheses ()content matching between, you can later with $1referenced, $2represents the second front ()in the content. Regular people get confused there is \an escape special characters.

rewrite example

Example 1 :

http {
    # 定义image日志格式
    log_format imagelog '[$time_local] ' $image_file ' ' $image_type ' ' $body_bytes_sent ' ' $status; # 开启重写日志 rewrite_log on; server { root /home/www; location / { # 重写规则信息 error_log logs/rewrite.log notice; # 注意这里要用‘’单引号引起来,避免{} rewrite '^/images/([a-z]{2})/([a-z0-9]{5})/(.*)\.(png|jpg|gif)$' /data?file=$3.$4; # 注意不能在上面这条规则后面加上“last”参数,否则下面的set指令不会执行 set $image_file $3; set $image_type $4; } location /data { # 指定针对图片的日志格式,来分析图片类型和大小 access_log logs/images.log mian; root /data/images; # 应用前面定义的变量。判断首先文件在不在,不在再判断目录在不在,如果还不在就跳转到最后一个url里 try_files /$arg_file /image404.html; } location = /image404.html { # 图片不存在返回特定的信息 return 404 "image not found\n"; } } 

As for the form /images/ef/uh7b3/test.pngof request to rewrite /data?file=test.png, so matched to location /data, look at the /data/images/test.pngfile exists or not, if there is a normal response, if there is a new rewriting tryfiles image404 location, status code 404 directly returns.

Example 2 :

rewrite ^/images/(.*)_(\d+)x(\d+)\.(png|jpg|gif)$ /resizer/$1.$4?width=$2&height=$3? last; 

The shape of the /images/bla_500x400.jpgdocument request, to rewrite the /resizer/bla.jpg?width=500&height=400address and will continue to try to match location.

Guess you like

Origin www.cnblogs.com/echojson/p/11005361.html