色魔王子7 发表于 2018-11-12 08:13:34

Nginx location&rewrite

Nginx的Location可以有以下几个匹配:  1. =   严格匹配这个查询。如果找到,停止搜索。
  2. ^~ 匹配路径的前缀,如果找到,停止搜索。
  3. ~   为区分大小写的正则匹配
  4. ~* 为不区分大小写匹配
  例子:
  location = / {
  # matches the query / only.
  # 只匹配 / 查询。
  [ configuration A ]
  }
  location / {
  # matches any query, since all queries begin with /, but regular
  # expressions and any longer conventional blocks will be
  # matched first.
  # 匹配任何查询,因为所有请求都已 / 开头。但是正则表达式规则和长的块规则将被优先和查询匹配。
  [ configuration B ]
  }
  location ^~ /images/ {
  # matches any query beginning with /images/ and halts searching,
  # so regular expressions will not be checked.
  # 匹配任何已 /images/ 开头的任何查询并且停止搜索。任何正则表达式将不会被测试。
  [ configuration C ]
  }
  location ~* ".(gif|jpg|jpeg)$ {
  # matches any request ending in gif, jpg, or jpeg. However, all
  # requests to the /images/ directory will be handled by
  # Configuration C.
  # 匹配任何已 gif、jpg 或 jpeg 结尾的请求。然而所有 /images/ 目录的请求将使用 Configuration C。
  [ configuration D ]
  }
  Rewrite指令
  rewrite regex replacement flag
  后面的flag可以是:
  last - completes processing of rewrite directives, after which searches for corresponding URI and location
  break - completes processing of rewrite directives
  redirect - returns temporary redirect with code 302; it is used if the substituting line begins with http://
  permanent - returns permanent redirect with code 301
  需要注意的是,如果rewrite后,不要它再去匹配其它的location,则需要加上break,如:
  location ^~ /html/l/
  {
  root /opt/docparser;
  rewrite "^/html/l/(.*)$" /$1;
  #注意,需要有break,否则rewrite后还会继续匹配下面的location
  break;
  }
  location ~ /(.*)$ {
  proxy_pass http://127.0.0.1:8080;
  }

页: [1]
查看完整版本: Nginx location&rewrite