Introduction to Nginx regular expression related parameters and rules

Introduction to Nginx regular expression related parameters and rules

Preface

Recently, I have been helping clients configure servers and frequently modifying Nginx configuration files. I have frequently used regular matching rules. Here are some commonly used regular parameters and rules for your reference.

The syntax rules of Location in Nginx configuration location [ = | ~ | ~* | ^~ | !~ | !~* ] /uri/{ … }

  1. = means exact match
  2. ~ indicates case-sensitive regular matching
  3. ~* indicates case-insensitive regular expression matching
  4. ^~ indicates that the URI starts with a regular string
  5. !~ indicates case-sensitive regular expression mismatch
  6. !~* means case-insensitive regular expression does not match
  7. / Universal match, any request will be matched

Matching order

When multiple locations are configured, the matching order is:

First match =
Secondly, match ^~
The second is the regular matching in the order of the file, and finally it is handed over to the / general matching. When a match is successful, the matching stops and the request is processed according to the current matching rules.

. matches any character except newline
\w matches letters, numbers, underscores, or Chinese characters
\s matches any whitespace character
\d matches a digit
\b matches the beginning or end of a word
^ matches the beginning of the string
$ matches the end of the string

* Repeats zero or more times
+ Repeat one or more times
? Repeat zero or one times
{n} repeat n times
{n,} Repeat n times or more
{n,m} repeat n to m times
*? Repeat as many times as you want, but as few times as possible
+? Repeat 1 or more times, but as few times as possible
?? Repeat 0 or 1 times, but as few times as possible
{n,m}? Repeat n to m times, but as few times as possible
{n,}? Repeat more than n times, but as few times as possible

\W matches any character that is not a letter, number, underscore, or Chinese character
\S matches any character that is not a whitespace character
\D matches any non-digit character
\B matches a position that is not the beginning or end of a word
[^x] matches any character except x
[^aeiou] matches any character except aeiou

Common rules

Exact Match

location = / {
 proxy_pass http://127.0.0.1:9090/
}

Forward all requests directly to port 9090 of the server.

Serving static files

#Directory matching location ^~ /static/ {
 root /webroot/static/;
}
#Suffix matching location ~* \.(gif|jpg|jpeg|png|css|js|ico)$ {
 root /webroot/res/;
}

Forward dynamic requests to the backend application server

#Forward requests starting with /account/ to the Account server location /account/ {
 proxy_pass http://127.0.0.1:8080/
}
#Forward requests starting with /order/ to the Order server location /order/ {
 proxy_pass http://127.0.0.1:9090/
}

rewrite directive

  • last is equivalent to the [L] mark in Apache, which means rewrite.
  • break After this rule is matched, the matching is terminated and the following rules will no longer be matched.
  • redirect returns a 302 temporary redirect, and the browser address will display the URL after the jump.
  • permanent returns a 301 permanent redirect, and the browser address will display the URL after the jump.
  • Use last and break to rewrite the URI, and the browser address bar remains unchanged.
  • When using the alias directive, you must use the last marker; when using the proxy_pass directive, you need to use the break marker.
  • The last tag will re-initiate a request to the server{……} tag after the rewrite rule is executed, while the break tag will terminate the matching after the rule is matched.

Summarize

This is the end of this article about Nginx regular expression related parameters and rules. For more relevant Nginx regular parameters and rules, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Nginx if statement plus regular expression to achieve string truncation
  • Detailed explanation of Nginx regular expressions
  • Detailed explanation of nginx location configuration regular expression example
  • Nginx pseudo-static Rewrite regular resource summary
  • Nginx rewrite regular matching rewriting method example
  • nginx configuration location summary location regular writing and rewrite rule writing
  • Python regular analysis of nginx access log
  • How to use regular expressions to automatically match wildcard domain names in nginx
  • How to use nginx to intercept specified URL requests through regular expressions

<<:  Summary of various methods for JavaScript to determine whether it is an array

>>:  Detailed explanation of MySQL 8.0.18 commands

Recommend

How to filter out duplicate data when inserting large amounts of data into MySQL

Table of contents 1. Discover the problem 2. Dele...

Vue implements infinite loading waterfall flow

This article example shares the specific code of ...

MySQL database table and database partitioning strategy

First, let's talk about why we need to divide...

Example of using swiper plugin to implement carousel in Vue

Table of contents vue - Use swiper plugin to impl...

Docker link realizes container interconnection

Table of contents 1.1. Network access between con...

MySQL sorting Chinese details and examples

Detailed explanation of MySQL sorting Chinese cha...

Detailed tutorial on how to install mysql8.0 using Linux yum command

1. Do a good job of cleaning before installation ...

html opens a new window with a hyperlink and can control window properties

1. The window size opened by the HTML hyperlink C...

XHTML introductory tutorial: text formatting and special characters

<br />This section introduces how to impleme...

How to implement a single file component in JS

Table of contents Overview Single file components...

How to set up Windows Server 2019 (with pictures and text)

1. Windows Server 2019 Installation Install Windo...

How to hide the border/separation line between cells in a table

Only show the top border <table frame=above>...

Practical method of upgrading PHP to 5.6 in Linux

1: Check the PHP version after entering the termi...

MySQL 5.7.27 installation and configuration method graphic tutorial

MySQL 5.7.27 detailed download, installation and ...