Principle and application example of Nginx URL rewriting rewrite mechanism

  • 2021-08-28 21:39:03
  • OfStack

URL rewriting is helpful to determine the preferred domain of website, and 301 redirection of multiple paths of the same resource page is helpful to concentrate URL weights

Introduction to Nginx URL Rewrite (rewrite)

Like web service software such as apache, the group function of rewrite is to realize the redirection of RUL address. The rewrite function of Nginx requires the support of PCRE software, that is, the rule matching is carried out through perl compatible regular expression statements. The default parameter compiling nginx will support rewrite modules, but it must also be supported by PCRE

rewrite is the key instruction to implement URL rewriting, which is redirected to replacement according to the contents of regex (regular expression), and ends with flag tag.

The rewrite syntax format and parameter syntax are described as follows:

rewrite < regex > < replacement > [flag];

Keyword regular replacement content flag tag

Keyword: The keyword error_log cannot be changed

Regular: perl is compatible with regular expression statements for rule matching

Alternative content: Replace the regular matching content with replacement

flag tag: flag tag supported by rewrite

flag Mark Description:

last # After matching this rule, continue to match the new location URI rule down break # This rule is terminated when it is matched, and no subsequent rules are matched redirect # Returns 302 Temporary Redirect, Browser Address will display URL Address after Jump permanent # returns 301 permanent redirection, and the browser address bar will display the URL address after jumping

Label segment location for rewrite parameter:

server,location,if

Examples:

rewrite ^/(.*) http://www.czlun.com/$1 permanent;

Description:

rewrite is a fixed keyword, indicating that rewrite matching rules are started The regex part is ^/(.*), which is a regular expression that matches the full domain name and the path address that follows The replacement section is http://www.czlun.com/$1 $1, which is taken from the regex section (). URL to jump to after successful matching. The flag section permanent represents the permanent 301 redirect flag, that is, jumping to the new http://www.czlun. com/$1 address

Explanation of Common Regular Expressions in regex

字符

描述

\

将后面接着的字符标记为1个特殊字符或1个原义字符或1个向后引用。如“\n”匹配1个换行符,而“\$”则匹配“$”

^

匹配输入字符串的起始位置

$

匹配输入字符串的结束位置

*

匹配前面的字符零次或多次。如“ol*”能匹配“o”及“ol”、“oll”

+

匹配前面的字符1次或多次。如“ol+”能匹配“ol”及“oll”、“oll”,但不能匹配“o”

?

匹配前面的字符零次或1次,例如“do(es)?”能匹配“do”或者“does”,"?"等效于"{0,1}"

.

匹配除“\n”之外的任何单个字符,若要匹配包括“\n”在内的任意字符,请使用诸如“[.\n]”之类的模式。

(pattern)

匹配括号内pattern并可以在后面获取对应的匹配,常用$0...$9属性获取小括号中的匹配内容,要匹配圆括号字符需要\(Content\)

rewrite Enterprise Application Scenarios

The rewrite feature of Nginx is widely used in enterprises:

u can adjust the URL browsed by users, which looks more standardized and meets the needs of developers and product personnel. u In order to make search engines search website content and user experience better, enterprises will disguise dynamic URL addresses as static addresses to provide services. After the u URL is replaced with a new domain name, let the old access jump to the new domain name. For example, visiting 360buy. com in JD.com will jump to jd. com u adjusts URL according to special variables, directories and client information

Introduction of Nginx Configuration rewrite Process

(1) Create an rewrite statement

vi conf/vhost/www.abc.com.conf

# vi Edit Virtual Host Configuration File

File content


server {
    listen 80;
    server_name abc.com;
    rewrite ^/(.*) http://www.abc.com/$1 permanent;
}
server {
    listen 80;
    server_name www.abc.com;
    location / {
        root /data/www/www;
        index index.html index.htm;
    }
    error_log  logs/error_www.abc.com.log error;
    access_log  logs/access_www.abc.com.log  main;
}

Or


server {
    listen 80;
    server_name abc.com www.abc.com;
    if ( $host != 'www.abc.com' ) {
        rewrite ^/(.*) http://www.abc.com/$1 permanent;
    }
    location / {
        root /data/www/www;
        index index.html index.htm;
    }
    error_log  logs/error_www.abc.com.log error;
    access_log  logs/access_www.abc.com.log  main;
}

(2) Restart the service

You can restart after confirmation. The operation is as follows:

nginx -t

# Results show that ok and success can be restarted without problems

nginx -s reload

(3) View the jump effect

Open a browser to access abc. com

When the page opens, the abc. com in the URL address bar changes to www. abc. com indicating that URL was successfully rewritten.


Related articles: