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

Even a novice can understand the difference between typeof and instanceof in js

Table of contents 1. typeof 2. instanceof 3. Diff...

A brief analysis of the basic implementation of Vue detection data changes

Table of contents 1. Object change detection 2. Q...

Easyswoole one-click installation script and pagoda installation error

Frequently asked questions When you are new to ea...

7 useful new TypeScript features

Table of contents 1. Optional Chaining 2. Null va...

MySQL 5.6.24 (binary) automatic installation script under Linux

This article shares the mysql5.6.24 automatic ins...

20 excellent foreign web page color matching cases sharing

This article collects 20 excellent web page color ...

Implementation of WeChat applet message push in Nodejs

Select or create a subscription message template ...

How to set Tomcat as an automatically started service? The quickest way

Set Tomcat to automatically start the service: I ...

How to install MySQL database on Debian 9 system

Preface Seeing the title, everyone should be thin...

Detailed tutorial on deploying apollo with docker

1. Introduction I won’t go into details about apo...

Use and understanding of MySQL triggers

Table of contents 1. What is a trigger? 2. Create...

In-depth explanation of closure in JavaScript

Introduction Closure is a very powerful feature i...