[go: up one dir, main page]

CN119629182A - Session persistence method, system and storage medium based on Nginx and Lua - Google Patents

Session persistence method, system and storage medium based on Nginx and Lua Download PDF

Info

Publication number
CN119629182A
CN119629182A CN202510136174.9A CN202510136174A CN119629182A CN 119629182 A CN119629182 A CN 119629182A CN 202510136174 A CN202510136174 A CN 202510136174A CN 119629182 A CN119629182 A CN 119629182A
Authority
CN
China
Prior art keywords
request
session
request information
information
end server
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Granted
Application number
CN202510136174.9A
Other languages
Chinese (zh)
Other versions
CN119629182B (en
Inventor
任洋
王苛宇
万正勇
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Jiuke Information Technology Shenzhen Co ltd
Original Assignee
Jiuke Information Technology Shenzhen Co ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Jiuke Information Technology Shenzhen Co ltd filed Critical Jiuke Information Technology Shenzhen Co ltd
Priority to CN202510136174.9A priority Critical patent/CN119629182B/en
Publication of CN119629182A publication Critical patent/CN119629182A/en
Application granted granted Critical
Publication of CN119629182B publication Critical patent/CN119629182B/en
Active legal-status Critical Current
Anticipated expiration legal-status Critical

Links

Classifications

    • HELECTRICITY
    • H04ELECTRIC COMMUNICATION TECHNIQUE
    • H04LTRANSMISSION OF DIGITAL INFORMATION, e.g. TELEGRAPHIC COMMUNICATION
    • H04L67/00Network arrangements or protocols for supporting network services or applications
    • H04L67/01Protocols
    • H04L67/10Protocols in which an application is distributed across nodes in the network
    • H04L67/1001Protocols in which an application is distributed across nodes in the network for accessing one among a plurality of replicated servers
    • H04L67/1027Persistence of sessions during load balancing
    • HELECTRICITY
    • H04ELECTRIC COMMUNICATION TECHNIQUE
    • H04LTRANSMISSION OF DIGITAL INFORMATION, e.g. TELEGRAPHIC COMMUNICATION
    • H04L67/00Network arrangements or protocols for supporting network services or applications
    • H04L67/01Protocols
    • H04L67/10Protocols in which an application is distributed across nodes in the network
    • H04L67/1001Protocols in which an application is distributed across nodes in the network for accessing one among a plurality of replicated servers
    • H04L67/1004Server selection for load balancing
    • H04L67/1006Server selection for load balancing with static server selection, e.g. the same server being selected for a specific client
    • HELECTRICITY
    • H04ELECTRIC COMMUNICATION TECHNIQUE
    • H04LTRANSMISSION OF DIGITAL INFORMATION, e.g. TELEGRAPHIC COMMUNICATION
    • H04L67/00Network arrangements or protocols for supporting network services or applications
    • H04L67/2866Architectures; Arrangements
    • H04L67/30Profiles

Landscapes

  • Engineering & Computer Science (AREA)
  • Computer Networks & Wireless Communication (AREA)
  • Signal Processing (AREA)
  • Computer And Data Communications (AREA)

Abstract

The application relates to a session maintaining method and a session maintaining system based on Nginx and Lua, which are characterized by acquiring request information initiated by a client, analyzing the request information, matching corresponding request paths based on location block information in an Nginx configuration file, calling a Lua script in the location block and determining an adopted session maintaining strategy if the request paths are successfully matched, and forwarding the request information to a specified back-end server based on the upstream block information in the Nginx configuration file according to the determined session maintaining strategy so as to enable the same user request to be routed to the same back-end server, thereby realizing session maintaining and load balancing.

Description

Session maintaining method, system and storage medium based on Nginx and Lua
Technical Field
The application relates to the technical field of software development, in particular to a session maintaining method, a session maintaining system and a session maintaining storage medium based on Nginx and Lua.
Background
Session maintenance techniques are critical in Web applications, which ensure that users can continuously access the same backend services in multiple requests, thereby maintaining the state and data consistency of the user session. This is essential to provide a smooth user experience, enhance data security, and promote application performance.
In the Nginx, three common session maintaining strategies are mainly ip_hash, url_hash and stilly_cookie_insert, and the strategies enable the Nginx to effectively maintain a user session and improve the overall performance and user experience of the application. These policies have their own drawbacks, for example, if the user changes IP addresses due to network switches, session loss may result. In addition, in a shared IP environment, the method can cause uneven load distribution, different URL parameters can cause the session to be routed to different servers, uneven load can be caused to applications with more dynamic contents, and the user can remove Cookie or the Cookie is out of date, so that the session is lost.
Disclosure of Invention
The application provides a session maintaining method, a session maintaining system and a session maintaining system based on Nginx and Lua and a storage medium, which are used for solving the problems of session loss risk and unbalanced load of a session maintaining strategy commonly used in the Nginx in the prior art.
In a first aspect, the present application provides a session maintaining method based on nginnx and Lua, including:
acquiring request information initiated by a client;
Analyzing the request information, and matching corresponding request paths based on location block information in the Nginx configuration file;
If the request path is successfully matched, calling a Lua script in the location block and determining an adopted session maintenance strategy;
Based on the upstream block information in the Nginx configuration file, forwarding the request information to a designated back-end server according to the determined session maintenance policy, so that the same user request is routed to the same back-end server.
Further, the calling the Lua script in the location block and determining the adopted session maintaining policy includes:
acquiring request parameters in the request information;
Determining the 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;
and after assignment is completed, the Lua script is run to determine the adopted session maintenance strategy.
Further, the parameter type includes an IP address, a user ID, a URL, and a BODY of a message request BODY, and the invoking the corresponding method according to the parameter type assigns a value to a target ID variable in the Lua script includes:
Acquiring an IP address in the request information by an ngx.var.remote_addr method, and assigning the IP address to the target ID variable;
Obtaining a user ID from a request header in the request information through an ngx.req.get_ headers () method, and assigning the user ID to the target ID variable;
acquiring a complete URI in the request information through an ngx.var.request_uri method, and assigning the complete URI to the target ID variable;
The JSON database is introduced first for analyzing JSON data in a request body of the request information, then the request body is read through an ngx.req.read_body () method, finally the JSON data is obtained and analyzed through an ngx.req.get_body_data () method, and the designated parameters are assigned to the target ID variable.
Further, the up stream block information in the nginnx configuration file is based, and the request information is forwarded to a designated backend server according to the determined session maintenance policy, so that the same user request is routed to the same backend server, including:
performing hash operation based on the assignment of the target ID variable to determine a target back-end server for forwarding the request information;
Based on a server group preset by an upstream block in an Nginx configuration file, carrying out load balancing processing on a back-end server in a software mode;
And forwarding the request information to a designated back-end server through a preset instruction based on the determined target back-end server and load balancing processing, so that the same user request is routed to the same back-end server.
Further, the parsing the request information and matching a corresponding request path based on the location block information in the nginnx configuration file includes:
and traversing the location blocks in the Nginx configuration file one by one according to the URI in the request information to find a matched rule so as to determine a request path corresponding to the URI.
Further, the Lua script defines an association relationship between the cache data and the user information.
Further, the Lua script is adjusted by self-definition, the session maintaining policy is self-defined, and various routing policies for routing the client request information to the back-end server are realized, wherein the various routing policies at least comprise routing policies based on IP addresses, user information, URLs, cookies and Token.
In a second aspect, the application provides a session maintaining system based on Nginx and Lua, which comprises a client module, a route forwarding module and a back-end server module, wherein the client module is used for sending a request message of a user, the route forwarding module is used for executing any method of the first aspect, and the back-end server module is used for receiving the client request information routed by the route forwarding module.
Further, the routing forwarding module comprises a custom configuration module, and the custom configuration module is used for a user to configure different session routing policies.
In a third aspect, the present application also provides a computer storage medium storing computer executable instructions for performing the nginnx and Lua based session maintenance method of the present application.
The session maintaining method and system based on the Nginx and the Lua acquire request information initiated by a client, analyze the request information, match corresponding request paths based on location block information in an Nginx configuration file, call a Lua script in the location block and determine an adopted session maintaining strategy if the request paths are successfully matched, and forward the request information to a specified back-end server based on the upstream block information in the Nginx configuration file according to the determined session maintaining strategy so as to enable the same user request to be routed to the same back-end server. Thus, through the combination of the Nginx configuration file and the Lua script, as the processing logic defined in the location block in the Nginx configuration file executes the Lua script for calling, and the Lua script can self-define various routing strategies based on IP addresses, user information, URLs, cookies, token and the like, when the Lua script is called in the Nginx configuration file and a session maintaining strategy is specified, the session maintaining strategy can flexibly cope with different use scenes, and the purpose that the same user request of a client is always routed and forwarded to the same back-end server (session maintaining) and load balancing is achieved.
Drawings
The accompanying drawings, which are incorporated in and constitute a part of this specification, illustrate embodiments consistent with the invention and together with the description, serve to explain the principles of the invention.
In order to more clearly illustrate the embodiments of the invention or the technical solutions of the prior art, the drawings which are used in the description of the embodiments or the prior art will be briefly described, and it will be obvious to a person skilled in the art that other drawings can be obtained from these drawings without inventive effort.
One or more embodiments are illustrated by way of example and not limitation in the figures of the accompanying drawings, in which like references indicate similar elements, and in which the figures of the drawings are not to be taken in a limiting sense, unless otherwise indicated.
Fig. 1 is a flowchart of a session maintaining method based on nginnx and Lua according to an embodiment of the present application;
FIG. 2 is a detailed flowchart of step S103 in an embodiment of the present application;
Fig. 3 is a block flow diagram of a session maintaining system based on nginnx and Lua according to an embodiment of the present application.
Detailed Description
For the purpose of making the objects, technical solutions and advantages of the embodiments of the present application more apparent, the technical solutions of the embodiments of the present application will be clearly and completely described below with reference to the accompanying drawings in the embodiments of the present application, and it is apparent that the described embodiments are some embodiments of the present application, but not all embodiments of the present application. All other embodiments, which can be made by those skilled in the art based on the embodiments of the application without making any inventive effort, are intended to be within the scope of the application.
The following disclosure provides many different embodiments, or examples, for implementing different structures of the invention. In order to simplify the present disclosure, components and arrangements of specific examples are described below. They are, of course, merely examples and are not intended to limit the invention. Furthermore, the present invention may repeat reference numerals and/or letters in the various examples. This repetition is for the purpose of simplicity and clarity and does not in itself dictate a relationship between the various embodiments and/or configurations discussed.
Nginx (engine x) is a high-performance HTTP and reverse proxy web server, and in nginnx, the common session-keeping policies mainly include three kinds of ip_hash, url_hash and stinky_cookie_insert, and the specific principles are as follows:
ip_hash-this mechanism routes requests to specific backend services by hashing the IP address based on the client's IP address. In this way, requests from the same IP address will always be directed to the same server.
Url_hash this policy ensures that users accessing the same resource are always routed to the same server by hashing the URL path of the request, thus achieving session maintenance.
The sticky Cookie insert is a Cookie-based session hold mechanism (data stored on the user's local terminal, which can also be understood as cached data). Upon first access by the user, nginx inserts a specific Cookie for tracking the user's session. In subsequent requests, the user will carry this Cookie and the nmginx will route the request to the corresponding server based on the value of the Cookie.
These policies enable the nmginx to effectively maintain user sessions, improving the overall performance and user experience of the application. However, these strategies also have respective drawbacks, such as:
The ip_hash strategy is simple and easy to use, is convenient to configure, and can rapidly realize session maintenance. However, if the user changes the IP address due to network handover, session loss may be caused. Furthermore, in a shared IP environment, this approach may lead to load maldistribution.
The url_hash strategy ensures consistency of the same resource request, is particularly suitable for static content or specific API access, and is beneficial to improving the cache utilization rate. But different URL parameters may cause sessions to be routed to different servers, and uneven loading may be caused to applications with more dynamic content, thereby affecting overall performance.
The policy can effectively cope with user IP or URL change, maintain session consistency, is suitable for high availability and load balancing environment, and flexibly cope with changeable request modes. However, attention is paid to the security of the Cookie, which can lead to session loss if the user clears the Cookie or the Cookie expires.
Based on the above, in order to solve the problems in the prior art, the application provides a session maintaining method and a session maintaining system based on Nginx and Lua, wherein the Nginx server can efficiently process requests, and flexible request processing and parameter extraction are performed by utilizing a Lua script, and meanwhile, load balancing and session maintaining are ensured.
Fig. 1 is a session maintaining method based on nginnx and Lua according to an embodiment of the present application, where the method includes:
s101, acquiring request information initiated by a client;
In the present application, session maintenance refers to that the same user request of the client is always routed and forwarded to the same back-end server. In one embodiment, the client-initiated request information refers to requests initiated by a user through a browser or other client (e.g., mobile application, API client), and in particular, these requests typically include the following components:
URL (Uniform Resource Locator, uniform resource locator, which is a uniform resource locator of WWW, refers to network address) contains the requested resource location, such as http:// sample.
Headers contains meta-information such as request Type (GET, POST, etc.), authentication information (beer Token, etc.), content Type (Content-Type, etc.).
Body data-in some requests, in particular POST requests, may contain data, typically JSON data or form data.
In the embodiment of the application, the request information initiated by the client needs to be always routed and forwarded to the same back-end server, so the request information initiated by the client needs to be acquired in the whole processing process.
In addition, the application relates to the deployment and implementation of the Nginx server because the application is a session maintaining method combining the Nginx and the Lua, and in particular, in the embodiment of the application, the deployment steps and implementation scheme of the Nginx server comprise the following aspects:
1. The Lua script is written by first initializing a result_id variable (target ID variable) with an initial value of null. A Lua script is then defined by rewrite _by_Lua to process the client request and assign a value to result_id. The method specifically comprises the steps of obtaining a client IP through an ngx.var.remote_addr method and setting a result_id as the client IP if a request parameter in the client request is an IP address, obtaining a USER ID in a header through an ngx.req.get_ headers () method and setting the result_id as the USER ID if the request parameter in the client request is a USER, obtaining a request URI of the client through an ngx.var.request_uri method and setting the result_id as a URI if the request parameter in the client request is a specified parameter in a body, firstly introducing JSON database through a local json=request ("cjson.sase") for analyzing JSON data in a request body, then reading the request body through an ngx.req.read_body () method and finally setting the request URI of the client through an ngx.var.request_uri=node_request_uri method and setting the result_id as a URI if the request parameter in the client request is a body.
2. An Nginx configuration file is written, wherein basic configuration worker_process and error_log logs/log debug are defined firstly, the worker_process defines the number of working threads, and the error_log logs/log debug specifies the file path and log level of an error log. Defining a time module to configure events and worker_ connections, setting the maximum connection number of each working process, defining an HTTP configuration block, defining an upstream server group by upstream for load balancing, configuring all server addresses, performing hash operation by using a result_id variable acquired by a Lua script by hash $result_id, and routing the same result_id to the same server to realize session maintenance. Then defining server configuration and request processing logic, specifically, listen defines Nginx monitor port, server_name defines server name, location definition matches specific request path, calling Lua script and appointing session holding strategy, proxy_pass forwards the request to upstream server group to complete hash operation and select route.
3. The Nginx service is started, specifically, the sudo SYSTEMCTL START ginx command is used for starting the Nginx service.
It should be noted that, the session maintaining method based on nmginx and Lua provided by the embodiment of the present application is implemented based on these written Lua scripts and nmginx configuration files.
S102, analyzing the request information, and matching corresponding request paths based on location block information in an Nginx configuration file;
In the embodiment of the application, when the request information of the client reaches the Nginx server, the Nginx serves as a reverse proxy server, and the method comprises the steps of analyzing HTTP (hyper text transport protocol) methods such as GET, POST, PUT, DELETE and the like, analyzing URI (uniform resource identifier in request information) that the Nginx server matches configuration according to the information, analyzing Headers that request header information is extracted and can be used for subsequent processing, and analyzing Body that if Body data exists, the Nginx server can read the portion so as to be used in subsequent processing logic.
Since the location block in ‌ ng is an important configuration unit, it is used to match and process client requests according to their URI (uniform resource identifier). ‌ location blocks can be nested in server blocks or in other location blocks, thereby implementing complex request processing logic. Further, the location block defines a location matching rule in the nmginx configuration file. When the client sends a request, the nginnx matches the location block according to the URI of the request, and executes a corresponding processing instruction. The location block can not only match a specific path, but also can perform more complex matching through regular expressions. Therefore, in the embodiment of the application, after the request information is analyzed, the most matched rule is searched for by traversing the location blocks in the Nginx configuration file one by one according to the URI in the request information so as to determine the request path corresponding to the URI. Illustratively, if the URI in the request message is "/api/data" and there is location/api {. In the configuration file, then the request message will match to the block.
Further, once a matching location block is found, the Nginx server will execute processing logic defined within the block, such as forwarding the request, executing the Lua script, etc.
S103, if the request path is successfully matched, calling a Lua script in the location block and determining an adopted session maintenance strategy;
In the embodiment of the application, a user can customize a session maintaining strategy, and a plurality of routing strategies based on IP addresses, user information, URLs, cookies, token and the like are realized by adjusting the Lua script so as to flexibly cope with different use scenes. In addition, the Lua script also defines the association relation between the cache data (Cookie) and the user information, enhances the safety of the Cookie, and avoids the session loss risk caused by the removal of the Cookie or the expiration of the Cookie by the user.
Further, referring to fig. 2, step S103 includes the following sub-steps:
s1031, acquiring request parameters in the request information;
S1032, determining the parameter type of the request parameter;
s1033, calling a corresponding method according to the parameter type to assign a value to a target ID variable in the Lua script;
and S1034, after assignment is completed, running the Lua script to determine the adopted session maintenance strategy.
In an embodiment, the parameter types include IP address, user ID, URL and message request BODY, specifically, according to different parameter types, the present application includes the following ways of assigning values to the target ID variable by function calls (i.e. the refinement of step S1033):
the first type (parameter type is IP address) is that the IP address in the request information is obtained by an ngx.var.remote_addr method, and the IP address is assigned to a target ID variable (result_id variable), and the method can be used for identifying users or performing access control;
second (parameter type is user ID):
Obtaining a User ID (if present) from a request header in the request message by the ngx.req.get_ headers () [ "User-ID" ] method, which is typically used for authentication or personalization services, and assigning the User ID to a target ID variable;
Thirdly, obtaining a complete URI in the request information through an ngx.var.request_uri method, and assigning the complete URI to a target ID variable, wherein the method is applicable to certain routing or logging scenes;
And fourthly (the parameter type is a message request BODY BODY), firstly introducing JSON data in a request BODY for analyzing request information into a JSON library, then reading the request BODY through an ngx.req.read_body () method, finally acquiring and analyzing the JSON data through an ngx.req.get_body_data () method, and assigning a specified parameter (such as a username) to a target ID variable.
After the assignment process to the target ID variable is performed as described above, the Lua script is run to determine the session maintenance policy to be employed. It should be noted that, the session maintaining policy in the present application is a plurality of routing policies, not a single routing policy based on a certain parameter type, and may be exemplified by a session maintaining policy based on a client IP address, a session maintaining policy based on client user information, a session maintaining policy based on URL in client request information, a session maintaining policy based on a client Cookie, or a session maintaining policy based on a client Token, where in a specific application scenario, in order to ensure correctness and persistence of session maintaining, a session maintaining policy based on a plurality of parameter types may be integrated policy.
S104, based on the up stream block information in the Nginx configuration file, forwarding the request information to a designated back-end server according to the determined session maintaining strategy so as to enable the same user request to be routed to the same back-end server.
In the embodiment of the application, when the request information of the client is processed and forwarded to the designated back-end server, the load balance of the back-end server needs to be considered so as to avoid the communication problem caused by overload of the back-end server. Specifically, since the upstream block in the ngnx configuration file defines an upstream server group for load balancing and configures all server addresses, in an embodiment, load balancing is performed on the backend server by adopting modes such as polling and IP hashing based on the server group preset by the upstream block in the ngnx configuration file. In addition, to ensure that the routing forwarding is completed based on the session maintenance policy adopted, hash operation is required to be performed on the assignment of the target ID variable to determine the target backend server for forwarding the request information, and in the location block, the request information is forwarded to the designated backend server (target backend server) through the proxy_pass instruction, so that the same user request is routed to the same backend server.
In the embodiment of the application, through the combination of the Nginx configuration file and the Lua script, as the processing logic defined in the location block in the Nginx configuration file executes the Lua script for calling, and the Lua script can self-define various routing strategies based on IP addresses, user information, URLs, cookies, token and the like, when the Lua script is called in the Nginx configuration file and the session maintenance strategy is appointed, the session maintenance strategy can flexibly cope with different use scenes, and particularly comprises 1, the problem that a user possibly suffers from session loss and uneven load when switching network IP or being in a shared IP environment. The application provides a session maintaining system based on Nginx and Lua, which comprises a server group, a resource allocation and a resource allocation, wherein the server group is preset based on an upstream block in an Nginx configuration file, and the load balancing processing is carried out on a back-end server in a polling, IP hash and other modes, the load policy can be adjusted in real time, the resource allocation is optimized, the overall performance of the session maintaining system based on Nginx and Lua is improved, and 3, when URL parameters in request information are changed, the request of the same user is possibly dispersed to different back-end servers. The Lua script can normalize the request path (location definition matches a particular request path) to maintain consistency of the session. In addition, for applications with more dynamic content, the traditional strategy can cause uneven load, and the flexibility of the Lua script allows real-time adjustment of the load to optimize performance, 4, when a user clears Cookie or the Cookie is out of date to cause the loss of a session, as the Lua script records the association relationship between the Cookie and the user information, the security of the Cookie can be enhanced by means of the Lua script, and further, an effective session recovery mechanism can be designed, so that the abnormal session caused by the tampering of the Cookie is prevented, the risk of session loss is reduced, and the stability and the reliability of the system of the application are improved. In summary, the application realizes the constant route forwarding of the same user request of the client to the same back-end server (session maintenance) and load balancing.
As shown in fig. 3, an embodiment of the present application provides a session maintaining system based on nginnx and Lua, where the session maintaining system includes a client module, a routing forwarding module, and a back-end server module, where the client module is configured to send a request message of a user, the routing forwarding module is configured to execute a technical processing step in any one of the foregoing method embodiments of the present application, and the back-end server module is configured to receive client request information routed by the routing forwarding module.
In addition, the route forwarding module further comprises a custom configuration module, wherein the custom configuration module is used for a user to configure different session routing strategies.
The session maintaining system based on Nginx and Lua in the embodiment of the application can be composed of a plurality of software modules, loads the technical characteristics provided by any one of the method embodiments, and realizes that the same user request of the client is always routed and forwarded to the same back-end server (session maintaining) and load balancing.
The embodiment of the present application also provides a computer readable storage medium having stored thereon a computer program which, when executed by a processor, implements the steps of the ng nx and Lua based session maintenance method provided by any one of the method embodiments described above.
The apparatus/system embodiments described above are merely illustrative, wherein the elements illustrated as separate elements may or may not be physically separate, and the elements shown as elements may or may not be physical elements, may be located in one place, or may be distributed over a plurality of network elements. Some or all of the modules may be selected according to actual needs to achieve the purpose of the solution of this embodiment.
From the above description of embodiments, it will be apparent to those skilled in the art that the embodiments may be implemented by means of software plus a general purpose hardware platform, or may be implemented by hardware. Based on such understanding, the foregoing technical solution may be embodied essentially or in a part contributing to the related art in the form of a software product, which may be stored in a computer readable storage medium, such as ROM/RAM, a magnetic disk, an optical disk, etc., including several instructions for causing a computer device (which may be a personal computer, a server, or a network device, etc.) to perform the method described in the respective embodiments or some parts of the embodiments.
It is to be understood that the terminology used herein is for the purpose of describing particular example embodiments only, and is not intended to be limiting. As used herein, the singular forms "a", "an" and "the" are intended to include the plural forms as well, unless the context clearly indicates otherwise. The terms "comprises," "comprising," "includes," "including," and "having" are inclusive and therefore specify the presence of 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 groups thereof. The method steps, processes, and operations described herein are not to be construed as necessarily requiring their performance in the particular order described or illustrated, unless an order of performance is explicitly stated. It should also be appreciated that additional or alternative steps may be used.
The foregoing is only a specific embodiment of the invention to enable those skilled in the art to understand or practice the invention. Various modifications to these embodiments will be readily apparent to those skilled in the art, and the generic principles defined herein may be applied to other embodiments without departing from the spirit or scope of the invention. Thus, the present invention is not intended to be limited to the embodiments shown herein but is to be accorded the widest scope consistent with the principles and novel features disclosed herein.

Claims (10)

1. A method for maintaining a session based on nginnx and Lua, the method comprising:
acquiring request information initiated by a client;
Analyzing the request information, and matching corresponding request paths based on location block information in the Nginx configuration file;
If the request path is successfully matched, calling a Lua script in the location block and determining an adopted session maintenance strategy;
Based on the upstream block information in the Nginx configuration file, forwarding the request information to a designated back-end server according to the determined session maintenance policy, so that the same user request is routed to the same back-end server.
2. The method of claim 1, wherein the invoking the Lua script within the location block and determining the session maintenance policy to employ comprises:
acquiring request parameters in the request information;
Determining the 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;
and after assignment is completed, the Lua script is run to determine the adopted session maintenance strategy.
3. The method according to claim 2, wherein the parameter types include an IP address, a user ID, a URL, and a message request BODY, and the invoking the corresponding method according to the parameter types assigns a value to a target ID variable in the Lua script includes:
Acquiring an IP address in the request information by an ngx.var.remote_addr method, and assigning the IP address to the target ID variable;
Obtaining a user ID from a request header in the request information through an ngx.req.get_ headers () method, and assigning the user ID to the target ID variable;
acquiring a complete URI in the request information through an ngx.var.request_uri method, and assigning the complete URI to the target ID variable;
The JSON database is introduced first for analyzing JSON data in a request body of the request information, then the request body is read through an ngx.req.read_body () method, finally the JSON data is obtained and analyzed through an ngx.req.get_body_data () method, and the designated parameters are assigned to the target ID variable.
4. A method according to claim 2 or 3, wherein said forwarding the request information to a designated back-end server based on the upstream block information in the nginix profile and according to the determined session maintenance policy to route the same user request to the same back-end server comprises:
performing hash operation based on the assignment of the target ID variable to determine a target back-end server for forwarding the request information;
Based on a server group preset by an upstream block in an Nginx configuration file, carrying out load balancing processing on a back-end server in a software mode;
And forwarding the request information to a designated back-end server through a preset instruction based on the determined target back-end server and load balancing processing, so that the same user request is routed to the same back-end server.
5. The method of claim 1, wherein the parsing the request information and matching the corresponding request path based on location block information in the nginnx configuration file comprises:
and traversing the location blocks in the Nginx configuration file one by one according to the URI in the request information to find a matched rule so as to determine a request path corresponding to the URI.
6. The method of claim 1, wherein the Lua script defines an association between cached data and user information.
7. The method of claim 1, wherein customizing the session maintenance policy is implemented by customizing the Lua script, and wherein a plurality of routing policies for routing client request information to a back-end server are implemented, wherein the plurality of routing policies at least include routing policies based on IP addresses, user information, URLs, cookies, and Token.
8. A session keeping system based on nginnx and Lua, wherein the session keeping system comprises a client module, a route forwarding module and a back-end server module, wherein the client module is configured to send a request message of a user, the route forwarding module is configured to perform the method of any one of claims 1 to 7, and the back-end server module is configured to receive client request information routed by the route forwarding module.
9. The system of claim 8, wherein the routing forwarding module comprises a custom configuration module for a user to configure different session routing policies.
10. A storage medium having stored thereon computer executable instructions for performing the method of any one of claims 1-7.
CN202510136174.9A 2025-02-07 2025-02-07 Session persistence method, system and storage medium based on Nginx and Lua Active CN119629182B (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202510136174.9A CN119629182B (en) 2025-02-07 2025-02-07 Session persistence method, system and storage medium based on Nginx and Lua

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202510136174.9A CN119629182B (en) 2025-02-07 2025-02-07 Session persistence method, system and storage medium based on Nginx and Lua

Publications (2)

Publication Number Publication Date
CN119629182A true CN119629182A (en) 2025-03-14
CN119629182B CN119629182B (en) 2025-06-20

Family

ID=94902531

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202510136174.9A Active CN119629182B (en) 2025-02-07 2025-02-07 Session persistence method, system and storage medium based on Nginx and Lua

Country Status (1)

Country Link
CN (1) CN119629182B (en)

Cited By (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN120528923A (en) * 2025-07-24 2025-08-22 宁波银行股份有限公司 A session maintenance method and a session maintenance platform

Citations (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US7873734B1 (en) * 2001-05-17 2011-01-18 Computer Associates Think, Inc. Management of multiple user sessions and user requests for multiple electronic devices
CN107360251A (en) * 2017-08-16 2017-11-17 中国工商银行股份有限公司 Method, system and the load balancing apparatus that session is kept
CN111371809A (en) * 2020-03-27 2020-07-03 武大吉奥信息技术有限公司 Server based on reverse proxy architecture and GIS service access control method
CN117714155A (en) * 2023-12-14 2024-03-15 天翼电子商务有限公司 Web page access request processing method and device, storage media and electronic equipment

Patent Citations (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US7873734B1 (en) * 2001-05-17 2011-01-18 Computer Associates Think, Inc. Management of multiple user sessions and user requests for multiple electronic devices
CN107360251A (en) * 2017-08-16 2017-11-17 中国工商银行股份有限公司 Method, system and the load balancing apparatus that session is kept
CN111371809A (en) * 2020-03-27 2020-07-03 武大吉奥信息技术有限公司 Server based on reverse proxy architecture and GIS service access control method
CN117714155A (en) * 2023-12-14 2024-03-15 天翼电子商务有限公司 Web page access request processing method and device, storage media and electronic equipment

Non-Patent Citations (2)

* Cited by examiner, † Cited by third party
Title
HUANGYIMO: "NGINX+LUA实现复杂的控制", 《CSDN- HTTPSBLOG.CSDN.NETHUANGYIMOARTICLEDETAILS80791816》, 24 June 2018 (2018-06-24), pages 3 *
唯有遗忘最漫长: "Nginx 实现会话保持的方式配置", 《CSDN- HTTPS://BLOG.CSDN.NET/M0_66572126/ARTICLE/DETAILS/142266592》, 14 September 2024 (2024-09-14), pages 2 - 5 *

Cited By (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN120528923A (en) * 2025-07-24 2025-08-22 宁波银行股份有限公司 A session maintenance method and a session maintenance platform

Also Published As

Publication number Publication date
CN119629182B (en) 2025-06-20

Similar Documents

Publication Publication Date Title
US10791201B2 (en) Server initiated multipath content delivery
US9325785B2 (en) Device, system, and method for client-governed session persistency between one or more clients and servers of a data center
EP3249546B1 (en) Content delivery network
USRE45806E1 (en) System and method for the optimization of database access in data base networks
US9613076B2 (en) Storing state in a dynamic content routing network
US10560543B2 (en) Rule based cache processing in application delivery controller for load balancing
US8380855B2 (en) HTTP header intermediary for enabling session-based dynamic site searches
CN113452780B (en) Access request processing method, device, equipment and medium for client
US20050265317A1 (en) Managing the flow of data traffic
US11252147B2 (en) Secure web application delivery platform
US11853806B2 (en) Cloud computing platform that executes third-party code in a distributed cloud computing network and uses a distributed data store
CN119629182B (en) Session persistence method, system and storage medium based on Nginx and Lua
US20090094611A1 (en) Method and Apparatus for Load Distribution in Multiprocessor Servers
WO2022036833A1 (en) Cookie management method, intermediate node, and webvpn system
US11621995B2 (en) Preloading on-demand code pieces in a distributed cloud computing network
WO2023097748A1 (en) Traffic proxy method and system, and smart terminal and storage medium
US12095730B2 (en) Controlling access to resources based on policies at different layers of a networking model
US10958580B2 (en) System and method of performing load balancing over an overlay network
KR20230003490A (en) Orchestrated proxy service
TW201828093A (en) Visit request conversion method and device that identifies a target service type of a visit request and breaks down the visit request to a data structure corresponding to the target service type to be supplied to a corresponding server
US20240311439A1 (en) Tenant network for rewriting of code included in a web page
KR102852608B1 (en) System and method for contents routing
Petroski et al. ANALISYS OF A EVENT ORIENTED WEB SERVER AS A REVERSE PROXY WITH FLEXIBLE LOAD DISTRIBUTION
CN120729927A (en) Content acquisition method for cloud phone and content acquisition system for cloud phone
CN119583619A (en) Communication method, system, device and storage medium

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination
GR01 Patent grant
GR01 Patent grant