nginx common location rewrite proxy_pass

location

Begin =, exact matching; if only the root end of the directory matches the request, can not be followed with any character string.
~ ^ Beginning, showing uri string beginning with a conventional, if matched, will not continue to match down. Not regular match
beginning ~, it represents a case-sensitive regular matching;
to ~ the beginning * indicates a case-insensitive regular matching
begins with a /, generic matches, if no other matches, any requests are matched to the
** Note location xxx wherein the {} brackets between xxx and require a lot of space, are preferably combined with *

Matching order:

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

rewrite

rewrite only on the server {}, location {}, if {}, and only after the file path name of the function.

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

rewrite regex replacement [flag];

flag

Here divided into stages and location server level, wherein if the server can be written and the location, and level corresponding location server level. The same level of execution in order to see the written order.
last: no longer perform at the same level rewrite, write again in a location matching the location.
BREAK: no longer perform at the same level rewrite, continue down, does not match the re-location;
redirect: return to 302 temporary redirect, the address bar will show jumping after the address
permanent: return 301 permanent redirect, the address bar will show the address after the jump
when not writing flag, again matching the same level of circulation

if(condition)

When the expression is only a variable, the value is null, or if any string that begins with 0 as false will
direct comparison of the variables and the contents, or use =! =
~ Regular expression matching
~ * insensitive matching of
! ~ not case-sensitive match
-f and! -f used to determine whether a file exists
-d and! -d to whether or not directory
-e and! -e used to determine whether there is a file or directory
-x and! -x It is used to determine whether an executable file

other

debugging
   default_type    application/json;
   在locaiton中 return 200 '$uri xxx' 
variable
set $a "1"
if ($a = "1") {
  return 302
}
proxy_pass

proxy_pass http://127.0.0.1:8008/;
Proxy_pass discussed here only in the location of;

1, location using a non-regular match

location /api {
 # 1. proxy_pass http://127.0.0.1:8008/a;
 # 2. proxy_pass http://127.0.0.1:8008;
}
  • A rear port in the "/", access / api / test → / a / test; (matching the remaining address / Test, then spliced ​​to the address proxy address)
  • 2. The rear port is no "/" Access / api / test → / api / test
    to be noted here is not whether there is finally uri "/" is whether port behind the "/"

2, location matching using regular

location ~ /api/ {
  proxy_pass http://127.0.0.1:8008;
}
  • The rear port without / feasible.
  • The rear port add /, you must use variables (variables any row, allowed the use of Rule 3). Because the proxy address will automatically add the rest of the match uri, agents to match the rest of the regular uri will address hell broke loose.

3, if the latter have proxy_pass variables that go directly to the address, out of 12 rules.

For example

server {
  listen       80;
  rewrite /a(.*) /b$1;
  rewrite /b(.*) /c$1 last;
  rewrite /c(.*) /d$1 break;
  location / {
    if ($uri ~ /d/) {
      rewrite /d/(.*) /api/$1 last;
    }
    return 200 '$uri';
  }
  location /api {
    proxy_pass http://127.0.0.1:8000/test; #端口号后面有/,代理删掉/api。
  }
  location ~ /t/(.*)/t {
    proxy_pass http://127.0.0.1:8000/test1/$1; 
    #proxy_pass http://127.0.0.1:8000/test1; 写法报错,必须使用变量,使用第3条代理规则。
  }
}
  1. Access / test returns / test
  2. Access / aaa return / caa
  3. Access / api return / cpi
  4. Access / d / dd agent to http://127.0.0.1:8000/test/dd
  5. Access / ee / t / test2 / t / every agency to go http://127.0.0.1:8000/test1/test2

Guess you like

Origin www.cnblogs.com/gsgs/p/11497587.html