nginx smoothly upgrades to add echo module, location configuration, and rewrite configuration
Article directory
- nginx smoothly upgrades to add echo module, location configuration, and rewrite configuration
-
- 1.Environmental description:
- 2.nginx smooth upgrade principle:
- 3. Smoothly upgrade nginx and add echo module
-
- 3.1. View the current nginx version and old version compilation parameter information
- 3.2. Download nginx-1.24.0 source code package and echo module
- 3.3. Compile and install nginx-1.24.0
- 3.4.Use of echo module
- 4.location configuration
- 5.rewrite (also called URL redirection)
-
- 5.1. Working principle:
- 5.2.Configure rewrite
1. Environment description:
Host name | IP address | Required services | System version |
---|---|---|---|
nginx | 192.168.195.133 | nginx-1.22.1 (not the latest version) | centos 8 |
zabbix | 192.168.195.130 | None (acts as a client to access the nginx host) | centos 8 |
Modifier | Function |
---|---|
= | Exact match |
~ | Regular expression pattern matching, case sensitive |
~* | Regular expression pattern matching, case-insensitive |
^~ | Prefix matching, similar to the behavior without modifiers, also starts with the specified module. The difference is that if the pattern matches, then it stops searching for other patterns and does not support regular expressions |
@ | Define named location sections. These sections cannot be accessed by clients and can only Accessed by internally generated requests, such as try_files or error_page, etc. |
No modifier means it must start with the specified mode, such as:
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf [root@nginx ~]# cat /usr/local/nginx/conf/nginx.conf . . . . . . location /abc {<!-- --> echo "this is /abc"; roothtml; } . . . . . . [root@nginx ~]# nginx -s reload //The following methods can all be accessed, ?The latter is used to pass parameters, which is equivalent to the login user entering the account and password, which will be passed to the server in the form of variables. [root@zabbix ~]# curl http://192.168.195.137/abc this is/abc [root@zabbix ~]# curl http://192.168.195.137/abc?a=10\ & amp;b=20 this is/abc [root@zabbix ~]# curl http://192.168.195.137/abc/ this is/abc
=: Indicates that it must match the specified pattern exactly, such as:
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf [root@nginx ~]# cat /usr/local/nginx/conf/nginx.conf . . . . . . location = /abc {<!-- --> echo "this is =abc"; roothtml; } . . . . . . [root@nginx ~]# nginx -s reload //The following methods can all be accessed [root@zabbix ~]# curl http://192.168.195.137/abc this is =abc [root@zabbix ~]# curl http://192.168.195.137/abc?a=10\ & amp;b=20 this is =abc
~: Indicates that the specified regular expression must be case-sensitive, such as:
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf [root@nginx ~]# cat /usr/local/nginx/conf/nginx.conf . . . . . . location ~ ^/abc$ {<!-- --> echo "this is ~abc"; roothtml; } . . . . . . [root@nginx ~]# nginx -s reload //The following methods can all be accessed [root@zabbix ~]# curl http://192.168.195.137/abc this is ~abc [root@zabbix ~]# curl http://192.168.195.137/abc?a=10\ & amp;b=20 this is ~abc
~*: Indicates that the specified regular expression is not case-sensitive, such as:
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf [root@nginx ~]# cat /usr/local/nginx/conf/nginx.conf . . . . . . location ~* ^/abc$ {<!-- --> echo "this is ~*abc"; roothtml; } . . . . . . [root@nginx ~]# nginx -s reload //The following methods can all be accessed [root@zabbix ~]# curl http://192.168.195.137/abc this is ~*abc [root@zabbix ~]# curl http://192.168.195.137/ABC this is ~*abc [root@zabbix ~]# curl http://192.168.195.137/abc?a=10\ & amp;b=20 this is ~*abc [root@zabbix ~]# curl http://192.168.195.137/ABC?a=10\ & amp;b=20 this is ~*abc
^~: Indicates the path of the specified regular expression. Anything starting with it will be matched, such as:
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf [root@nginx ~]# cat /usr/local/nginx/conf/nginx.conf . . . . . . location ^~ /abc/ {<!-- --> echo "this is ^~abc"; roothtml; } . . . . . . [root@nginx ~]# nginx -s reload //The following methods can all be accessed [root@zabbix ~]# curl http://192.168.195.137/abc/ this is ^~abc [root@zabbix ~]# curl http://192.168.195.137/abc/fdf this is ^~abc
~: Similar to the behavior without modifiers, it also starts with the specified pattern. The difference is that if the pattern matches, it stops searching for other patterns
Search order and priority: from high to low
- Exact matches with
=
take precedence - Regular expressions are used in the order they are defined in the configuration file
- With
^~
modifier, match at the beginning - With the
~
or~*
modifier, if the regular expression matches the URI - Exact match without modifiers
The order of priority is as follows:
( location = path) --> ( location ^~ path ) --> ( location ~ regular ) --> ( location ~* regular ) --> ( location path )
5.rewrite (also called URL redirection)
Syntax: rewrite regex replacement flag;
, such as:
rewrite ^/images/(.*\.jpg)$ /imgs/$1 break;
$1 here is used to reference the content matched by (.*.jpg), another example:
rewrite ^/bbs/(.*)$ http://www.idfsoft.com/index.html redirect;
As shown in the above example, replacement can be a path or a URL
Common flags
flag | Function |
---|---|
last | Basically use this flag to indicate that the current match is over and continue to the next match, with a maximum of 10 matches. Once this rewrite rule is rewritten, it will no longer be processed by other subsequent rewrite rules. Instead, the UserAgent will re-initiate a request for the rewritten URL and start a similar process from the beginning |
break | Abort Rewrite and no longer continue to match. Once this rewrite rule is rewritten, the UserAgent Re-initiate the request for the new URL and will no longer be checked by any rewrite rules in the current location |
redirect | Return new URL with HTTP status 302 of temporary redirect |
permanent | Return new URL with HTTP status 301 of permanent redirect |
The rewrite module is used to perform URL redirection. This mechanism is beneficial to removing malicious access URLs and is also beneficial to search engine optimization (SEO).
The syntax used by nginx is derived from the Perl Compatible Regular Expression (PCRE) library. The basic syntax is as follows:
Identifier | Meaning |
---|---|
^ | Must start with the entity after ^ |
$ | Must end with the entity before $ |
. | Match any character |
[] | Match any character in the specified character set |
[^] | Match any string not included in the specified character set |
| | Match | Entities before or after |
() | Group to form a group with For matching entities, there is usually | to assist |
Capturing subexpressions can capture any text placed between (), such as:
^(hello|sir)$ //The string captured by "hi sir": $1=hi$2=sir //These captured data can be used as variables later.
5.1. Working principle:
- Match request URI:
rewrite
first uses a regular expression to match the request URI. This regular expression usually appears within alocation
block and will attempt to match the request URI. - Capturing group: If the regular expression contains a capturing group, then after successful matching, the capturing group can be used for subsequent processing. For example, you can use parentheses in a regular expression to create capturing groups, and then use
$1
,$2
, and so on to reference these capturing groups in the rewritten string. - Replace URI: If the regular expression matches successfully,
rewrite
will modify the requested URI using the specified replacement string. Replacement strings typically contain substitution variables such as$1
,$2
, etc., which refer to capturing groups in the regular expression. - Choose redirect type:
rewrite
also typically supports flags such aspermanent
andredirect
for specifying redirects type. For example,permanent
represents a permanent redirect (HTTP 301), whileredirect
represents a temporary redirect (HTTP 302). - Continue processing: Once the URI is rewritten, Nginx will continue to process the rewritten URI. This may involve further
location
block matching, processing of otherrewrite
directives or passing the request to a backend server or static resource.
Here is an example that demonstrates how to use rewrite
to redirect requests:
location /old-path/ { rewrite ^/old-path/(.*)$ /new-path/$1 permanent; }
In this example, if the request URI matches /old-path/
, rewrite
will capture the matching part (for example, xxx
) and then The request is redirected to /new-path/xxx
, using the permanent
flag to indicate permanent redirection.
In short, the working principle of the rewrite
directive is based on regular expression matching and URI replacement, allowing you to redirect and modify incoming HTTP requests.
5.2. Configure rewrite
//Create a new directory file in the html directory to store the jump page [root@nginx html]# cd [root@nginx ~]# cd /usr/local/nginx/html [root@nginx html]#ls 50x.html index.html [root@nginx html]# mkdir images [root@nginx html]#ls 50x.html images index.html [root@nginx html]# cd images/ [root@nginx images]# echo "hello world" > index.html [root@nginx images]# cat index.html hello world [root@nginx images]#
Have access
//Now change the images directory under html to another name (imgs), and then access images again to see if it can be accessed [root@nginx images]# cd .. [root@nginx html]#ls 50x.html images index.html [root@nginx html]# mv images imgs [root@nginx html]#ls 50x.html imgs index.html
Unable to access
However, we cannot prevent users from accessing through the original address just because we have changed the domain name, so we need to use rewrite
[root@nginx conf]# vim nginx.conf [root@nginx conf]# cat nginx.conf . . . . . . location /images {<!-- --> rewrite ^/images/(.*)$ /imgs/$1 break; } . . . . . . [root@nginx conf]# nginx -s reload
Visit again, the visit is successful
If you want the URL to jump to a website on the Internet, the same configuration method
[root@nginx conf]# vim nginx.conf [root@nginx conf]# cat nginx.con . . . . . . location /images {<!-- --> rewrite ^/images/(.*)$ https://www.bing.com/images/search?q=Puppy Pictures & amp;form=IQFRBA & amp;id=52207A2CC7D8B74EFE142A76A4CDE2B26A8594C3 & amp;first=1 & amp; disoverlay=1 break; } . . . . . . [root@nginx conf]# nginx -s reload
Jump successfully
Usage of last and break in flag
First jump to imgs through images, and then request again. The request finds that there is another URL jump, and finally jumps to the landscape page. Break stops the rule check. [root@nginx conf]# vim nginx.conf [root@nginx conf]# cat nginx.conf . . . . . . location /images {<!-- --> rewrite ^/images/(.*)$ /imgs/$1 last; } location /imgs {<!-- --> rewrite ^/imgs/(.*)$ /ftx/$1 last; } location /ftx {<!-- --> rewrite ^/ftx/(.*)$ http://baidu.com break; }
Successfully accessed the landscape page