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

Smooth upgrade adds echo module, configures location and rewrite

Smooth upgrade adds echo module

View nginx version number and compilation information

[root@localhost ~]# nginx -v
nginx version: nginx/1.22.1
[root@localhost ~]#
[root@localhost ~]# nginx -V
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 ~]#

Download the new version of nginx

[root@localhost ~]# cd /usr/src/
[root@localhost src]#
[root@localhost src]# ls
debug kernels nginx-1.22.1 nginx-1.22.1.tar.gz
[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.22.1.tar.gz 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
...
Resolving deltas: 100% (1645/1645), done.
[root@localhost src]#
[root@localhost src]# ls
debug echo-nginx-module kernels nginx-1.22.1 nginx-1.22.1.tar.gz nginx-1.24.0.tar.gz
[root@localhost src]#

Back up old version nginx

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

Unzip the new version of nginx package, compile nginx again and add –add-module

Note: The new version only compiles but does not install, do not execute make install

[root@localhost src]# tar xf nginx-1.24.0.tar.gz
[root@localhost src]#
[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

[root@localhost nginx-1.24.0]# make
[root@localhost nginx-1.24.0]#

Manually replace with new version and restart

[root@localhost nginx-1.24.0]# cd objs/
[root@localhost objs]# ls
addon autoconf.err Makefile nginx nginx.8 ngx_auto_config.h ngx_auto_headers.h ngx_modules.c ngx_modules.o src
[root@localhost objs]#
[root@localhost objs]# systemctl stop nginx;\cp nginx /usr/local/nginx/sbin/nginx;nginx
[root@localhost objs]#
[root@localhost objs]# ss -anlt
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 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]#

Verify whether the echo module is added successfully

[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
...
        location / {<!-- -->
            echo "hello world";
            roothtml;
            index index.html index.htm;
        }
...
[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 ~]#

location configuration

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

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 the client 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 ~]# vi /usr/local/nginx/conf/nginx.conf
...
        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 ~]#
[root@localhost ~]# nginx -s reload
[root@localhost ~]#

Access using the command line

In the absence of modifiers, as long as your domain name is followed by the word /abc, it will match.

[root@node1 ~]# curl http://192.168.200.10/abc
this is/abc
[root@node1 ~]#
[root@node1 ~]# curl http://192.168.200.10/abc\?a\=10
this is/abc
[root@node1 ~]#
[root@node1 ~]# curl http://192.168.200.10/abc/
this is/abc
[root@node1 ~]#

=: Indicates that the specified pattern must match exactly

[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
...
        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 ~]#
[root@localhost ~]# nginx -s reload
[root@localhost ~]#

Only when the matching content is the same as the content after the equal sign and the parameters are passed, an exact match can be achieved.

[root@node1 ~]# curl http://192.168.200.10/abc
this is =abc
[root@node1 ~]#
[root@node1 ~]# curl http://192.168.200.10/abc\?a\=10
this is =abc
[root@node1 ~]#

The following two cannot be matched

[root@node1 ~]# curl http://192.168.200.10/abc/
this is/abc
[root@node1 ~]#
[root@node1 ~]# curl http://192.168.200.10/abcbb
this is/abc
[root@node1 ~]#

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

[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
...
        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 ~]#
[root@localhost ~]# nginx -s reload
[root@localhost ~]#

It can only be matched if it starts with /abc and cannot have anything at the end.

[root@node1 ~]# curl http://192.168.200.10/abc
this is ~abc
[root@node1 ~]#
[root@node1 ~]# curl http://192.168.200.10/abc\?a\=10
this is ~abc
[root@node1 ~]#

The following three cannot be matched

[root@node1 ~]# curl http://192.168.200.10/abc/
this is/abc
[root@node1 ~]#
[root@node1 ~]# curl http://192.168.200.10/abcbb
this is/abc
[root@node1 ~]#
[root@node1 ~]# curl http://192.168.200.10/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@node1 ~]#

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

[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
...
        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 ~]#
[root@localhost ~]# nginx -s reload
[root@localhost ~]#

It can only be matched case-insensitively if both uppercase and lowercase characters and all uppercase characters are used.

[root@node1 ~]# curl http://192.168.200.10/abC
this is ~*abc
[root@node1 ~]#
[root@node1 ~]# curl http://192.168.200.10/ABC
this is ~*abc
[root@node1 ~]#
[root@node1 ~]# curl http://192.168.200.10/aBc\?a\=10
this is ~*abc
[root@node1 ~]#

The following two cannot be matched

[root@node1 ~]# curl http://192.168.200.10/abc/
this is/abc
[root@node1 ~]#
[root@node1 ~]# curl http://192.168.200.10/abcde
this is/abc
[root@node1 ~]#

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

[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
...
        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 ~]#
[root@localhost ~]# nginx -s reload
[root@localhost ~]#

It can only be matched by ^~ if it starts with abc and is followed by something.

[root@node1 ~]# curl http://192.168.200.10/abc/
this is ^~abc
[root@node1 ~]#
[root@node1 ~]# curl http://192.168.200.10/abc/abab
this is ^~abc
[root@node1 ~]#

The following situations cannot be matched

[root@node1 ~]# curl http://192.168.200.10/abcde
this is/abc
[root@node1 ~]#
[root@node1 ~]# curl http://192.168.200.10/abc
this is ~abc
[root@node1 ~]#
[root@node1 ~]# curl http://192.168.200.10/abc\?a\=10
this is ~abc
[root@node1 ~]#

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).

Syntax: rewrite regex replacement flag;, such as:

rewrite ^/images/(.*\.jpg)$ /imgs/$1 break;

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

Redirect operation

Create a directory and add an html file

[root@localhost ~]# cd /usr/local/nginx/html/
[root@localhost html]# mkdir mhy
[root@localhost html]#
[root@localhost html]#ls
50x.html index.html mhy
[root@localhost html]# cd mhy/
[root@localhost mhy]#
[root@localhost mhy]# echo "hello world" > index.html

Content can be accessed

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

Rename the mhy directory

[root@localhost ~]# cd /usr/local/nginx/html/
[root@localhost html]#ls
50x.html index.html mhy
[root@localhost html]#
[root@localhost html]# mv mhy op
[root@localhost html]#
[root@localhost html]#ls
50x.html index.html op
[root@localhost html]#

Use the original path to access again, but the content cannot be accessed.

[root@localhost html]# curl http://192.168.200.10/mhy/index.html
<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 html]#

Add location for redirection

[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
...
        location /mhy {<!-- -->
            rewrite ^/mhy/(.*)$ /op/$1 break;
        }
...
[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 ~]#
[root@localhost ~]# nginx -s reload
[root@localhost ~]#

Use the original path again to access the content.

[root@localhost html]# curl http://192.168.200.10/mhy/index.html
hello world
[root@localhost html]#
[root@localhost html]#ls
50x.html index.html op
[root@localhost html]#

Redirect can be a path or a URL

[root@localhost html]# vi /usr/local/nginx/conf/nginx.conf
...
        location /mhy {<!-- -->
            rewrite ^/mhy/(.*)$ http://www.baidu.com/index.html break;
        }
...
[root@localhost html]# 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 html]#
[root@localhost html]# nginx -s reload
[root@localhost html]#

Redirect to visit Baidu

image-20231020005219904