Nginx of URL rewriting (rewrite) Configuration

Nginx URL rewriting (the rewrite) Detailed configuration and information of
1) if a determination instruction

Syntax if (condition) {...} # conditions given condition is determined.
If true, rewrite instructions within the braces will be executed, if the condition (conditon) may be any of the following elements:

  a: When the expression is only one variable, if the value is null or in any string that begins with 0 as will be false, otherwise, it is true.
  a b: a direct comparison of the variables and the content, use or = =!
  C: regular expression matching, the * insensitive matching, and!! *on the contrary.

Note: Use regular expression string generally do not need quotation marks, but if it contains the closing brace "}" or semicolon ";" when the characters have to give the entire regular expression in quotes

Other instructions:
! -F -f and used to determine whether the requested file exists
-d -d and used to determine whether there is a request for directory!
-E -e and is used to determine whether the requested file or directory exists!
The -X-and! -x used to determine whether the requested file is the executable

Examples: IF (-f $ request_filename) {
        ... # determines whether the requested file exists, there exists on the implementation of the code block
    }

2) break command

Interrupting the current for the same scope Nginx configuration and the Java syntax similar to break, it can be used in the block and location server, and if block.
Syntax: break;

3) if the global variable available
there $ host_host variable, and $ host differences are as follows:
$ host without port, with the port $ HTTP_HOST


4) return instruction

 This command is used to complete the processing of the request, response status code directly to the client. And the return of Java syntax similar. And the server can then use the block and if the block location.

 Syntax: return code URL; #code indicates the state code, URL URL address represents the return to the customer orders
 or: return URL: # When the status code is 302 or 307, they can use the returned URL must include the "http: // "," https: // "or directly" $ scheme "variable (RequestScheme on behalf of the transport protocol,
the Nginx built-in variables)
  or return [text]; # to return to the body in response to the content client that supports the use of variables

5) rewrite instruction

 The instruction to change the URI. There may be one or more instructions at the same time, once a URL and processed in the order matching by using a regular expression. The instructions may be arranged at the latter location server block block

  Syntax: Command Syntax: the rewrite REGEX Replacement [In Flag];
   the rewrite instruction is important to achieve the redirection URL,  
   REGEX: URI to match regular expressions;
   Replacement: URI string matching is used to replace the intercepted content successfully, By default, if the string contains "http: //", "https : //" at the beginning, it will not continue down the URI other treatments. Returned directly to the client rewritten URI
    flag: set to the URI rewrite processing behavior, comprising the following data:

DESCRIPTION mark symbol
last termination process in the present location URI in the received block, and other URI rewritten here treated as a new location URI. (Just terminate processing of the current location of)
BREAK here will be rewritten as a new URI URI resumed in the current location, the new URI will not turn to other location.
The URI rewritten redirect client returns a status code 302 indicating a temporary redirection, mainly in the replacement string is not used to "http: //", "https : //" at the beginning or "$ scheme";
permanent rewritten URI returned to the client, the status code 301 indicating a permanent redirection;
. 6) instructions rewrite_log

The output of the instruction whether to open URL rewriting log
syntax:
  rewrite_log ON | OFF
  default is off, if configured as on, URL rewriting relevant log will notice the level of output to the log file configuration instructions in error_log

7) set of instructions

To set a new variable,
  the syntax: the SET variable value;
    variable, this is the name of the variable, the symbol "$" must be the first character as a variable, and can not Nginx server and global variables of the same name preset
    value, variable value.
For example, set $ id "3"; # set the id of 3

8)uninitialized_variable_warn指令

This command is used to configure the time will use uninitialized variables, whether the warning log records,
 syntax: unitialized_variable_warn on | off
  default setting on state

9) the security chain Examples

Syntax: valid_referers none | blocked | server_name | String ...;
1
here represents whether the request header field Referer match on face value, matching the $ invalid_referer if the value is 0, no match is 1;

Character Description
none shows a case Referer header field does not exist in
the case of value Referer header field to detect blocked by a firewall or proxy server to delete or disguise this case, the value of the header field not to "http: //" or "https: // "at the beginning
server_name provided one or more URL, Referer header field values detected whether a URL in these
10) examples

Examples of a (Domain Name Jump):
Server {
the listen 80;
server_name abc.com;
the rewrite ^ / HTTP (*.): Permanent //www.ab c.com/$1; # jumps to the URL www.abc.com
}
example two:
Server {
the listen 80;
server_name www.MyWeb.com www.web.info
IF ($ MyWeb Host ~ \ .info) requires {# "\" escape, this is matched to www.web "." when .info
the rewrite http://www.myweb.com/&1 permant ^ (*.); # http://www.myweb.com permanently redirected to the URL 1 is uri & matched
}
}
example tris (anti-theft chain ):
. ~ LOCATION * \ (GIF | JPG | PNG | SWF | FLV) {$
valid_referers none blocked www.vison.com www.wsvison.com; where # represents Referer header field value is none, or blocked, or these latter URL will return to normal gif | jpg | png | swf | flv file, otherwise if the following block of code
if ($ invalid_referer) {# above is not successfully matched, $ invalid_referer value of 1, otherwise 0
return 404;
} // anti-theft chain
}
: Other examples
IF (~ $ HTTP_USER_AGENT MSIE) {
(. *) The rewrite ^ $ / MSIE / $. 1 BREAK;
} // if the UA contains "MSIE", rewrite request to the next / msid / directory

IF (HTTP_COOKIE ~ $ * "ID = ([^;] +) (:;? | $)") {
SET $ $ id. 1;
} // if the cookie matches the regular, set the variable $ id is equal to the positive reference part

IF (= $ REQUEST_METHOD the POST) {
return 405;
} // If submission method is POST, the state is returned 405 (Method not allowed). return not return 301,302

IF ($ SLOW) {
limit_rate 10K;
} // speed limit, $ slow instruction set may be provided by

IF (-f $ request_filename!) {
BREAK;
proxy_pass of http://127.0.0.1;
} // if the requested file does not exist, to the reverse proxy localhost. Break here also stop rewrite check

IF (POST = 140 ~ $ args) {
the rewrite with http://example.com/ Permanent ^;
} // if the query string contains "post = 140", the permanent redirect example.com

Guess you like

Origin www.cnblogs.com/cangqinglang/p/11124209.html