Location routing reverse proxy and rewrite strategy for Nginx applications

1. Common settings

1、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. Reverse proxy transparent transmission 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、Global variables

copy code

$args #This variable is equal to the arguments in the request line.
$content_length #Content-length field in the request header.
$content_type #Content-Type field in the request header.
$document_root #The value specified in the root directive for the current request.
$host #Request host header field, otherwise the server name.
$http_user_agent #Client agent information
$http_cookie #Client cookie information
$limit_rate #This variable can limit the connection rate.
$request_body_file #Temporary file name of client request body information.
$request_method #The action requested by the client, usually GET or POST.
$remote_addr #IP address of the client.
$remote_port #The port of the client.
$remote_user #The username that has been authenticated by the Auth Basic Module.
$request_filename #The file path of the current request, generated by the root or alias directive and the URI request.
$query_string #same as $args.
$scheme #HTTP method (eg http, https).
$server_protocol #The protocol used by the request, usually HTTP/1.0 or HTTP/1.1.
$server_addr #Server address, this value can be determined after completing a system call.
$server_name #Server name.
$server_port #The port number of the request to the server.
$request_uri #Contains the original URI of the request parameters, excluding the hostname, such as: "/foo/bar.php?arg=baz".
$uri #The current URI without request parameters, $uri does not contain the hostname, such as "/foo/bar.html".
$document_uri #Same as $uri.

copy code

2. Rewrite rules

Syntax: rewrite regular replacement flag

flag flag (the last parameter of the rewrite command):

1.last   last是终止当前location的rewrite检测,但会继续重试location匹配并处理区块中的rewrite规则。

2.break  break是终止当前location的rewrite检测,而且不再进行location匹配。

3.redirect  返回302临时重定向,浏览器地址会显示跳转后的URL地址。

4.permanent  返回301永久重定向,浏览器地址会显示跳转后的URL地址。

example:

# regex 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 variables

3. Routing strategy of reverse proxy

Location configuration:

grammar:

location [=|~|~*|^~] /uri/ {…}

Syntax Description:

=   开头表示精确匹配,不支持正则。

^~  开头表示uri以某个常规字符串开头,不支持正则,理解为匹配url路径即可。

~和~* 开头表示区分大小写的和不区分大小写的正则匹配。

!~和!~* 开头表示区分大小写不匹配及不区分大小写不匹配的正则匹配。

/ 通用匹配,任何请求都会匹配,通常放着配置的最后。

 

Match priority:

= > ^~ > ~, ~* > 空

全匹配 > 路径匹配 > 正则匹配 > 字符串匹配

 

Example:

copy code

# string match
location /static {
    alias  /home/www/static;
    access_log off;
}
# Path matching, at this time the end of proxy_pass / decide whether to bring the matching path
location ^~ /333/ {
    proxy_pass http://106.185.48.229/;
}
# Regular match, proxy_pass cannot end with /
location ~ ^/(xxx|yyy)/ {
    proxy_pass http://106.185.48.229;
}
# String matching, at this time the end of proxy_pass / decide whether to bring the matching path
location /zzz/ {
    proxy_pass http://106.185.48.229/;
}
# default match
location / {
    proxy_pass http://127.0.0.1:8080;
}

copy code

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324431767&siteId=291194637