Nginx Rewrite rules and instances

Source: Internet
Author: User
The Rewrite rules of Nginx and the commands related to instance NginxRewrite rules include if, rewrite, set, return, and break. The most important of these commands is rewrite. A simple NginxRewrite rule syntax is as follows: rewrite ^/B /(. *)\. html/play. php? Video... the Rewrite rules of Nginx and the commands related to the Nginx Rewrite rules of the instance include if, rewrite, set, return, and break. The most important of these commands is rewrite. A simple Nginx Rewrite rule syntax is as follows: rewrite ^/B /(. *)\. html/play. php? Video = $1 break; 1. default value of the break command: none; environment: server, location, if; this command is used to complete the current rule set and does not process the rewrite command. 2. if command default value: none; use environment: server, location this command is used to check whether a condition is met, if the condition is met, execute the statement in braces. The If command does not support nesting and does not support multiple conditions & |. A. variable name. the error values include: Null string "" or any string starting with 0. for variable comparison, you can use "=" (equal to) and "! = "(Not equal to) C. Regular expression pattern matching can be used "~ * "And "~ "Symbol D ."~ "Symbol: matching E ."~ * "The symbol indicates matching F ."!~ "And "!~ * "The role of the symbol is exactly the same "~ ","~ * "On the contrary, G."-f "and "! -F "is used to determine whether the file exists. H."-d "and "! -D "is used to determine whether the directory contains I."-e "and "! -E "is used to determine whether a file or directory contains J."-x "and "! -X "is used to determine whether the file is executable. some regular expressions can be in (), with $1 ~ $9 to access 3. return command syntax: return code; environment: server, location, if; this command is used to end rule execution and return the status code to the client. Example: If the Accessed URL ends with ". sh" or ". bash", status code 403 location ~ is returned ~. * \. (Sh | bash )? $ {Return 403;} 4. rewrite command syntax: rewrite regex replacement flag default value: none; use environment: server, location, if this command redirects the URI according to the expression, or modify the string. The command is executed according to the order in the configuration file. Note that the rewrite expression is only valid for relative paths. If you want to pair the host name, you should use the 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. the flag can be 1. last is equivalent to the [L] Mark in apache, indicating rewrite. 2. after the break rule is matched, the match is terminated and the subsequent rule is no longer matched. 3. redirect returns the 302 temporary redirection. the browser address will display the URL address after the jump. 4. permanent returns 301 permanent redirection. the browser address will display the URL address after the jump. Use last and break to rewrite the URI. the address bar of the browser remains unchanged. In addition, there are minor differences between the two. the alias command must use the last tag. when using the proxy_pass command, you must use the break tag. After the Last flag is executed for this rewrite rule, it will be directed to its server {......} the tag re-initiates the request, and the break tag terminates the match after the rule is matched. Usually in the same location (location /{...}) or write rewrite rules directly in the server tag. we recommend that you use the last tag. in non-root location (location/cms /{...}), use break. If the URI contains parameters (/app/test. php? Id = 5). by default, the parameter is automatically appended to the replacement string. you can add it at the end of the replacement string? Mark to solve this problem. Example: rewrite ^/test (. *) $ http://www.xiaozhe.com /Home permanent; access http://www.xiaozhe.com /Test? Id = 5 will jump http://www.xiaozhe.com /Home? Id = 5 for example: if we redirect a URL like/photo/123456 to/path/to/photo/12/1234/123456.png Rewrite "/photo/([0-9] {2 }) ([0-9] {2}) ([0-9] {2}) "/path/to/photo/$1/$1 $2/1_11_21_3.png; note: if the regular expression contains curly brackets "{" or "}", double quotation marks or single quotation marks should be used. 5. Set command syntax: set variable value; default value: none; environment: server, location, if; this command is used to define a variable and assign values to the variable. The value of a variable can be the combination of text, variables, and text variables. Example: set $ varname "hello world"; 6. uninitialized_variable_warn command syntax: uninitialized_variable_warn on | off Environment: http, server, location, if this command is used to enable and disable the warning information of uninitialized variables. the default value is enable. 7. global variables $ 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_user, $ request_filename, $ request_uri, $ query_string, $ scheme, $ server_protocol, $ server_addr, $ server_name, $ server_port, $ uri. compile Nginx Rewrite rules for example 1. when the Accessed file and directory do not exist, redirect to a php file if (! -E $ request_filename) {rewrite ^/(. *) $ index. php last;} 2. Directory swap/123456/xxxx ==>/ xxxx? Id = 123456 rewrite ^/(\ d +)/(. +) // $2? Id = $1 last; 3. if the client uses the ie browser, redirect to the/ie directory if ($ http_user_agent ~ MSIE) {rewrite ^ (. *) $/ie/$1 break;} 4. prohibit access to multiple directories location ~ ^/(Cron | templates)/{deny all; break;} 5. prohibit access to the file location starting with/data ~ ^/Data {deny all;} 6.forbidden location ~. * \. (Sh | flv | mp3) $ {return 403;} 7. set the browser cache time location for some types of files ~. * \. (Gif | jpg | jpeg | png | bmp | swf) $ {expires 30d;} location ~. *\. (Js | css) $ {expires 1 h;} comparison between Nginx and Apache Rewrite rule instances 1. generally, simple Nginx rules are slightly different from Apache rules and are fully compatible. for example, Apache: RewriteRule ^/abc/$/web/abc. php [L] Nginx: rewrite ^/abc/$/web/abc. php last; we can see that you only need to change Apache RewriteRule to Nginx rewrite, and Apache [L] to last. If you change Apache rules to Nginx rules and run the Nginx-t command to check for errors, you can try to enclose the conditions with quotation marks, for example, rewrite "^/(0-9]%5%%%.html $"/x. php? Id = $1 last; 2. the Rewrite rules of Apache and Nginx are slightly different in URL redirection: Apache: RewriteRule ^/html/([a-zA-Z] + )/. * $/$1/[R = 301, L] Nginx: rewrite ^/html/([a-zA-Z] + )/. * $ http: // $ host/$1/premanent; we can see that http: // $ host must be added to the Nginx redirect, this is strongly required in Nginx. 3. below are some mappings between Apache and Nginx rules.. apache RewriteCond corresponds to Nginx ifb. apache RewriteRule corresponds to Nginx rewritec. apache [R] corresponds to Nginx redirectd. apache [P] corresponds to Nginx laste. apache [R, L] corresponds to Nginx redirectf. apache [P, L] corresponds to Nginx lastg. apache's [PT, L] corresponds to Nginx's last such as: allows the specified domain name to access this site, and all other domain names are switched to www. xiaozhe. comApache: RewriteCond % {HTTP_HOST }! ^ (.*?) \. Aaa \. com $ [NC] RewriteCond % {HTTP_HOST }! ^ Localhost $ RewriteCond % {HTTP_HOST }! ^ 192 \. 168 \. 0 \.(.*?) $ RewriteRule ^/(. *) $ http://www.xiaozhe.com [R, L] Nginx: if ($ host ~ * ^ (. *) \. Aaa \. com $) {set $ allowHost '1';} if ($ host ~ * ^ Localhost) {set $ allowHost '1';} if ($ host ~ * ^ 192 \. 168 \. 1 \.(.*?) $) {Set $ allowHost '1';} if ($ allowHost !~ '1') {rewrite ^/(. *) $ http://www.xiaozhe.com Redirect ;}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.