Smooth upgrade adds echo module, location configuration, and rewrite configuration

Smooth upgrade adds echo module, location configuration, and rewrite configuration

Smooth upgrade process:

  1. Get compilation information of old versions
  2. Old version backup
  3. Compile a new version or new function (make install cannot be executed)
  4. Manually replace with new version and restart
  5. Verify new version

Environmental preparation

Operating system Old version New version New features
CentOS-8 nginx-1.22.1 nginx-1.24.0 echo-nginx-module

Smoothly upgrade and add echo module

View nginx version number and compilation information

[root@localhost ~]# nginx -v #View version number
nginx version: nginx/1.22.1
[root@localhost ~]# nginx -V #View compilation parameters
nginx version: nginx/1.22.1
built by gcc 8.5.0 20210514 (Red Hat 8.5.0-4) (GCC)
built with OpenSSL 1.1.1k FIPS 25 Mar 2021
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-debug --with-http_ssl_module --with-http_realip_module --with-http_image_filter_module --with-http_gunzip_module -- with-http_gzip_static_module --with-http_stub_status_module --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log
[root@localhost ~]#

Back up old version nginx

[root@localhost ~]# cp /usr/local/nginx/sbin/nginx /opt/nginx-1.22.1
[root@localhost ~]# ls /opt/
nginx-1.22.1
[root@localhost ~]#

Download the new version of nginx

[root@localhost ~]# cd /usr/src/
[root@localhost src]#
[root@localhost src]# ls
debug kernels nginx-1.22.1
[root@localhost src]#
[root@localhost src]# wget http://nginx.org/download/nginx-1.24.0.tar.gz
--2023-10-19 20:44:52-- http://nginx.org/download/nginx-1.24.0.tar.gz
...
[root@localhost src]# ls
debug kernels nginx-1.22.1 nginx-1.24.0.tar.gz
[root@localhost src]#

Install git tools and clone the echo module

[root@localhost src]# yum -y install git
[root@localhost src]# git clone https://github.com/openresty/echo-nginx-module.git
Cloning into 'echo-nginx-module'...
remote: Enumerating objects: 3061, done.
remote: Counting objects: 100% (43/43), done.
remote: Compressing objects: 100% (31/31), done.
remote: Total 3061 (delta 21), reused 30 (delta 12), pack-reused 3018
Receiving objects: 100% (3061/3061), 1.18 MiB | 1.74 MiB/s, done.
Resolving deltas: 100% (1645/1645), done.
[root@localhost src]# ls
debug echo-nginx-module kernels nginx-1.22.1 nginx-1.24.0.tar.gz

Unzip the new version nginx package, compile nginx again, and add the –add-module module based on the old version compilation.

[root@localhost ~]# cd /usr/src/
[root@localhost src]# tar xf nginx-1.24.0.tar.gz
[root@localhost src]# ls
debug echo-nginx-module kernels nginx-1.22.1 nginx-1.24.0 nginx-1.24.0.tar.gz
[root@localhost src]# cd nginx-1.24.0
[root@localhost nginx-1.24.0]#
[root@localhost nginx-1.24.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-debug --with-http_ssl_module --with-http_realip_module - -with-http_image_filter_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_stub_status_module --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/ nginx/error.log --add-module=../echo-nginx-module #equal to the location of the following module package
[root@localhost nginx-1.24.0]# make

To smoothly upgrade, you must first close the old version of nginx service, then replace the old version with the newly compiled nginx main program, and then start the service.

[root@localhost nginx-1.24.0]# cd objs/
[root@localhost objs]# ls
addon Makefile nginx.8 ngx_auto_headers.h ngx_modules.o
autoconf.err nginx ngx_auto_config.h ngx_modules.c src
[root@localhost objs]# systemctl stop nginx;\cp nginx /usr/local/nginx/sbin/nginx;systemctl start nginx
[root@localhost objs]# ss -antl #Port number 80 has been started
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
[root@localhost objs]#

View version number

[root@localhost objs]# nginx -v
nginx version: nginx/1.24.0
[root@localhost objs]#

Test the newly added echo function

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
...
location / {<!-- -->
            echo "hello world"; #Add echo module
            roothtml;
            index index.html index.htm;
        }
...
#nginx supports hot deployment, and there is no need to restart the service after modifying the configuration file.
[root@localhost ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok #The prompt syntax is correct
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost ~]#

location configuration

location section, by specifying a pattern to match the URI requested by the client

//Function: Allows matching of each defined location based on the URI requested by the user. When matched, the request will be processed by the configuration in the corresponding location configuration block, such as access control and other functions.

//Syntax: location [modifier] pattern {......}
Common modifier description:
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, it will stop searching for other patterns. Regular expressions are not supported
@ Define named location sections. These sections cannot be accessed by clients and can only be accessed by internally generated requests, such as try_files or error_page.

Search order and priority: from high to low:

  1. Exact matches with = take precedence
  2. Regular expressions are used in the order they are defined in the configuration file
  3. With ^~ modifier, match at the beginning
  4. With the ~ or ~* modifier, if the regular expression matches the URI
  5. Exact match without modifiers
No modifier means it must start in the specified mode
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
...
        location /abc {<!-- -->
            echo "this is /abc";
        }
...

[root@localhost ~]# nginx -t #Check if there are any syntax errors
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost ~]# nginx -s reload #Reload nginx

Access using the command line

In the absence of modifiers, as long as your domain name is followed by /abc, whatever follows it will be matched.

[root@localhost ~]# curl http://192.168.10.130/abc
this is/abc
[root@localhost ~]# curl http://192.168.10.130/abc\?a\=10
this is/abc
[root@localhost ~]# curl http://192.168.10.130/abc$sayhduig
this is/abc
[root@localhost ~]# curl http://192.168.10.130/abc/
this is/abc
[root@localhost ~]#
=: Indicates that it must match the specified pattern exactly
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
...
        location /abc {<!-- -->
            echo "this is /abc";
        }

        location = /abc {<!-- -->
            echo "this is =abc";
        }
...
[root@localhost ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost ~]# nginx -s reload

Access using the command line

[root@localhost ~]# curl http://192.168.10.130/abcdaf
this is/abc
[root@localhost ~]# curl http://192.168.10.130/abc #Only when the matching content is the same as the content after the equal sign and the parameters are passed, an accurate match can be made
this is =abc
[root@localhost ~]# curl http://192.168.10.130/abc\?a\=10 #? is used to pass parameters \ is the escape character
this is =abc
[root@localhost ~]# curl http://192.168.10.130/abc/ #If there is / after abc, it will not match
this is/abc
[root@localhost ~]# curl http://192.168.10.130/abc/asd #/If there is something behind it, it cannot be matched. It can only be matched by /abc.
this is/abc

~: Indicates that the specified regular expression is case-sensitive

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
...
        location /abc {<!-- -->
            echo "this is /abc";
        }
        
# location = /abc {<!-- -->
# echo "this is =abc";
# }

        location ~ ^/abc$ {<!-- -->
            echo "~abc";
        }
...
[root@localhost ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost ~]# nginx -s reload

It can only be matched if it starts with /abc and cannot have anything at the end, and it is case-sensitive.

[root@localhost ~]# curl http://192.168.10.130/abc
~123
[root@localhost ~]# curl http://192.168.10.130/abc\?a\=10
~123

The following three cannot be matched

[root@localhost ~]# curl http://192.168.10.130/abcl
this is/abc
[root@localhost ~]# curl http://192.168.10.130/abc/
this is/abc
[root@localhost ~]# curl http://192.168.10.130/ABC
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<center>nginx/1.24.0</center>
</body>
</html>
[root@localhost ~]#

~*: Indicates that the specified regular expression is not case-sensitive

#Note that when configuring location, you should put the case-sensitive ones first, because the higher the priority, the higher the priority. If you put the case-insensitive ones first, it will override the case-sensitive access
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
...
        location ~ ^/abc$ {<!-- -->
            echo "~abc"
        }

        location ~* ^/abc$ {<!-- -->
            echo "~*abc"
        }

...

It can only be matched if it starts with /abc and cannot have anything at the end, and it is not case sensitive.

[root@localhost ~]# curl http://192.168.10.130/AbC
~*abc
[root@localhost ~]# curl http://192.168.10.130/abc\?a\=10
~abc
[root@localhost ~]#

The following cannot be matched

[root@localhost ~]# curl http://192.168.10.130/abc/
this is/abc
[root@localhost ~]# curl http://192.168.10.130/abcadas/
this is/abc
[root@localhost ~]#

^~: indicates matching at the beginning, does not support regular expressions

[root@localhost ~]# curl http://192.168.10.130/abc/
^~abc
[root@localhost ~]# curl http://192.168.10.130/abc/asda
^~abc
[root@localhost ~]# curl http://192.168.10.130/abc/ads\?a\=10/ads
^~abc

The following cannot be matched

[root@localhost ~]# curl http://192.168.10.130/abc
~abc
[root@localhost ~]# curl http://192.168.10.130/abcasd
this is/abc
[root@localhost ~]# curl http://192.168.10.130/abc\?a\=10
~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

  1. Exact matches with = take precedence
  2. Regular expressions are used in the order they are defined in the configuration file
  3. With ^~ modifier, match at the beginning
  4. With the ~ or ~* modifier, if the regular expression matches the URI
  5. Exact match without modifiers

The order of priority is as follows:

( location = path) --> ( location ^~ path ) --> ( location ~ regular ) --> ( location ~* regular ) --> ( location path )

rewrite configuration

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 functions of URL redirection, such as:

When we search for: http://www.baidu.com, URL redirection can automatically help us change it to https://www.baidu.com

Or when I access the images directory, it can automatically change me to the imgs directory.

?

Syntax: rewrite regex replacement flag;

? #rewrite starts with regular expression and the content to be replaced is marked.

Here are examples:

# All files ending in .jpg under the images directory can be found in the imgs directory. break ends here
rewrite ^/images/(.*\.jpg)$ /imgs/$1 break;

$1 here is used to reference the content matched by (.*.jpg), another example:

# When accessing all contents in the bbs directory, forward to this domain name for access. redirect
rewrite ^/bbs/(.*)$ http://www.idfsoft.com/index.html redirect;

As shown in the above example, replacement can be a path or a URL

Redirect operation

redirect directory

#Create a directory and add an html file
[root@localhost ~]# cd /usr/local/nginx/html/
[root@localhost html]# mkdir 123
[root@localhost html]#ls
123 50x.html index.html
[root@localhost html]# echo "hello world" > 123/index.html
[root@localhost html]#

access test

[root@localhost ~]# curl http://192.168.10.130/123/index.html
hello world
[root@localhost ~]#

Change directory name

[root@localhost html]# mv 123 666
[root@localhost html]#ls
50x.html 666 index.html

Under normal circumstances, nothing can be accessed in the new directory.

[root@localhost ~]# curl http://192.168.10.130/666/
<html>
<head><title>403 Forbidden</title></head>
<body>
<center><h1>403 Forbidden</h1></center>
<center>nginx/1.24.0</center>
</body>
</html>

Add location for redirection

[root@localhost html]# vim /usr/local/nginx/conf/nginx.conf
...
        location /123 {<!-- -->
            rewrite ^/123/(.*)$ /666/$1 break;
}

[root@localhost html]# nginx -s reload
[root@localhost html]#ls
50x.html 666 index.html
[root@localhost html]#

access test

[root@localhost ~]# curl http://192.168.10.130/123/ #The original path can still be accessed
hello world
[root@localhost ~]#

Redirect URL

#When I access any content in the /666 directory, it will be forwarded to https://www.4399.com/.
[root@localhost html]# vim /usr/local/nginx/conf/nginx.conf
...
        location /666 {<!-- -->
            rewrite ^/666/(.*)$ https://www.4399.com/ break;
}

Web page testing

Common flags

flag Function
last Basically this flag is used, indicating that the current matching is over and continues to the next matching, with a maximum of 10 to 20 matches. Once this rewrite rule is repeated After the writing is completed, it will no longer be processed by other subsequent rewrite rules. Instead, the UserAgent will initiate a request for the rewritten URL again, and perform a similar process from the beginning
break Abort Rewrite and no longer continue to match. Once this rewrite rule is rewritten, UserAgent will re-initiate a request for the new URL, and it will no longer be Any rewrite rules in the current location checked
redirect are returned with HTTP status 302 of temporary redirection New URL
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 of entities for matching, usually with | to assist
Compatible with regular rules 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 of entities for matching, usually with | to assist