1024programmer Nginx Detailed instructions related to NginxRewrite rules

Detailed instructions related to NginxRewrite rules

Nginx Rewrite rule-related instructions

Relevant instructions include if, rewrite, set, return, break, etc., the most critical of which is rewrite. A simple Nginx
The syntax of the Rewrite rule is as follows: rewrite ^/b/(.*)\.html /play.php?video=$1
break;

1.break command
Default value: none ; Environment: server, location, if ;
The function of this instruction is to complete the current rule set and no longer process the rewrite instruction.

2.if instruction
Default value: none; Use environment: server, location
This instruction is used to check whether a condition is met, and if the condition is met, execute the statement inside the braces. The If instruction does not support nesting, and does not support multiple conditions && and || processing.

A. Variable name, the wrong value includes: empty string “” or any string starting with 0

B. Variable comparison can use “=” (meaning equal) and “!=” (meaning not equal)

C. Regular expression pattern matching can use “~*” and “~” symbols

D. The “~” symbol indicates a case-sensitive match

E. The “~*” symbol indicates a case-insensitive match

F. The functions of “!~” and “!~*” symbols are just opposite to those of “~” and “~*”, indicating that they do not match

G. “-f” and “!-f” are used to determine whether the file exists

H.”-d” and “!-d” are used to determine whether the directory exists

I. “-e” and “!-e” are used to determine whether a file or directory exists

J. “-x” and “!-x” are used to determine whether the file is executable

K. Some regular expressions can be accessed in () with $1~$9

3. return command
Syntax: return code ; Environment: server, location, if ;
This command is used to end the execution of the rule and return the status code to the client.
Example: If the accessed URL ends with “.sh” or “.bash”, return 403 status code location ~
.*\.(sh|bash)?${return 403;}
4. rewrite command
Syntax: rewrite regex replacement flag
Default value: none ; Use environment: server,location,if
This directive redirects a URI based on an expression, or modifies a string. Instructions are executed according to the order in the configuration file. Note that rewriting expressions are only valid for relative paths. If you want to pair hostnames, you should use an if statement, for example: if(
$host ~* www\.(.*) ){set $host_without_www
$1;rewrite ^(.*)$ http://$host_without_www$1 permanent;}

The last parameter of the rewrite command is the flag tag, and the supported flag tags are:
1.last is equivalent to the [L] mark in apache, which means rewrite.
2.break After the matching of this rule is completed, the matching is terminated, and the following rules are no longer matched.
3.redirect returns 302 temporary redirection, and the browser address will display the redirected URL address.
4. permanent returns 301 permanent redirection, and the browser address will display the redirected URL address.

Use last and break to realize URI rewriting, and the browser address bar remains unchanged. And there are subtle differences between the two, the last mark must be used when using the alias command; the break mark needs to be used when using the proxy_pass command. After the execution of this rewrite rule, the Last tag will re-initiate the request to the server{…} tag where it is located, and the break tag will terminate the matching after the matching of this rule is completed.

Generally in the following location (location
/{…}) or write rewrite rules directly in the server tag, it is recommended to use the last tag; in non-root location (location
/cms/{…}), use break.

If the URI contains parameters (/app/test.php?id=5), the parameters will be automatically appended to the replacement string by default, you can solve this problem by adding a ? mark at the end of the replacement string.
For example: rewrite
^/test(.*)$ http://www.tiyee.net/home permanent;
Visiting http://www.tiyee.net/test?id=5 will jump to http://www.tiyee.net/home?id=5

Example: If we redirect a URL like /photo/123456 to
/path/to/photo/12/1234/123456.pngRewrite
“/photo/([0-9]{2})([0-9]{2})([0-9]{2})”
/path/to/photo/$1/$1$2/$1$2$3.png ;

Note: If there are curly braces “{” or “}” in the regular expression, double quotes or single quotes should be used.

5. Set command
Syntax: set variable value ; Default value: none ; Environment: server,location,if;
This instruction is used to define a variable and assign a value to the variable. The value of a variable can be text, a variable, and a union of text variables.
Example: set $varname “hello world”;

6. Uninitialized_variable_warn instruction
Syntax: uninitialized_variable_warn on|off
Use environment: http, server, location, if
This command is used to turn on and off the warning message of uninitialized variables, and the default value is on.

7. Global variables that Nginx Rewrite can use

$args , $content_length , $content_type , $document_root
,$document_uri ,$host ,$http_user_agent ,$http_COOKIE ,$limit_rate
,$request_body_file ,$request_method ,$remote_addr ,$remote_port
,$remote_userttp_variable_sent_content_length, 0, 0, 0 }, {
ngx_string(“sent_http_location”),
NULL, ngx_http_variable_sent_location, 0, 0, 0 }, {
ngx_string(“sent_http_last_modified”),
NULL, ngx_http_variable_sent_last_modified, 0, 0, 0 }, {
ngx_string(“sent_http_connection”),
NULL, ngx_http_variable_sent_connection, 0, 0, 0 }, {
ngx_string(“sent_http_keep_alive”),
NULL, ngx_http_variable_sent_keep_alive, 0, 0, 0 }, {
ngx_string(“sent_http_transfer_encoding”),
NULL, ngx_http_variable_sent_transfer_encoding, 0, 0, 0 }, {
ngx_string(“sent_http_cache_control”), NULL,
ngx_http_variable_headers, offsetof(ngx_http_request_t,
headers_out. cache_control), 0, 0 }, { ngx_string(“limit_rate”),
ngx_http_variable_request_set_size, ngx_http_variable_request_get_size, offsetof(ngx_http_request_t,
limit_rate),NGX_HTTP_VAR_CHANGEABLE|NGX_HTTP_VAR_NOCACHEABLE, 0 },{
ngx_string(“nginx_version”), NULL,
ngx_http_variable_nginx_version, 0, 0, 0 }, { ngx_string(“hostname”),
NULL, ngx_http_variable_hostname, 0, 0, 0 }, { ngx_string(“pid”),
NULL, ngx_http_variable_pid, 0, 0, 0 }, { ngx_null_string, NULL,
NULL, 0, 0, 0 }};
Extract these variables and summarize them as follows:

arg_PARAMETER #This variable contains the value of the variable PARAMETER in the GET request.

args
#This variable is equal to the parameters in the request line (GET request), such as foo=123&bar=blahblah;

binary_remote_addr #Binary customer address.

body_bytes_sent #The number of body bytes sent in response. This data is accurate even if the connection is interrupted.

content_length #The Content-length field in the request header.

content_type
#The Content-Type field in the request header.

COOKIE_COOKIE #COOKIE
The value of the COOKIE variable

document_root #The current request specifies the value in the root command.

document_uri #with

The uris are the same.

host
#Request host header field, otherwise server name.

hostname
#Set to the machine’s hostname as returned by gethostname

http_header

is_args #if any

args parameter, this variable is equal to “?”, otherwise it is equal to “”, empty value.

http_user_agent #Client agent information

http_COOKIE #Client COOKIE information

limit_rate
#This variable can limit the connection rate.

query_string #with

args are the same.

request_body_file #The temporary file name of the client request body information.

request_method #The action requested by the client, usually GET or POST.

remote_addr #Client’s IP address.

remote_port #The port of the client.

remote_user #Has passed Auth
Username for Basic Module authentication.

request_completion #If the request ends, set to OK.
Empty when the request is not complete or if the request is not the last in the chain of requests.

request_method #GET or POST

request_filename #The file path of the current request, generated by the root or alias command and the URI request.

request_uri #Include the original URI of the request parameters, excluding the host name, such as: “/foo/bar.php?arg=baz”. Can not be modified.

scheme
#HTTP method (such as 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 where the request arrives at the server.

p; #Server address, this value can be determined after completing a system call.

server_name #Server name.

server_port #The port number where the request arrives at the server.

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/detailed-instructions-related-to-nginxrewrite-rules/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索