nginx rewrite static and dynamic separation

rewrite

rewrite the regular expression to replace the target [flag]
difference nginx in rewrite and location of:
rewrite the location of the function a bit like the main difference: rewrite is to change the access to resources within the same domain path,
and location is the path to do to control access or reverse proxy may be used to proxy_pass another host, in many cases
rewrite also written in the location in which the order of execution -
[1] rewrite instruction execution server block
[2] perform matching location
[3] perform the selected location in the rewrite instructions
Note: If a certain part of the URI is rewritten, re-circulation 1-3, until you find the file real, cycle Supercharged
10 times, 500 Internal server error error is returned (server encountered an unexpected the condition that causes
it could not complete the processing of the request. in general, this problem will occur when server-side source code error.)
·················································· ·················································· ··························
a, and regular Nginx location match
. 1, location rule matching
^ ~: identifier followed by a string matching after the string is stopped on the subsequent regular expression matching
=: precise match
~: case-insensitive matching
~ *: not sensitive match
! -: negated on a case-sensitive match
! ~ *: Taking the non-case-insensitive matching
/: generic matches, if no other matches, any requests are matched to
2, the regular expression
*: Repeat the previous character zero or more times
:? Repeat the previous character or 0 or 1 time
+: repeat the previous character one or more times
: match any character other than a newline.
(a | B): a matching or B
^: beginning ......
$: ...... end to
{n}: n times repeating the previous character
{n,}: repeat the previous character at least n times
{n, m}: repeat the previous character to m times n
* repetition of the preceding character ?: zero or more times; do may be less repeat
+ ?: repeat the previous character one or more times; less repeated as
??: repeat the previous character or 0 or 1; less repeated as
{n, m} ?: repeat the previous character to m times n; repeated as few
{n,} ?: previous character is repeated at least n times; less repeated as
3, the regular expression added
\ W: not match the letter , numbers, characters, underlined characters
\ S: matching is not whitespace characters
\ D: matching non-numeric characters
\ B: matching is not a word beginning or end of the character
[a]: matching a single character a
[az]: match az
[^ az]: non-matching characters az of
4, location rules apply
location [= | ~ | ~ * | ^ ~ |! ~ |! ~ *] / Url {......}
default: no
use fields: Server
. 5, the matching priority
=: the highest priority matching precision
full path |
^ ~ path |
regular expressions |
partial path V
/ lowest priority
6, practical recommendations
1) direct match with the site, visit the Web site home page through the domain name more frequently, the use of this process will accelerate
LOCATION = / {
   proxy_pass HTTP: // Tomcat: 8080;
}
2) static files request.
Directory matches:
LOCATION ^ ~ / static / {
the root / usr / local / Nginx / jTML / static;
}
suffix match:
LOCATION ~ * \ (GIF | JPG | JPEG | PNG | CSS | JS | ICO) {.
    The root / Webroot / RES /;
}
. 3) General rules; dynamic requests to the backend application server for forwarding
LOCATION / {
proxy_pass HTTP: // Tomcat: 8080 /;
}
two, the rewrite rule
1, function:
using global variables or provided nginx to set up their own variable, a regular expression and binding realize flag bits and redirect url rewriting, the rewrite only on the server {}, location {}, if {} in, and removed only for the domain name is transmitted back to the parameters string work.
2, the syntax:
rewrite replace certain regular expression [] flag
execution order:
execution Server block rewrite instruction
execution Location matching
rewrite instruction is executed Location
Note: If a step url is rewritten, the re-execution 1- 3, know where to find the file, circulation more than 10 times, return 500
error
3, flag flag
last: represent complete rewrite (on behalf of the end of the action)
BREAK: After matching this rule, termination matching, not matching rules behind the
redirect: return to 302 temporary redirect, the browser address bar displays the address after the jump
permanent : returns 301 permanent redirect, the browser address bar displays the address after the jump
Note: last generally written in the server and if in general use in the location and break the
last match does not terminate after url rewriting, that will be the new url and then again away from the matching server process, and after the termination of rewriting matching break
break and can last tissue continues behind rewrite instruction
4, if the determined global variable
syntax: IF (for condition Condition) {.....}
= and ! =: Compare variable content
! -F and -f: used to determine if a file exists
-d and -d:! Used to determine whether there is a directory
-e and -e:! Used to determine whether there is a file or directory
-x and! -x: used to determine whether an executable
5, nginx built-in variable
$ args: # this variable is equal to the parameter request line, with $ QUERY_STRING (things behind?)
$ CONTENT_LENGTH: request Content-length header field
$ content_type: request Content-Type header field
$ document_root: request the current value specified in the instruction root
$ 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 requested action, usually GET or POST
$ remote_addr: client IP address
$ remote_port: client port
$ remote_user: has elapsed Auth Basic Module authenticated user name
$ request_filename: the file path of the current request, a request by the root or alias instruction URI generated
$ scheme: hTTP method (e.g., http , HTTPS)
$ SERVER_PROTOCOL: protocol requests used, usually HTTP / 1.0 or the HTTP / 1.1
$ the server_addr: server address, can be determined this value after the completion of a system call
$ server_name: server name
$ server_port: request reaches the server port number
$ request_uri: contains the original request URI parameters, do not include the host name, such as: "? / foo / baz bar.php Arg ="
$ uri: with no request parameters of the current URI, $ uri does not contain a host name, such as "/ foo / bar.html "
$ DOCUMENT_URI: the same as the $ uri

Guess you like

Origin www.cnblogs.com/elin989898/p/11911575.html