nginx smoothly upgrades to add echo module, localtion configuration, and rewrite configuration

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:

< /table>

2.nginx smooth upgrade principle:

Nginx smooth upgrade refers to upgrading the old version of the Nginx server to the new version without interrupting the service. Its principle is mainly based on the following steps:

  1. Copy the new version of the Nginx executable file: First, copy the new version of the Nginx executable file to the specified directory on the server. This directory can be the installation directory of an older version of Nginx or another suitable location.
  2. Restart the Nginx process of the old version: Use the Nginx startup script or command of the old version to restart the Nginx process. Doing so will bring the new Nginx executable into effect, but will not immediately interrupt the running connection.
  3. Coexistence of old and new versions: The Nginx process of the old version will continue to receive and handle connections from clients, while the Nginx process of the new version is started on the same port and starts listening for new connections.
  4. Gradually switch connections to the new version: The new version of the Nginx process will gradually take over the connection from the old version. This can be achieved by gradually reducing the number of worker processes for the old version of Nginx, and correspondingly increasing the number of worker processes for the new version of Nginx. When the old version’s connections wind down, the new version’s Nginx process will completely take over all connections.

Through this smooth upgrade method, service interruption time can be minimized and users can complete the upgrade process without being aware of it. However, in actual operation, it is still necessary to proceed with caution to ensure the stability and reliability of the upgrade process.

3. Smoothly upgrade nginx and add echo module

3.1. View the current nginx version and old version compilation parameter information

//First check our current nginx version, use the -v option, and check that it is nginx-1.22.1
[root@nginx ~]# nginx -v
nginx version: nginx/1.22.1

//Use the -V option to view parameter information when nginx is compiled
[root@nginx ~]# 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

//Back up old version
[root@nginx ~]# cp /usr/local/nginx/sbin/nginx /opt/nginx-1.22.1

3.2. Download nginx-1.24.0 source code package and echo module

nginx official website nginx: download

//Download the source code package of nginx-1.24.0
[root@nginx ~]# wget https://nginx.org/download/nginx-1.24.0.tar.gz
--2023-10-19 16:33:35-- https://nginx.org/download/nginx-1.24.0.tar.gz
Resolving nginx.org (nginx.org)... 52.58.199.22, 3.125.197.172, 2a05:d014:edb:5704::6, ...
Connecting to nginx.org (nginx.org)|52.58.199.22|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1112471 (1.1M) [application/octet-stream]
Saving to: nginx-1.24.0.tar.gz.1’

nginx-1.24.0.tar.gz.1 100%[==================================== ======>] 1.06M 648KB/s in 1.7s

2023-10-19 16:33:39 (648 KB/s) - nginx-1.24.0.tar.gz.1’ saved [1112471/1112471]

[root@nginx ~]# ls
anaconda-ks.cfg nginx-1.22.1.tar.gz nginx-1.24.0.tar.gz

The github website pulls the echo module Repository search results · GitHub

//Install the software package for the required commands
[root@nginx ~]# yum -y install git
[root@nginx ~]# which git
/usr/bin/git

//Pull into the virtual machine through git command
[root@nginx ~]# 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 | 944.00 KiB/s, done.
Resolving deltas: 100% (1645/1645), done.
[root@nginx ~]# ls
anaconda-ks.cfg echo-nginx-module nginx-1.22.1.tar.gz nginx-1.24.0.tar.gz

3.3. Compile and install nginx-1.24.0

//Unzip nginx-1.24.0
[root@nginx ~]# tar xf nginx-1.24.0.tar.gz
[root@nginx ~]# ls
anaconda-ks.cfg echo-nginx-module nginx-1.22.1.tar.gz nginx-1.24.0 nginx-1.24.0.tar.gz

//Compile and install nginx-1.24.0, and add an echo module
[root@nginx ~]# cd nginx-1.24.0/
[root@nginx nginx-1.24.0]# ./configure --help | grep add-module
  --add-module=PATH enable external module
[root@nginx 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
Omit . . .
  nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/local/nginx/sbin/nginx"
  nginx modules path: "/usr/local/nginx/modules"
  nginx configuration prefix: "/usr/local/nginx/conf"
  nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
  nginx pid file: "/usr/local/nginx/logs/nginx.pid"
  nginx error log file: "/var/log/nginx/error.log"
  nginx http access log file: "/var/log/nginx/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"

//Compile new versions or new features (make install cannot be used)
[root@nginx nginx-1.24.0]# make //Use make directly to compile, you cannot use make install

//After compilation is completed, our main program file is nginx in the objs directory in the installation directory. We can also see the difference in file size between /usr/local/nginx/sbin/nginx and the nginx file in the objs directory in the current directory.
[root@nginx nginx-1.24.0]# ls
auto CHANGES CHANGES.ru conf configure contrib html LICENSE Makefile man objs README src
[root@nginx nginx-1.24.0]# ls objs/
addon Makefile nginx.8 ngx_auto_headers.h ngx_modules.o
autoconf.err nginx ngx_auto_config.h ngx_modules.c src
[root@nginx nginx-1.24.0]# ll /usr/local/nginx/sbin/nginx
-rwxr-xr-x 1 root root 6192536 Oct 16 15:54 /usr/local/nginx/sbin/nginx
[root@nginx nginx-1.24.0]# ll objs/nginx
-rwxr-xr-x 1 root root 6740696 Oct 19 16:46 objs/nginx

//We need to replace objs/nginx with /usr/local/nginx/sbin/nginx. At this time, we can only use one command to stop the nginx started by the original version, and then replace it and use the new version immediately. nginx starts service
[root@nginx nginx-1.24.0]# ss -antl //The nginx service is on at this time
State Recv-Q Send-Q Local Address:Port Peer Address:Port
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@nginx nginx-1.24.0]# cd objs/
[root@nginx 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@nginx objs]# /usr/local/nginx/sbin/nginx -s stop;\cp nginx /usr/local/nginx/sbin/nginx;nginx //A semicolon is a command, \cp is In order to overwrite without asking whether to overwrite, but directly overwrite
[root@nginx objs]# ss -antl //The port exists normally
State Recv-Q Send-Q Local Address:Port Peer Address:Port
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@nginx objs]# nginx -v //Check the version again and successfully upgraded to nginx-1.24.0 version
nginx version: nginx/1.24.0

Smooth upgrade completed

3.4.Use of echo module

The echo module in Nginx is a third-party module used to customize response content. Its main use is to generate custom responses in the Nginx server rather than getting content from the backend server. The echo module is usually used in the following situations:

  1. Testing and debugging: During development and debugging, the echo module allows administrators to easily generate custom HTTP responses to verify that the Nginx configuration is working as expected.
  2. Custom error page: You can use the echo module to create a custom error page to replace the Nginx default error page. This way, you can provide more engaging or brand-consistent error messages to users.
  3. Redirects and Rewrites: The echo module allows you to create custom redirection rules or URL rewriting rules to meet specific needs. This can be used for URL mapping or routing rules.
  4. Dynamic content generation: Although not the most efficient way, you can use the echo module to generate some dynamic content, such as the current timestamp or other simple information. This is generally not as efficient as generating content through a back-end application.
  5. HTTP header information manipulation: The echo module also allows you to manipulate HTTP response header information. You can add, remove, or modify HTTP headers to meet specific needs.

Here is a simple example showing how to use the echo module to generate a custom HTTP response:

location /custom {<!-- -->
    echo "This is a custom response";
    echo "Generated by the echo module";
}

It should be noted that the echo module is a third-party module, and Nginx may need to be manually compiled to include this module. Additionally, it is generally not recommended for generating large amounts of dynamic content, as Nginx’s performance is better at handling static content and proxying requests. For large-scale dynamic content generation, specialized web application servers such as Node.js, Django, or Ruby on Rails are often used.

4.location configuration

location section, by specifying the 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 {<!-- -->......}

Explanation of common modifiers:

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

  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 )

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:

  1. Match request URI: rewrite first uses a regular expression to match the request URI. This regular expression usually appears within a location block and will attempt to match the request URI.
  2. 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.
  3. 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.
  4. Choose redirect type: rewrite also typically supports flags such as permanent and redirect for specifying redirects type. For example, permanent represents a permanent redirect (HTTP 301), while redirect represents a temporary redirect (HTTP 302).
  5. Continue processing: Once the URI is rewritten, Nginx will continue to process the rewritten URI. This may involve further location block matching, processing of other rewrite 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