Linux10.9 rewrite configuration

Source: Internet
Author: User

Domain jump (redirect), url rewrite (pseudo-static), Static and dynamic separation (jump domain name, and Access CDN for acceleration)
Rewrite relies on Pcre library
Module: Ngx_http_rewrite_module

Nginx's rewrite configuration--if

If directive

Format: if (conditional judgment) {specific rewrite rule}

Examples of conditions

The conditional Judgment statement consists of Nginx built-in variables, logical judgment symbols and target strings. Among them, the built-in variables are nginx fixed non-custom variables, such as, $request _method, $request _uri and so on. Logical judgment symbols, with =,! =, ~, ~*,!~,!~*! mean the opposite, ~ to match the symbol, to the right of the regular expression, to the case, and ~* to the case-insensitive match. The target string can be a regular expression, usually without quotation marks, but when there are special symbols in the expression, such as spaces, curly braces, semicolons, etc., you need to enclose them in single quotation marks.

Example

if ($request _method = post)  //When the requested method is post, return the 405 status code {    return 405;//The rewrite rule is not used in the example, and if is supported with a return instruction. }if ($http _user_agent ~ MSIE)//user_agent request with MSIE character, directly returns 403 status code {    return 403;} If you want to limit multiple user_agent at the same time, you can also write this if ($http _user_agent ~ "Msie|firefox|spider") {    return 403;} if (!-f $request _filename)  //When the requested file does not exist, the following rewrite rule {Rewrite statement will be executed    ;} if ($request _uri ~* ' gid=\d{9,12}/')  //\d represents a number, {9,12} indicates that the number occurs 9 to 12 times, such as gid=123456789/is eligible. {    rewrite statement;}

Nginx's rewrite configuration--break and last
The two instruction usage is the same, but the meaning is different, it needs to be placed at the end of the rewrite rule to control whether the rewritten link continues to be executed by Nginx configuration (mainly rewrite, return instructions). Example 1 (two consecutive rewrite rules): server{    listen;     server_name test.com;    root/tmp/123.com;    rewrite/1.html/2.html;    rewrite/2.html/3.html;    } When we request 1.html, the final access to the 3.html, two rewrite rules are executed successively.

You can configure rewrite_log on within the server, open Errorr_log logs/error.log notice in nginx.conf, and only notice level will record errorlog in Rewrite_log.

Examples are as follows:

[[email protected] ~]# cat/usr/local/nginx/conf/vhost/www.1.com.confserver {Listen 80;        server_name www.1.com;        root/data/wwwroot/www.1.com;        Index welcome.html;        Rewrite_log on;        rewrite/1.html/2.html; rewrite/2.html/3.html;} Open nginx.conf in error_log notice configuration [[email protected] ~]#/usr/local/nginx/sbin/nginx-tnginx:the Configuration file/usr/local/nginx/conf/nginx.conf syntax is oknginx:configuration file/usr/local/nginx/conf/nginx.conf test is Successful[[email protected] ~]#/usr/local/nginx/sbin/nginx-s reload[[email protected] ~]# Curl- x127.0.0.1:80 www.1.com/1.html333333[[email protected] ~]# cat/usr/local/nginx/logs/error.log ... 2018/07/19 21:17:13 [notice] 2166#0: "/1.html" matches "/1.html", client:127.0.0.1, server:www.1.com, Request: "GET H Ttp://www.1.com/1.html http/1.1 ", Host:" Www.1.com "2018/07/19 21:17:13 [notice] 2166#0: * Rewritten data:"/2.html ", arg S: "", client:127.0.0.1, Server:www.1.com, Request: "GET HTTP://www.1.com/1.html http/1.1", Host: "Www.1.com" 2018/07/19 21:17:13 [notice] 2166#0: "/2.html" matches "/2.html", client:127.0.0.1, server:www.1.com, Request: "GET HTTP://www.1.com/1.html http/1.1", hos T: "www.1.com" 2018/07/19 21:17:13 [notice] 2166#0: * * Rewritten data: "/3.html", args: "", client:127.0.0.1, Server:www. 1.com, Request: "GET HTTP://www.1.com/1.html http/1.1", Host: "Www.1.com"

If you add break or last after rewrite/1.html/2.html in the server, the second line will not execute.

Break and last are outside location {}
Format: rewrite xxxxx break  ; Example 2 (add break): server{    listen;     server_name test.com;    root/tmp/123.com;    Rewrite/1.html/2.html break;    rewrite/2.html/3.html;} When we request 1.html, the final access to the 2.html description of break in this example, the effect is to no longer execute the rewrite rule below the break. However, when there is a location in the configuration file, it also executes the configuration of the location{} segment (the request is to match the location).
Example 3 (there is a location segment after break): server{    listen;     server_name test.com;    root/tmp/123.com;    Rewrite/1.html/2.html break;    rewrite/2.html/3.html;    location/2.html {        return 403;    }} When 1.html is requested, the 403 status code is eventually returned, indicating that it matches the location{} configuration after break. Example 2 and Example 3, you can replace break with last, both of which have the same effect.
When break and last are inside location{}
Example 4 (Nothing added): server{listen 80;    server_name test.com;        root/tmp/123.com;        Location/{rewrite/1.html/2.html;    rewrite/2.html/3.html;    } location/2.html {rewrite/2.html/a.html;    } location/3.html {rewrite/3.html/b.html; }} When the request is/1.html, it will eventually access the/b.html, performing the location/under two consecutive rewrite, jumping to/3.html, and then matching location/3.html example 5 (add break): server{Listen     80;    server_name test.com;        root/tmp/123.com;        Location/{rewrite/1.html/2.html break;    rewrite/2.html/3.html;    } location/2.html {rewrite/2.html/a.html;    } location/3.html {rewrite/3.html/b.html; }} When the request is/1.html and eventually accesses/2.html inside the location{}, all instructions within this location{and all subsequent location{} are no longer executed.     Example 6 (add last): server{listen 80;    server_name test.com;        root/tmp/123.com;        Location/{rewrite/1.html/2.html last;    rewrite/2.html/3.html;  } location/2.html {rewrite/2.html/a.html;  } location/3.html {rewrite/3.html/b.html; }} When the request is/1.html and eventually accesses/a.html inside the location{}, the subsequent instruction in this location{} is no longer executed, and the rewritten URL starts again from the beginning, matching the rules from beginning to end.
    • When the rewrite rule is outside of location{}, the break and last function, the subsequent Rewrite/return statements are no longer executed after a break or a later encounter. However, the subsequent location{} will also be executed in close to the location{} statement, provided that the request must match the location.
    • When the rewrite rule is in location{}, all Rewrite/return rules for this location{} and other location{} are no longer executed after the break is encountered.
    • When the rewrite rule is in location{}, the subsequent Rewrite/return rule in this location{} is not executed, but the rewritten URL executes all the rules from the beginning to the next, which match executes which.
Nginx's rewrite configuration--return

This directive is typically used to return a response status code directly to the requesting client. All Nginx configurations that follow the return within the scope are not valid. Can be used in the server, location, and if configurations. In addition to supporting the status code, you can also link with a string or URL.

Return directly returns the status code
Example 1:server{    listen;    server_name www.111.com;    return 403;    Rewrite/(. *)/abc/$1;  The row configuration will not be executed. } example 2:server {... $request _uri ~ "\.htpasswd|\.bak") {    return 404;    Rewrite/(. *)/aaa.txt;  The row configuration will not be executed. }//If there are other configurations below, it will be executed. .....}
return string
Example 3:server{    listen;    server_name www.aming.com;    return "Hello";} Note: If you want to return a string, you must add a status code, or you will get an error. You can also support JSON data samples 4:location ^~/aming {    default_type Application/json;    Return "  {" "Name": "Chyuanliu", "id": "100"} ';} also supports writing a variable example 5:location/test {    return "$host $request _uri";}
Return URL
Example 6:server{    listen;    server_name www.111.com;    return http://www.111.com/123.html;    Rewrite/(. *)/abc/$1;  The row configuration will not be executed. Note that the URL after return must start with http://or https://.
Generate Scene Combat
Background: The site has been hacked, all Baidu Click to this site's request, all jump to a gambling site. Via Nginx Solution: if ($http _referer ~ ' baidu.com ') {    return "
Rewrite rules
Format: Rewrite  regex replacement [flag] * Rewrite configuration can take effect within server, location, and if configuration segments * Regex is a regular expression used to match URIs, which do not match to $ Host (domain name) * Replacement is the URI of the target jump, you can start with http://or https://, you can omit $host, write the $request_uri section directly (that is, the requested link) * Flag, Used to set the processing behavior of rewrite on URIs with break, last, Rediect, permanent, where break and last have been described earlier, Rediect and permanent differ in that the former is temporary redirect (302) , while the latter is permanent redirection (301), the effect is consistent for users to access through the browser. However, for the search engine spider Crawler, there is a difference, the use of 301 more conducive to SEO. Therefore, it is recommended to use the permanent with flags starting with http://or https://. Replacemnet.

Note: The regex matches the URI for the https://www.cnblogs.com/after the domain namechyuanliu/p/9327529.html

  

example, typically used for domain name redirection

Location/{    rewrite/(. *) http://www.aming.com/$1 Permanent;} Description:. * is a regular expression, enclosed in (), it can be called in a later URI, the first occurrence () is called with the second occurrence (), and so on. Location/{    rewrite/.* Http://www.aming.com$request_uri permanent;} Description: In replacement, support variable, here $request_uri is the client request link//request_uri is except the rest of the domain name

example, error analysis

server{    Listen;    server_name www.123.com;    root/tmp/123.com;    Index index.html;    Rewrite/(. *)/abc/$1 redirect;} Note: In this example, the rewrite rule has a problem, will create a continuous loop, will eventually fail, to solve the problem there are two scenarios. On the number of cycles, the test found that curl will cycle 50 times, Chrome will cycle 80 times, IE will cycle 120 times, Firefox will cycle 20 times. Programme 1:server{    Listen;    server_name www.123.com;    root/tmp/123.com;    Index index.html;    Rewrite/(. *)/abc/$1 break;} Description: Using break in rewrite avoids loops. Programme 2:server{    Listen;    server_name www.123.com;    root/tmp/123.com;    Index index.html;    if ($request _uri!~ ' ^/abc/')    {        rewrite/(. *)/abc/$1 redirect;    }} Note: Adding a condition limit can also avoid generating loops

  

Linux10.9 rewrite configuration

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.