Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Nginx URL rewrite (rewrite) configuration and detailed information explanation

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Nginx URL rewrite (rewrite) configuration and detailed information explanation

URL rewriting is beneficial to the determination of the preferred domain of the website, and the redirection of multiple paths to the same resource page is helpful to the centralization of URL weights.

Introduction to Nginx URL rewriting (rewrite)

Like web service software such as apache, the group function of rewrite is to redirect RUL addresses. The rewrite function of Nginx needs the support of PCRE software, that is, rule matching is carried out through perl compatible regular expression statements. Compiling nginx with default parameters will support rewrite's module, but it must also be supported by PCRE

Rewrite is the key instruction to implement URL rewriting. According to the regex (regular expression) part of the content, it is redirected to replacement, ending with a flag tag.

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

Rewrite [flag]

Keyword regular replacement content flag tag

Keyword: where the keyword error_log cannot be changed

Regular: perl compatible regular expression statements for rule matching

Alternative content: replace regular matching content with replacement

Flag tags: flag tags supported by rewrite

The last tag says:

Last # after the matching of this rule is complete, continue to match the new location URI rule down

Break # this rule is terminated upon completion of matching, and no longer matches any subsequent rules

Redirect # returns 302 temporary redirection, and the browser address will display the URL address after the jump.

Permanent # returns 301 permanent redirection, and the browser address bar will display the jumped URL address.

The label segment location of the rewrite parameter:

Server,location,if

Example:

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

Description:

Rewrite is a fixed keyword, indicating the start of rewrite matching rules.

The regex part is ^ / (. *), which is a regular expression that matches the complete domain name and the subsequent path address.

The replacement section is http://www.czlun.com/$1 $1, which is taken from the regex section (). The URL to jump to after a successful match.

Flag partial permanent indicates a permanent 301redirect flag, that is, to jump to a new http://www.czlun.com/$1 address

Description of regular expressions commonly used in regex

Character

Description

\

Marks the following character as a special character or a literal character or a backward reference. For example, "\ n" matches a newline character, while "\ $" matches "$"

^

Matches the starting position of the input string

$

Matches the end position of the input string

*

Matches the preceding character zero or more times. For example, "ol*" can match "o" and "ol", "oll"

+

Matches the preceding character one or more times. For example, "ol+" can match "ol" and "oll" and "oll", but not "o"

?

Match the preceding character zero or once, such as "do (es)?" Can match "do" or "does", "?" Equivalent to "{0jue 1}"

.

Match any single character except "\ n". To match any character including "\ n", use a pattern such as "[.\ n]".

(pattern)

Match the pattern in parentheses and get the corresponding match later. The $0.match 9 attribute is commonly used to get the matching content in parentheses. To match the parenthesis character, you need\ (Content\)

Rewrite enterprise application scenario

The rewrite function of Nginx is widely used in enterprises:

U can adjust the URL browsed by users to look more standardized and meet the needs of developers and product personnel.

In order to make the search engine search website content and user experience better, enterprises will provide services with dynamic URL addresses disguised as static addresses.

After the u URL is replaced with a new domain name, the old access is redirected to the new domain name. For example, visiting JD.com 's 360buy.com will jump to jd.com.

U adjust URL according to special variables, directories, client information, etc.

Introduction to the process of Nginx configuring rewrite (1) creating rewrite statements

Vi conf/vhost/www.abc.com.conf

# vi Editing 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

Restart after confirming that it is correct, as follows:

Nginx-t

# results show that ok and success can be restarted if there is no problem

Nginx-s reload

(3) check the jump effect

Open a browser to access abc.com

When the page opens, the abc.com of the URL address bar becomes www.abc.com, indicating that the URL was rewritten successfully.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report