Session Persistence Method, System and Storage Medium Based on Nginx and Lua

By using Lua scripts in Nginx to determine the session hold policy and forward the request, the problems of session loss and uneven load in the existing Nginx session hold policy are solved, and stable session hold and load balancing are achieved.

CN119629182BActive Publication Date: 2025-06-20NINETECH INFORMATION TECH (SHENZHEN) CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202510136174.9
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2025-02-07
Publication Date
2025-06-20
Estimated Expiration
2045-02-07

AI Technical Summary

Technical Problem

The commonly used session retention strategies in existing Nginx have problems with session loss risk and load imbalance.

Method used

By combining Nginx and Lua technologies, client request information is obtained, request paths are parsed and matched, Lua scripts are called to determine the session retention policy, and forward the request in the Nginx configuration file to the specified backend server based on this policy.

Benefits of technology

It realizes that the same user request on the client is always routed to the same backend server, ensuring sessions are maintained and load-balanced, flexibly responding to different usage scenarios, and reducing the risk of session loss.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119629182B_ABST
    Figure CN119629182B_ABST
Patent Text Reader

Abstract

The present application relates to a session retention method and system based on Nginx and Lua, which includes obtaining request information initiated by a client; parsing the request information and matching a corresponding request path based on the location block information in the Nginx configuration file; if the request path matches successfully, calling the Lua script within the location block and determining the adopted session retention strategy; and forwarding the request information to a specified backend server based on the upstream block information in the Nginx configuration file and according to the determined session retention strategy, so that the same user requests are routed to the same backend server, thereby achieving session retention and load balancing.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the technical field of software development, and particularly to a session retention method, system, and storage medium based on Nginx and Lua. Background Art

[0002] Session retention technology is crucial in web applications. It ensures that users can continuously access the same backend service during multiple requests, thereby maintaining the state and data consistency of user sessions. This is essential for providing a smooth user experience, enhancing data security, and improving application performance.

[0003] In Nginx, there are mainly three common session retention strategies: ip_hash, url_hash, and sticky_cookie_insert. These strategies enable Nginx to effectively maintain user sessions and improve the overall performance and user experience of the application. However, these strategies also have their own drawbacks. For example, if a user changes their IP address due to network switching, it may lead to session loss. In addition, in a shared IP environment, this method may cause uneven load distribution; different URL parameters may cause sessions to be routed to different servers, which may result in uneven load for applications with a large amount of dynamic content; if a user clears their cookies or the cookies expire, it will lead to session loss. Summary of the Invention

[0004] This application provides a session retention method, system, and storage medium based on Nginx and Lua to solve the problems of session loss risk and load imbalance existing in the common session retention strategies in the prior art Nginx.

[0005] In a first aspect, this application provides a session retention method based on Nginx and Lua, including:

[0006] Obtain the request information initiated by the client;

[0007] Parse the request information and match the corresponding request path based on the location block information in the Nginx configuration file;

[0008] If the request path matches successfully, call the Lua script within the location block and determine the session retention strategy to be adopted;

[0009] Based on the upstream block information in the Nginx configuration file and according to the determined session retention strategy, forward the request information to the specified backend server so that the same user requests are routed to the same backend server.

[0010] Further, invoking the Lua script within the location block and determining the adopted session persistence strategy includes:

[0011] Obtain the request parameters in the request information;

[0012] Determine the parameter type of the request parameters;

[0013] Call the corresponding method according to the parameter type to assign a value to the target ID variable in the Lua script;

[0014] After the assignment is completed, run the Lua script to determine the adopted session persistence strategy.

[0015] Further, the parameter types include IP address, user ID, URL, and message request body BODY. Calling the corresponding method according to the parameter type to assign a value to the target ID variable in the Lua script includes:

[0016] Obtain the IP address in the request information through the ngx.var.remote_addr method and assign the IP address to the target ID variable;

[0017] Obtain the user ID from the request headers in the request information through the ngx.req.get_headers() method and assign the user ID to the target ID variable;

[0018] Obtain the complete URI in the request information through the ngx.var.request_uri method and assign the complete URI to the target ID variable;

[0019] First introduce the JSON library to parse the JSON data in the request body of the request information, then read the request body through the ngx.req.read_body() method, and finally obtain and parse the JSON data through the ngx.req.get_body_data() method, and assign the specified parameter to the target ID variable.

[0020] Further, based on the upstream block information in the Nginx configuration file and according to the determined session persistence strategy, forward the request information to the specified backend server so that the same user requests are routed to the same backend server, including:

[0021] Perform a hash operation based on the assignment of the target ID variable to determine the target backend server to which the request information is forwarded;

[0022] Based on the server group preset in the upstream block of the Nginx configuration file, perform load balancing processing on the backend servers in a software manner;

[0023] Based on the determined target backend server and load balancing processing, forward the request information to the specified backend server through a preset instruction, so that the same user request is routed to the same backend server.

[0024] Further, parsing the request information and matching the corresponding request path based on the location block information in the Nginx configuration file includes:

[0025] According to the URI in the request information, traverse the location blocks in the Nginx configuration file one by one to find the matching rule to determine the request path corresponding to the URI.

[0026] Further, the Lua script defines the association relationship between the cached data and the user information.

[0027] Further, by customizing and adjusting the Lua script, customize the session persistence policy, and implement multiple routing policies for routing the client request information to the backend server. Among them, the multiple routing policies at least include: routing policies based on IP address, user information, URL, Cookie, and Token.

[0028] In a second aspect, the present application provides a session persistence system based on Nginx and Lua, including: a client module, a routing and forwarding module, and a backend server module. Among them, the client module is used to send the user's request message, the routing and forwarding module is used to execute any method described in the first aspect above, and the backend server module is used to receive the client request information routed by the routing and forwarding module.

[0029] Further, the routing and forwarding module includes a custom configuration module, and the custom configuration module is used for users to configure different session routing policies.

[0030] In a third aspect, the present application also provides a computer storage medium storing computer-executable instructions, and the computer-executable instructions are used to execute the session persistence method based on Nginx and Lua of the present application.

[0031] A session retention method and system based on Nginx and Lua provided by an embodiment of the present application obtain request information initiated by a client; parse the request information, and match a corresponding request path based on location block information in the Nginx configuration file; if the request path matches successfully, call the Lua script within the location block and determine the adopted session retention policy; based on upstream block information in the Nginx configuration file, and according to the determined session retention policy, forward the request information to a specified backend server, so that the same user requests are routed to the same backend server. In this way, through the combination of the Nginx configuration file and the Lua script, since the processing logic defined within the location block in the Nginx configuration file is to call and execute the Lua script, and the Lua script can customize various routing policies based on IP addresses, user information, URLs, cookies, tokens, etc., when calling the Lua script in the Nginx configuration file and specifying the session retention policy, the session retention policy can flexibly handle different usage scenarios, and achieve routing and forwarding the same user requests from the client to the same backend server (session retention) and load balancing. Description of the Drawings

[0032] The drawings here are incorporated into the specification and form a part of this specification, showing embodiments consistent with the present invention, and are used together with the specification to explain the principles of the present invention.

[0033] In order to more clearly illustrate the technical solutions in the embodiments of the present invention or in the prior art, the following will briefly introduce the drawings required for use in the description of the embodiments or the prior art. Obviously, for those of ordinary skill in the art, without creative efforts, other drawings can also be obtained based on these drawings.

[0034] One or more embodiments are exemplarily illustrated by the pictures in the corresponding drawings. These exemplary illustrations do not limit the embodiments. Elements with the same reference numerals in the drawings are represented as similar elements, unless otherwise stated, and the drawings in the figures do not constitute a proportional limitation.

[0035] Figure 1 It is a flowchart of a session retention method based on Nginx and Lua provided by an embodiment of the present application;

[0036] Figure 2 It is a refined flowchart of step S103 in an embodiment of the present application;

[0037] Figure 3 It is a module flowchart of a session retention system based on Nginx and Lua provided by an embodiment of the present application. Detailed Embodiments

[0038] To make the objectives, technical solutions, and advantages of the embodiments of this application clearer, the following will clearly and completely describe the technical solutions in the embodiments of this application with reference to the accompanying drawings in the embodiments of this application. Obviously, the described embodiments are some, but not all, of the embodiments of this application. All other embodiments obtained by those of ordinary skill in the art based on the embodiments in this application without creative efforts shall fall within the scope of protection of this application.

[0039] The following disclosure provides many different embodiments or examples for implementing different structures of the present invention. To simplify the disclosure of the present invention, components and settings of specific examples are described below. Of course, they are only examples and are not intended to limit the present invention. In addition, the present invention may repeat reference numerals and / or letters in different examples. Such repetition is for the purpose of simplification and clarity and does not itself indicate the relationship between the various embodiments and / or settings discussed.

[0040] Nginx (engine x) is a high-performance HTTP and reverse proxy web server. In Nginx, there are mainly three common session persistence strategies: ip_hash, url_hash, and sticky_cookie_insert. The specific principles are as follows:

[0041] ip_hash: This mechanism is based on the client's IP address. By performing a hash operation on the IP address, requests are routed to a specific backend service. In this way, requests from the same IP address will always be directed to the same server.

[0042] url_hash: This strategy performs a hash calculation on the URL path of the request to ensure that users accessing the same resource are always routed to the same server, thereby achieving session persistence.

[0043] sticky_cookie_insert: A session persistence mechanism based on Cookies (data stored on the user's local terminal, which can also be understood as cached data). When a user accesses for the first time, Nginx inserts a specific Cookie to track the user's session. In subsequent requests, the user will carry this Cookie, and Nginx routes the request to the corresponding server according to the value of the Cookie.

[0044] These strategies enable Nginx to effectively maintain user sessions and improve the overall performance and user experience of the application. However, these strategies also have their own drawbacks. For example:

[0045] ip_hash: This strategy is simple and easy to use, with convenient configuration, and can quickly achieve session persistence. However, if the user changes the IP address due to network switching, it may lead to session loss. In addition, in a shared IP environment, this method may cause uneven load distribution.

[0046] url_hash: This strategy ensures the consistency of requests for the same resource, is especially suitable for static content or specific API access, and helps to improve cache utilization. However, different URL parameters may cause sessions to be routed to different servers, which may cause uneven load for applications with more dynamic content, thus affecting the overall performance.

[0047] sticky_cookie_insert: This strategy can effectively handle changes in the user's IP or URL, maintain session consistency, is applicable to high-availability and load-balancing environments, and can flexibly handle changing request patterns. However, attention needs to be paid to the security of cookies. If the user clears the cookies or the cookies expire, it will lead to session loss.

[0048] Based on this, in order to solve the problems in the prior art, the present application provides a session persistence method and system based on Nginx and Lua. The Nginx server can efficiently process requests, use Lua scripts for flexible request processing and parameter extraction, while ensuring load balancing and session persistence.

[0049] Figure 1 A session persistence method based on Nginx and Lua provided by an embodiment of the present application, the method includes:

[0050] S101. Obtain the request information initiated by the client;

[0051] In the present application, session persistence means that the same user requests from the client are always routed and forwarded to the same backend server. In one embodiment, the request information initiated by the client refers to the requests initiated by the user through a browser or other clients (such as: mobile applications, API clients). Specifically, these requests usually include the following parts:

[0052] URL (Uniform Resource Locator, which is the unified resource location flag of the WWW and refers to the network address): It contains the location of the requested resource, such as: http: / / example.com / api / data.

[0053] Headers: It contains meta-information, such as the request type (GET, POST, etc.), authentication information (Bearer Token, etc.), content type (Content-Type, etc.).

[0054] Body data: In some requests, especially POST requests, data can be included, usually JSON data or form data.

[0055] In the embodiments of the present application, since the request information initiated by the client needs to be always routed and forwarded to the same backend server, the request information initiated by the client needs to be obtained first during the whole processing process.

[0056] In addition, since the present application is a session retention method combining Nginx and Lua, the deployment and implementation of the Nginx server are involved. Specifically, in the embodiments of the present application, the deployment steps and implementation solutions of the Nginx server include the following aspects:

[0057] 1. Write a Lua script: First, initialize a result_id variable (target ID variable) with an empty initial value. Then, define a Lua script through rewrite_by_Lua to process the client request and assign a value to result_id. Specifically, if the request parameter in the client request is an IP address, obtain the client IP through the ngx.var.remote_addr method and set result_id to the client IP; if the request parameter in the client request is USER, obtain the user ID in the header through the ngx.req.get_headers() method and set result_id to the user ID; if the request parameter in the client request is a URL, obtain the client request URI through the ngx.var.request_uri method and set result_id to the URI; if the request parameter in the client request is a specified parameter in the body, first introduce the JSON library by local json=require("cjson.safe") to parse the JSON data in the request body, then read the request body through the ngx.req.read_body() method, and finally parse the JSON data through ngx.var.result_id=json.decode(ngx.req.get_body_data()).xxx and set the target value xxx to the result_id variable.

[0058] 2. Write the Nginx configuration file: First, define the basic configurations worker_processes and error_log logs / error.log debug. Among them, worker_processes defines the number of worker threads, and error_log logs / error.log debug specifies the file path of the error log and the log level. Then, define the time module configurations events and worker_connections, and set the maximum number of connections for each worker process. Next, define the HTTP configuration block. upstream defines an upstream server group for load balancing, configures all server addresses, and hash $result_id means to perform a hash operation using the result_id variable obtained by the Lua script to select the routed server. The same result_id will be routed to the same server, thus achieving session persistence. Then, define the server configuration and the request processing logic. Specifically, listen defines the Nginx listening port, server_name defines the server name, location defines the matching specific request path, calls the Lua script and specifies the session persistence policy, and proxy_pass forwards the request to the upstream server group to complete the hash operation and select the route.

[0059] 3. Start the Nginx service: Specifically, use the sudo systemctl start nginx command to start the Nginx service.

[0060] It should be noted that the session persistence method based on Nginx and Lua provided in the embodiments of this application is implemented based on these written Lua scripts and Nginx configuration files.

[0061] S102. Analyze the request information and match the corresponding request path based on the location block information in the Nginx configuration file;

[0062] In the embodiments of this application, when the request information of the client arrives at the Nginx server, Nginx, as a reverse proxy server, first analyzes the request information, specifically including: analyzing the HTTP method, such as GET, POST, PUT, DELETE, etc.; analyzing the URI (Uniform Resource Identifier in the request message): The Nginx server matches the configuration based on this information; analyzing the Headers: extracting the request header information, which can be used for subsequent processing; analyzing the Body: If there is Body data, the Nginx server will also read this part for use in subsequent processing logic.

[0063] Since the location block in Nginx is an important configuration unit for matching and processing client requests based on the requested URI (Uniform Resource Identifier). The location block can be nested within the server block or within other location blocks to implement complex request - handling logic. Further, the location block defines a location - matching rule in the Nginx configuration file. When a client sends a request, Nginx matches the location block based on the request's URI and executes the corresponding processing instructions. The location block can not only match specific paths but also perform more complex matching using regular expressions. Therefore, in the embodiments of this application, after parsing the request information, based on the URI in the request information, the location blocks in the Nginx configuration file are traversed one by one to find the most - matching rule to determine the request path corresponding to this URI. Exemplarily, if the URI in the request information is " / api / data" and there is a location / api{...} in the configuration file, then this request information will match this block.

[0064] Further, once a matching location block is found, the Nginx server will execute the processing logic defined within the block, such as: forwarding the request, executing Lua scripts, etc.

[0065] S103. If the request path matches successfully, call the Lua script within the location block and determine the session - persistence strategy to be adopted;

[0066] In the embodiments of this application, users can customize the session - persistence strategy. By adjusting the Lua script, various routing strategies based on IP address, user information, URL, Cookie, and Token can be implemented to flexibly handle different usage scenarios. In addition, the Lua script also defines the association relationship between cached data (Cookie) and user information, enhancing the security of the Cookie and avoiding the risk of session loss caused by users clearing the Cookie or the Cookie expiring.

[0067] Further, referring to Figure 2 , step S103 includes the following sub - steps:

[0068] S1031. Obtain the request parameters in the request information;

[0069] S1032. Determine the parameter type of the request parameters;

[0070] S1033. Assign a value to the target ID variable in the Lua script according to the parameter type by calling the corresponding method;

[0071] S1034. After the assignment is completed, run the Lua script to determine the session retention policy adopted.

[0072] In one embodiment, the parameter types include IP address, user ID, URL, and message request body BODY. Specifically, according to different parameter types, the present application includes the following several ways of calling functions to assign values to the target ID variable (i.e., the refined implementation of step S1033):

[0073] The first one (parameter type is IP address): Obtain the IP address in the request information through the ngx.var.remote_addr method, and assign this IP address to the target ID variable (result_id variable). This method can be used to identify users or perform access control.

[0074] The second one (parameter type is user ID):

[0075] Obtain the user ID (if it exists) from the request headers in the request information through the ngx.req.get_headers()["User-ID"] method, and assign this user ID to the target ID variable. This method is usually used for authentication or personalized services.

[0076] The third one (parameter type is URL): Obtain the complete URI in the request information through the ngx.var.request_uri method, and assign this complete URI to the target ID variable. This method is applicable in some routing or logging scenarios.

[0077] The fourth one (parameter type is message request body BODY): First, introduce the JSON library to parse the JSON data in the request body of the request information, then read the request body through the ngx.req.read_body() method, and finally obtain and parse this JSON data through the ngx.req.get_body_data() method, and assign the specified parameter (such as: username) to the target ID variable.

[0078] After the above assignment process for the target ID variable, run the Lua script to determine the session persistence strategy to be adopted. It should be noted that the session persistence strategy in this application is a variety of routing strategies, rather than a routing strategy based on a single parameter type. Exemplarily, it can be a session persistence strategy based on the client IP address, a session persistence strategy based on the client user information, a session persistence strategy based on the URL in the client request information, a session persistence strategy based on the client Cookie, or it can also be a session persistence strategy based on the client Token. In a specific application scenario, to ensure the correctness and persistence of session maintenance, it can be a fusion strategy of session persistence strategies based on multiple parameter types.

[0079] S104. Based on the upstream block information in the Nginx configuration file, and forward the request information to the specified backend server according to the determined session persistence strategy, so that the same user requests are routed to the same backend server.

[0080] In the embodiment of this application, when processing the client's request information and forwarding the request information to the specified backend server, it is necessary to consider the load balancing of the backend server to avoid communication problems caused by the overload of the backend server. Specifically, since the upstream block in the Nginx configuration file defines an upstream server group for load balancing and configures all server addresses, in one embodiment, based on the server group preset in the upstream block of the Nginx configuration file, the backend servers are processed for load balancing by means of round-robin, IP hashing, etc. In addition, to ensure that the routing forwarding is completed based on the adopted session persistence strategy, it is necessary to perform a hashing operation on the assignment of the target ID variable to determine the target backend server to which the request information is forwarded, and in the location block, forward the request information to the specified backend server (the target backend server) through the proxy_pass directive, so that the same user requests are routed to the same backend server.

[0081] In the embodiments of the present application, through the combination of the Nginx configuration file and the Lua script, since the processing logic defined in the location block of the Nginx configuration file is to call and execute the Lua script, and the Lua script can customize various routing policies based on IP addresses, user information, URLs, Cookies, Tokens, etc., when calling the Lua script in the Nginx configuration file and specifying the session persistence policy, this session persistence policy can flexibly handle different usage scenarios, specifically including: 1. When a user switches the network IP or is in a shared IP environment, they may face problems such as session loss and uneven load. Through the Lua script, intelligent routing can be performed based on the user identity information, thus ensuring the stability of the session; 2. Through the Lua script (using polling, IP hashing, etc. to perform load balancing processing on the backend servers based on the server group preset in the upstream block of the Nginx configuration file), the load policy can be adjusted in real time, optimizing resource allocation and improving the overall performance of a session persistence system based on Nginx and Lua provided by the present application; 3. When the URL parameter in the request information changes, the requests of the same user may be distributed to different backend servers. The Lua script can standardize the request path (the location definition matches a specific request path), maintaining the consistency of the session. In addition, for applications with a large amount of dynamic content, traditional policies may cause uneven load, while the flexibility of the Lua script allows real-time adjustment of the load to optimize performance; 4. When a user clears the Cookie or the Cookie expires, causing session loss, since the Lua script records the association relationship between the Cookie and the user information, the security of the Cookie can be enhanced with the help of the Lua script. Further, an effective session recovery mechanism can be designed to prevent session anomalies caused by Cookie tampering, reducing the risk of session loss, thereby improving the stability and reliability of the system of the present application. In summary, the present application realizes routing and forwarding the same user requests from the client to the same backend server (session persistence) and load balancing.

[0082] As Figure 3 shown, the embodiments of the present application provide a session persistence system based on Nginx and Lua. The session persistence system includes a client module, a routing and forwarding module, and a backend server module. Among them, the client module is used to send the request message of the user, the routing and forwarding module is used to execute the technical processing steps in any of the foregoing method embodiments of the present application, and the backend server module is used to receive the client request information routed by the routing and forwarding module.

[0083] In addition, the routing and forwarding module further includes a custom configuration module, and the custom configuration module is used for users to configure different session routing policies.

[0084] A session persistence system based on Nginx and Lua in the embodiments of the present application can be composed of multiple software modules, loading the technical features provided by any one of the foregoing method embodiments, and realizing that the same user requests from the client are always routed and forwarded to the same backend server (session persistence) and load balancing.

[0085] The embodiments of the present application also provide a computer-readable storage medium, on which a computer program is stored. When the computer program is executed by a processor, the steps of the session persistence method based on Nginx and Lua provided by any one of the foregoing method embodiments are realized.

[0086] The device / system embodiments described above are merely illustrative. The units described as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed to multiple network units. Some or all of the modules can be selected according to actual needs to achieve the purpose of the solution of this embodiment.

[0087] Through the description of the above embodiments, those skilled in the art can clearly understand that each embodiment can be implemented by means of software plus a general hardware platform, and of course, it can also be implemented by hardware. Based on such an understanding, the essence of the above technical solution or the part that contributes to the related technology can be embodied in the form of a software product. The computer software product can be stored in a computer-readable storage medium, such as ROM / RAM, magnetic disk, optical disk, etc., including several instructions for causing a computer device (which can be a personal computer, a server, or a network device, etc.) to execute the methods described in each embodiment or some parts of the embodiments.

[0088] It should be understood that the terms used in this specification are only for the purpose of describing specific example embodiments and are not intended to be limiting. Unless the context clearly indicates otherwise, the singular forms "a", "an", and "the" as used in this specification may also include the plural forms. The terms "comprising", "including", "containing", and "having" are inclusive and thus specify the presence of the stated features, steps, operations, elements, and / or components, but do not preclude the presence or addition of one or more other features, steps, operations, elements, components, and / or combinations thereof. The method steps, processes, and operations described in this specification are not to be construed as necessarily requiring them to be executed in the specific order described or illustrated, unless the execution order is clearly indicated. It should also be understood that additional or alternative steps can be used.

[0089] The above are only specific embodiments of the present invention, enabling those skilled in the art to understand or implement the present invention. Various modifications to these embodiments will be obvious to those skilled in the art, and the general principles defined herein can be implemented in other embodiments without departing from the spirit or scope of the present invention. Therefore, the present invention will not be limited to these embodiments shown herein, but rather will be accorded the widest scope consistent with the principles and novel features claimed herein.

Claims

1. A session maintenance method based on Nginx and Lua, characterized in that: The method comprises: Get the request information initiated by the client; Parse the request information and match the corresponding request path based on the location block information in the Nginx configuration file; If the request path matches successfully, the Lua script in the location block is called and the session persistence strategy to be adopted is determined; Based on the upstream block information in the Nginx configuration file and according to the determined session persistence policy, the request information is forwarded to the designated backend server so that the same user request is routed to the same backend server; The calling of the Lua script in the location block and determining the adopted session retention strategy includes: Obtaining request parameters in the request information; Determining a parameter type of the request parameter; Calling a corresponding method according to the parameter type to assign a value to a target ID variable in the Lua script; After the assignment is completed, the Lua script is run to determine the adopted session retention strategy; By customizing and adjusting the Lua script, the session persistence strategy is customized to implement a variety of routing strategies for routing client request information to the backend server, wherein the multiple routing strategies at least include: routing strategies based on IP address, user information, URL, Cookie and Token; A fusion strategy based on session persistence strategies of multiple parameter types to achieve customization of the session persistence strategy; When switching network IP or being in a shared IP environment, intelligent routing is performed according to the user information through the Lua script; The Lua script is used to define the association between the cache data and the user information to avoid session loss caused by clearing cookies or cookie expiration, and a session recovery mechanism is designed to prevent session anomalies caused by cookie tampering; The load is adjusted in real time via the Lua script to optimize performance.

2. The method according to claim 1, characterized in that The parameter types include an IP address, a user ID, a URL, and a message request body BODY, and the calling of a corresponding method according to the parameter type to assign a value to a target ID variable in the Lua script includes: Obtain the IP address in the request information through the ngx.var.remote_addr method, and assign the IP address to the target ID variable; Get the user ID from the request header in the request information through the ngx.req.get_headers() method, and assign the user ID to the target ID variable; Obtain the complete URI in the request information through the ngx.var.request_uri method, and assign the complete URI to the target ID variable; First, introduce the JSON library to parse the JSON data in the request body of the request information, then read the request body through the ngx.req.read_body() method, and finally obtain and parse the JSON data through the ngx.req.get_body_data() method, and assign the specified parameters to the target ID variable.

3. The method according to claim 1 or 2, characterized in that: The method of forwarding the request information to a designated backend server based on the upstream block information in the Nginx configuration file and according to the determined session retention strategy, so that the same user request is routed to the same backend server, includes: Performing a hash operation based on the value assigned to the target ID variable to determine the target backend server to which the request information is forwarded; Based on the server group preset in the upstream block in the Nginx configuration file, the backend servers are load balanced using software. Based on the determined target backend server and load balancing processing, the request information is forwarded to the designated backend server through preset instructions, so that the same user request is routed to the same backend server.

4. The method according to claim 1, characterized in that: The parsing of the request information and matching the corresponding request path based on the location block information in the Nginx configuration file includes: According to the URI in the request information, the location blocks in the Nginx configuration file are traversed one by one to find matching rules to determine the request path corresponding to the URI.

5. A session persistence system based on Nginx and Lua, characterized in that: The session maintenance system includes a client module, a routing forwarding module and a back-end server module, wherein the client module is used to send a user's request message, the routing forwarding module is used to execute the method described in any one of claims 1-4, and the back-end server module is used to receive the client request information routed by the routing forwarding module.

6. The system according to claim 5, characterized in that The routing forwarding module includes a custom configuration module, and the custom configuration module is used for users to configure different session routing strategies.

7. A storage medium, characterized in that: Computer executable instructions are stored, and the computer executable instructions are used to execute the method according to any one of claims 1 to 4.