Example of Location routing reverse proxy and rewriting policy applied by Nginx

  • 2020-05-13 04:43:59
  • OfStack

1. Common Settings

1 the & # 65380; Log format


log_format main '$time_iso8601|$remote_addr|$remote_user|$request_method|$uri|'
     '$status|$request_time|$request_length|$body_bytes_sent|$bytes_sent|'
     '$connection|$http_x_forwarded_for|$upstream_addr|$upstream_status|'
     '$upstream_response_time|$args|$http_referer|$http_user_agent';
access_log logs/access.log main;

2 the & # 65380; Reverse proxy passes client IP Settings


proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

3 the & # 65380; The global variable


$args # This variable is equal to the argument in the request line. 
$content_length # In the request header Content-length Field. 
$content_type # In the request header Content-Type Field. 
$document_root # Current request in root A value specified in an instruction. 
$host # Request the host header field, otherwise the server name. 
$http_user_agent # The client agent information 
$http_cookie # The client cookie information 
$limit_rate # This variable can limit the connection rate. 
$request_body_file # The client requests the temporary file name of the principal information. 
$request_method # The action requested by the client, usually GET or POST . 
$remote_addr # The client's IP Address. 
$remote_port # Client port. 
$remote_user # Have already been Auth Basic Module The authenticated user name. 
$request_filename # The current requested file path, by root or alias Command and URI Request generation. 
$query_string # with $args The same. 
$scheme #HTTP Methods (such as: http . https ). 
$server_protocol # Request to use the protocol, usually HTTP/1.0 or HTTP/1.1 . 
$server_addr # Server address at completion 1 This value can be determined after the next system call. 
$server_name # Server name. 
$server_port # The port number at which the request arrives at the server. 
$request_uri # Contains the original of the request parameter URI , without the host name, as in:" /foo/bar.php?arg=baz ". 
$uri # Current without request parameters URI . $uri Does not contain a host name, such as" /foo/bar.html ". 
$document_uri # with $uri The same. 

2. Rewrite rules

Syntax: rewrite regular substitution flag bit

flag marker (the last parameter of the rewrite directive) :

1.last last terminates the current rewrite detection of location, but will continue to retry location to match and process rewrite rules in the block.

2.break break terminates the rewrite detection of the current location and no longer matches location.

3.redirect returns 302 temporary redirection, and the browser address displays the URL address after the jump.

4. When permanent returns a 301 permanent redirect, the browser address will display the URL address after the jump.

Ex. :


#  Regular match 
location ~ ^/(a|bb|ccc)/ {
  rewrite ^/([a-z]+)/(.*)$ http://106.185.48.229/$2?$1;
}
#  Note: the parameters enclosed in parentheses are the following  $1 $2  variable 

3. Reverse proxy routing strategy

Configuration of Location:

Grammar:

location [= | | | ~ ~ * ^ ~] / uri / {... }
Grammatical instructions:

The beginning of = indicates an exact match. Regularization is not supported.

^~ means that uri begins with a regular string. It does not support regex. It can be interpreted as matching the url path.

The ~ and ~* start with regular matching of case-sensitive and case-insensitive cases.

! ~ and! The ~* beginning represents a regular match that is case-insensitive and case-insensitive.

General match, any request will be matched, usually at the end of the configuration.

Matching priority:

= > ^~ > ~, ~* > empty

The whole match > Path matching > Regular match > String matching

Example:


#  String matching 
location /static {
  alias /home/www/static;
  access_log off;
}
#  Path match, at this point proxy_pass The end of the  /  Decide whether or not to take a matching path 
location ^~ /333/ {
  proxy_pass http://106.185.48.229/;
}
#  Regular matching, at this point proxy_pass Can't end with  /
location ~ ^/(xxx|yyy)/ {
  proxy_pass http://106.185.48.229;
}
#  String match, at this point proxy_pass The end of the  /  Decide whether to bring the matched path or not 
location /zzz/ {
  proxy_pass http://106.185.48.229/;
}
#  The default matching 
location / {
  proxy_pass http://127.0.0.1:8080;
}

Related articles: