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

Rewrite configuration of nginx

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

Domain name jump (redirect), URL rewrite (pseudo-static), static and dynamic separation (jump domain name and connect to CDN for acceleration)

# relying on the PCRE library

# Module: ngx_http_rewrite_module

Rwrite related instructions

# if (condition) {command} coding.net/u/aminglinux/p/nginx/git/blob/master/rewrite/if.md

# break and last coding.net/u/aminglinux/p/nginx/git/blob/master/rewrite/break.md

# return is followed by status code, URL, text (supporting variables) coding.net/u/aminglinux/p/nginx/git/blob/master/rewrite/return.md

# rewrite Rule coding.net/u/aminglinux/p/nginx/git/blob/master/rewrite/rewrite_ruler.md

# rewrite_log defines rewrite log rewrite_log on; writes to error_log notice level

=

If instruction

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

Conditional examples

The conditional judgment statement consists of three parts: 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, there are =,! =, ~,! ~

! It means the opposite, ~ is a matching symbol, it is a regular expression on the right, it is case-sensitive, and ~ is a 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 1

If ($request_method = POST) / / if the requested method is POST, the status code will be returned directly.

{

Return 405; / / the rewrite rule is not used in this example, and the return instruction is supported in if.

}

Example 2

If ($http_user_agent ~ MSIE) / / user_agent request with MSIE character, which directly returns the 403 status code

{

Return 403

}

If you want to restrict multiple user_agent at the same time, you can also write something like this

If ($http_user_agent ~ "MSIE | firefox | spider")

{

Return 403

}

Example 3

If (!-f $request_filename) / / when the requested file does not exist, the following rewrite rule will be executed

{

Rewrite statement

}

Example 4

If ($request_uri ~ 'gid=\ d {9 gid= 12} /') / /\ d indicates a number, and {9 ~ 12} indicates that the number appears 9 to 12 times. For example, gid=123456789/ is eligible.

{

Rewrite statement

}

Break and last in rewrite

The two instructions have the same usage, but have different meanings, and need to be placed at the end of the rewrite rule to control whether the rewritten link continues to be executed by the nginx configuration (mainly rewrite, return instructions).

Example 1 (two consecutive rewrite rules):

Server {

Listen 80

Server_name test.com

Root / tmp/123.com

Rewrite / 1.html / 2.html; rewrite / 2.html / 3.html

}

When we request 1.html, we end up accessing 3.html, and the two rewrite rules are executed one after another.

Break and last are external to location {}

Format: rewrite xxxxx break

Example 2 (add break):

Server {

Listen 80

Server_name test.com

Root / tmp/123.com

Rewrite / 1.html / 2.html break;rewrite / 2.html / 3.html

}

When we request 1.html, we end up accessing 2.html

Note that the role of break in this example is to no longer execute rewrite rules below break.

However, when there is a location in the configuration file, it also performs the configuration of the location {} segment (requested to match the location).

Example 3 (there is a location segment after break):

Server {

Listen 80

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 a 1.html is requested, it will eventually return a 403 status code, indicating that it matches the location {} configuration after the break.

In the above two examples, you can replace break with last, and they have exactly the same effect.

When break and last are in 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.hml;}

}

When you request / 1.html, you will eventually visit / b.html, execute rewrite under location / twice in a row, jump to / 3.html, and then match 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.hml;}

}

When you request / 1.html, you will eventually visit / 2.html

Inside location {}, if you encounter break, all instructions in this location {} and all subsequent location {} will no longer be 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.hml;}

}

When you request / 1.html, you will eventually visit / a.html

Within location {}, if you encounter last, the subsequent instructions in this location {} will no longer be executed, while the rewritten url starts from scratch again, matching the rules from beginning to end.

Conclusion

When the rewrite rule is outside location {}, break and last function the same. When break or last is encountered, the subsequent rewrite/return statements are no longer executed. However, if there is a location {}, the statement in location {} will be executed further, of course, as long as the request must match the location. When rewrite rules are in location {} and break is encountered, all rewrite/return rules of this location {} and other location {} are no longer executed. When the rewrite rule is in location {}, when the last is encountered, the subsequent rewrite/return rule in this location {} is not executed, but the rewritten url executes all the rules from scratch again, which matches which.

Return instruction of nginx

This instruction is generally used to return the response status code directly to the requesting client. All nginx configurations that follow return in this scope are invalid.

Can be used in server, location, and if configurations.

In addition to supporting status codes, you can also link to strings or url.

Return status code directly

Example 1:

Server {

Listen 80

Server_name www.aming.com

Return 403

Rewrite / (. *) / abc/$1; / / the line configuration will not be performed.

}

Example 2:

Server {

.

If ($request_uri ~ ".htpasswd | .bak")

{

Return 404

Rewrite / (. *) / aaa.txt; / / the line configuration will not be performed.

}

/ / if there are other configurations below, they will be executed.

.

}

Returns a string

Example 3:

Server {

Listen 80

Server_name www.aming.com

Return 200 "hello"

}

Note: if you want to return a string, you must add a status code, otherwise an error will be reported.

Json data can also be supported

Example 4:

Location ^ ~ / aming {

Default_type application/json

Return 200'{"name": "aming", "id": "100"}'

}

Writing a variable is also supported.

Example 5:

Location / test {

Return 200 "$host $request_uri"

}

Return to url

Example 6:

Server {

Listen 80

Server_name www.aming.com

Return http://www.aminglinux.com/123.html;

Rewrite / (. *) / abc/$1; / / the line configuration will not be performed.

}

Note: the url after return must start with http:// or https://.

Generate a scene for actual combat

Background: the website has been hacked, and all requests that click on this site on Baidu have been redirected to another website.

Resolve through nginx:

If ($http_referer ~ 'baidu.com')

{

Return 200 "_ window.location.href='//$host$request_uri';"

}

If it is written as:

Return http://$host$request_uri; will prompt "too many redirects" in the browser.

Rewrite rule

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 URI, which does not match to $host (domain name)

Replacement is the URI of the target jump, which can start with http:// or https://, or omit $host and write the $request_uri part directly (that is, the requested link).

Flag, which is used to set the processing behavior of rewrite to URI, including break, last, rediect and permanent, in which break and last have been introduced earlier

The difference between rediect and permanent is that the former is a temporary redirect (302), while the latter is a permanent redirect (301), which has the same effect for users to access through a browser.

However, for search engine spider crawlers, there is a difference, using 301 is more beneficial to SEO. Therefore, it is recommended that flag whose replacemnet begins with http:// or https:// use permanent.

Example 1

Location / {

Rewrite / (.) Http://www.aming.com/$1 permanent

}

Description:. As a regular expression, enclosed in (), it can be called later in URI, with $1 for the first occurrence, $2 for the second occurrence, and so on.

Example 2

Location / {

Rewrite /. * http://www.aming.com$request_uri permanent

}

Description: in replacement, variables are supported. The $request_uri here is the link requested by the client.

Example 3

Server {

Listen 80

Server_name www.123.com

Root / tmp/123.com

Index index.html

Rewrite / (. *) / abc/$1 redirect

}

Explanation: there is a problem with the rewrite rule in this example, which will create a continuous loop and eventually fail. There are two solutions to solve this problem.

With regard to 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.

Example 4

Server {

Listen 80

Server_name www.123.com

Root / tmp/123.com

Index index.html

Rewrite / (. *) / abc/$1 break

}

Note: using break in rewrite avoids loops.

Example 5

Server {

Listen 80

Server_name www.123.com

Root / tmp/123.com

Index index.html

If ($request_uri! ~'^ / abc/')

{

Rewrite / (. *) / abc/$1 redirect

}

}

Note: by adding a conditional restriction, the cycle can also be avoided.

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