Cross-domain agent method and device, electronic equipment and storage medium
By adding custom fields in the client request header and parsing these fields on the Nginx proxy server, dynamically deciding proxy forwarding is solved, and the problems of low flexibility and performance degradation caused by static configuration of cross-domain request processing in the existing technology are implemented. Efficient and manageable network communications.
Patent Information
- Application Number
- CN202411349853.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-09-26
- Publication Date
- 2025-02-18
AI Technical Summary
The existing cross-domain request processing methods require static configuration and cannot dynamically adjust the policy, resulting in low system flexibility and prone to security vulnerabilities or cross-domain access restrictions. The proxy server cannot effectively reduce the pressure on the back-end server when processing a large number of requests. Causes performance degradation.
By configuring custom fields in the client request header, including URL and parameter information for the target resource, and parsing these fields on the Nginx proxy server to decide whether to perform proxy forwarding. The target resource server processes the request according to the dynamic cross-domain request processing policy and passes the response back to the client.
It realizes dynamic cross-domain request processing, reduces system complexity, improves network communication efficiency and manageability, is suitable for various network application scenarios, and can be customized and expanded according to specific needs.
Smart Images

Figure CN119484632A_ABST
Abstract
Description
Technical Field
[0001] The present application belongs to the field of cross-domain proxy technology, and in particular, relates to a cross-domain proxy method, device, electronic device and storage medium. Background Art
[0002] In computer network communications, proxy forwarding is a common technology used to forward client requests to the back-end server to obtain network resources or exchange data. The proxy server is usually used to handle the middle-layer communication between the client and the target server.
[0003] Currently, proxies usually need to specify the address of the target server through static configuration files. If the client request requires different target resources, it is usually necessary to manually configure the proxy for each different resource, which not only increases the complexity of management and maintenance, but also is not suitable for dynamic and frequently changing network environments. Moreover, the fixed configuration used in existing cross-domain request processing cannot dynamically adjust the policy according to different client sources, which is easy to cause security vulnerabilities or restrictions on cross-domain access, reducing the flexibility of the system. At the same time, the proxy server cannot effectively reduce the pressure on the backend server when processing a large number of requests, resulting in performance degradation. Summary of the invention
[0004] In view of this, the present application aims to propose a cross-domain proxy method, device, electronic device and storage medium to solve at least one of the above-mentioned problems.
[0005] To achieve the above purpose, the technical solution of this application is implemented as follows:
[0006] In a first aspect, the present application provides a cross-domain proxy method, comprising:
[0007] Configure the client's request header, wherein a custom field is added to the request header, the value of the field is the URL of the target resource, and parameter information;
[0008] Configure an Nginx proxy server, where the Nginx proxy server is used to parse data of the custom field to determine whether to perform proxy forwarding;
[0009] Request processing: in response to receiving a client request, the Nginx proxy server forwards the received request, and the target resource server processes the request according to the dynamic cross-domain request processing strategy;
[0010] Response delivery: the target resource server generates a response after processing the request, and the Nginx proxy server delivers the response back to the client.
[0011] In a second aspect, based on the same inventive concept, the present application also provides a cross-domain proxy device, including:
[0012] A first configuration module is configured to configure a request header of a client, wherein a custom field is added to the request header, the value of the field being a URL of a target resource and parameter information;
[0013] The second configuration module is configured to configure an Nginx proxy server, and the Nginx proxy server is used to parse the data of the custom field to determine whether to perform proxy forwarding;
[0014] The processing module is configured to process the request. In response to receiving a client request, the Nginx proxy server forwards the received request through a proxy, and the target resource server processes the request according to a dynamic cross-domain request processing strategy.
[0015] The response module is configured to deliver the response. The target resource server generates a response after processing the request, and the Nginx proxy server delivers the response back to the client.
[0016] In a third aspect, based on the same inventive concept, the present application also provides an electronic device, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein when the processor executes the program, the method described in the first aspect is implemented.
[0017] In a fourth aspect, based on the same inventive concept, the present application also provides a non-transitory computer-readable storage medium, wherein the non-transitory computer-readable storage medium stores computer instructions, and the computer instructions are used to enable the computer to execute the method described in the first aspect.
[0018] Compared with the prior art, the cross-domain proxy method, device, electronic device and storage medium described in this application have the following beneficial effects:
[0019] The cross-domain proxy method, device, electronic device and storage medium described in the present application implement the proxy forwarding method in the request header by configuring the request header, Nginx proxy server and request processing. The method simplifies the implementation of proxy forwarding, reduces system complexity, makes network communication more efficient and manageable, and can be customized and expanded according to specific needs, and is suitable for various network application scenarios. BRIEF DESCRIPTION OF THE DRAWINGS
[0020] The drawings constituting a part of the present application are used to provide a further understanding of the present application. The illustrative embodiments and descriptions of the present application are used to explain the present application and do not constitute an improper limitation on the present application. In the drawings:
[0021] Figure 1 A schematic diagram of a cross-domain proxy method process described in an embodiment of the present application;
[0022] Figure 2 A schematic diagram of the structure of a cross-domain proxy device according to an embodiment of the present application;
[0023] Figure 3 This is a schematic diagram of the hardware structure of the electronic device described in the embodiment of the present application. DETAILED DESCRIPTION
[0024] In order to make the objectives, technical solutions and advantages of the present application more clearly understood, the present application is further described in detail below in combination with specific embodiments and with reference to the accompanying drawings.
[0025] It should be noted that, unless otherwise defined, the technical terms or scientific terms used in the embodiments of the present application should be the usual meanings understood by people with ordinary skills in the field to which the present application belongs. The "first", "second" and similar words used in the embodiments of the present application do not represent any order, quantity or importance, but are only used to distinguish different components. "Including" or "comprising" and similar words mean that the elements or objects appearing in front of the word cover the elements or objects listed after the word and their equivalents, without excluding other elements or objects. "Connect" or "connected" and similar words are not limited to physical or mechanical connections, but can include electrical connections, whether direct or indirect. "Up", "down", "left", "right" and the like are only used to indicate relative positional relationships. When the absolute position of the described object changes, the relative positional relationship may also change accordingly.
[0026] The embodiments of the present application are described in detail below with reference to the accompanying drawings.
[0027] See also Figure 1 As shown, this embodiment provides a cross-domain proxy method, including:
[0028] Step S101, configuring the request header of the client, wherein a custom field is added to the request header, the value of the field is the URL of the target resource, and parameter information.
[0029] In some implementations, the URL of the target resource and parameter information are combined into a URL string using the stringifyUrl() function, which is used to convert the parameter information into a query string and append it to the URL. The URL string includes the resource path and parameter information.
[0030] Specifically, in this embodiment, it is first necessary to add a field named 'X-Proxy-Url' in the client request header. The value of this field is the URL of the target resource and related parameter information. It contains the resource path and parameter information. The information contained in this request header not only indicates the location of the target resource requested, but also can carry additional request parameters to facilitate parsing and processing on the server side.
[0031] This application uses the stringifyUrl(url,params) function to merge the URL and parameter information into a complete URL. The function converts the parameters into a query string and appends it to the URL to ensure that the URL is in the correct format. stringifyUrl() is a function used to construct the target resource URL. It can merge the URL and parameter information into a complete URL string.
[0032] This step can achieve efficient proxy forwarding: by adding the 'X-Proxy-Url' field in the request header, efficient proxy forwarding of the target resource can be achieved, reducing the overhead of network communication.
[0033] Step S102: configure an Nginx proxy server, where the Nginx proxy server is used to parse data of the custom field to determine whether to perform proxy forwarding.
[0034] In some implementations, the Nginx proxy server uses the 'proxy_pass' instruction to proxy the request to the target resource address specified in the custom field in response to the existence of the custom field, depending on whether the request header contains the custom field, and retains the parameter information in the original request.
[0035] Specifically, in this embodiment, after configuring the client's request header, the next step is to perform proxy configuration on the Nginx proxy server. The specific configuration is as follows: In this configuration, the Nginx proxy server checks whether the request header contains the 'X-Proxy-Url' field. If it exists, it will use the 'proxy_pass' instruction to proxy the request to the target resource address specified in 'X-Proxy-Url' and retain the parameter information in the original request.
[0036] During the proxy process, the Nginx proxy server not only forwards the request, but also retains all parameters in the original request to ensure that the target resource server can receive all the information requested by the client and then process the request correctly.
[0037] Based on this step, the request parameter processing is completed. The Nginx proxy server can process the parameters in the request, ensure the integrity and correctness of the request forwarded by the proxy, and improve the reliability of communication.
[0038] Step S103, request processing, in response to receiving the client request, the Nginx proxy server forwards the received request by proxy, and the target resource server processes the request according to the dynamic cross-domain request processing strategy.
[0039] In some embodiments, dynamically setting a cross-origin response header on the Nginx proxy server includes dynamically generating a response header based on a request header of the client to allow the client to initiate access requests from different sources;
[0040] In response to receiving all the information requested by the client, the Nginx proxy server proxies the request to the target resource server, which parses the request and performs corresponding data processing operations based on the parsed data content.
[0041] Specifically, in this embodiment, once the Nginx proxy server receives the request and forwards it, the target resource server will process the request from the client, including parameter parsing, data processing, and response generation. According to specific business requirements, the request parameters can be further processed on the target resource server to meet specific functional requirements.
[0042] In addition, in this embodiment, in order to support cross-domain requests, the corresponding response header is configured on the Nginx proxy server, such as setting the Access-Control-Allow-Origin header to allow the client to initiate requests from different sources.
[0043] Specifically, a whitelist of origins (a list of domain names allowed to be accessed) is maintained on the server side. When a request arrives, the Origin header of the request is used to determine whether the origin is in the whitelist. If it is in the whitelist, an Access-Control-Allow-Origin response header is dynamically generated to allow the specific origin to access the resource.
[0044] This step can solve the cross-domain problem by setting the response header and improve performance and resource utilization based on the configured cache strategy.
[0045] In some implementations, the URL and parameter information are used as cache keys, and whether to cache and the cache time are determined according to business logic and based on a dynamic cache strategy, wherein the dynamic cache strategy includes short-term caching on the client side and long-term caching on the server side;
[0046] Dynamically adjust the cache strategy according to the request volume, response time, and server load, and adjust the cache behavior in real time according to the system's operating conditions (such as request volume, response time, server load, etc.) to ensure that the system can operate efficiently under different pressures.
[0047] Specifically, in this embodiment, in order to improve the efficiency of proxy forwarding and resource utilization, this embodiment configures a cache strategy on the Nginx proxy server, which helps to reduce the load of the server and speed up the response speed.
[0048] Most existing cache strategies only cache URL paths, ignoring the impact of request parameters. This means that even though the request path is the same, the returned results may be different due to different parameters (for example, paging API requests). This embodiment uses the URL and parameter information together as cache keys. This ensures that even if the URL path is the same, different request results can be cached separately due to different parameters. For example, / api / data? page=1 and / api / data? page=2 will be cached separately to avoid data conflicts.
[0049] Decide whether to cache and how long to cache based on business logic. For example, for relatively stable data (such as product lists), you can set a longer cache time; while for frequently updated data (such as inventory or user data), you can set a shorter cache time or even not cache it.
[0050] This embodiment performs short-term caching on the client side (such as a browser) to reduce repeated requests; and performs long-term caching on the server side (such as Nginx), which is suitable for relatively stable resources.
[0051] Step S104, response delivery: the target resource server generates a response after processing the request, and the Nginx proxy server delivers the response back to the client.
[0052] In some implementations, after the target resource server completes request processing, it returns the generated response data to the Nginx proxy server. After receiving the response data, the Nginx proxy server directly passes the data back to the client.
[0053] Specifically, in this embodiment, the target resource server generates a response after processing the request, and the Nginx proxy server passes the response back to the client, realizing proxy forwarding. The client will receive the response data from the target resource, which is the same as directly requesting the target resource. The communication between the client and the target resource server is seamlessly proxyed through Nginx.
[0054] The cross-domain proxy method described in this embodiment implements a proxy forwarding method in a request header by configuring a request header, an Nginx proxy server, and request processing. This method simplifies the implementation of proxy forwarding, reduces system complexity, makes network communication more efficient and manageable, and can be customized and expanded according to specific needs, and is suitable for various network application scenarios.
[0055] It should be noted that the above describes some embodiments of the present application. Other embodiments are within the scope of the appended claims. In some cases, the actions or steps recorded in the claims can be performed in an order different from that in the above embodiments and still achieve the desired results. In addition, the processes depicted in the accompanying drawings do not necessarily require the specific order or continuous order shown to achieve the desired results. In some embodiments, multitasking and parallel processing are also possible or may be advantageous.
[0056] Based on the same inventive concept, corresponding to any of the above-mentioned embodiment methods, an embodiment of the present application further provides a cross-domain proxy device.
[0057] like Figure 2 As shown, the cross-domain proxy device includes:
[0058] The first configuration module 11 is configured to configure the request header of the client, wherein a custom field is added to the request header, the value of the field being the URL of the target resource and parameter information;
[0059] The second configuration module 12 is configured to configure an Nginx proxy server, and the Nginx proxy server is used to parse the data of the custom field to determine whether to perform proxy forwarding;
[0060] The processing module 13 is configured to process the request. In response to receiving the client request, the Nginx proxy server forwards the received request by proxy, and the target resource server processes the request according to the dynamic cross-domain request processing strategy;
[0061] The response module 14 is configured for response delivery. The target resource server generates a response after processing the request, and the Nginx proxy server delivers the response back to the client.
[0062] For the convenience of description, the above devices are described in terms of functions and are divided into various modules. Of course, when implementing the embodiments of the present application, the functions of each module can be implemented in the same or multiple software and / or hardware.
[0063] The device of the above embodiment is used to implement the corresponding method in any of the above embodiments, and has the beneficial effects of the corresponding method embodiment, which will not be described in detail here.
[0064] Based on the same inventive concept, corresponding to any of the above-mentioned embodiments and methods, an embodiment of the present application also provides an electronic device, including a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein when the processor executes the program, the method described in any of the above embodiments is implemented.
[0065] Figure 3 A more specific schematic diagram of the hardware structure of an electronic device provided in this embodiment is shown, and the device may include: a processor 1010, a memory 1020, an input / output interface 1030, a communication interface 1040, and a bus 1050. The processor 1010, the memory 1020, the input / output interface 1030, and the communication interface 1040 are connected to each other through the bus 1050 in the device.
[0066] The processor 1010 can be implemented by a general-purpose CPU (Central Processing Unit), a microprocessor, an application-specific integrated circuit (ASIC), or one or more integrated circuits, and is used to execute relevant programs to implement the technical solutions provided in the embodiments of this specification.
[0067] The memory 1020 may be implemented in the form of ROM (Read Only Memory), RAM (Random Access Memory), static storage device, dynamic storage device, etc. The memory 1020 may store an operating system and other application programs. When the technical solutions provided in the embodiments of this specification are implemented by software or firmware, the relevant program codes are stored in the memory 1020 and are called and executed by the processor 1010.
[0068] The input / output interface 1030 is used to connect the input / output module to realize information input and output. The input / output module can be configured in the device as a component (not shown in the figure), or it can be externally connected to the device to provide corresponding functions. The input device may include a keyboard, a mouse, a touch screen, a microphone, various sensors, etc., and the output device may include a display, a speaker, a vibrator, an indicator light, etc.
[0069] The communication interface 1040 is used to connect a communication module (not shown) to realize communication interaction between the device and other devices. The communication module can realize communication through a wired mode (such as USB, network cable, etc.) or a wireless mode (such as mobile network, WIFI, Bluetooth, etc.).
[0070] The bus 1050 includes a path that transmits information between the various components of the device (eg, the processor 1010, the memory 1020, the input / output interface 1030, and the communication interface 1040).
[0071] It should be noted that, although the above device only shows the processor 1010, the memory 1020, the input / output interface 1030, the communication interface 1040 and the bus 1050, in the specific implementation process, the device may also include other components necessary for normal operation. In addition, it can be understood by those skilled in the art that the above device may also only include the components necessary for implementing the embodiments of the present specification, and does not necessarily include all the components shown in the figure.
[0072] The electronic device of the above embodiment is used to implement the corresponding method in any of the above embodiments, and has the beneficial effects of the corresponding method embodiment, which will not be described in detail here.
[0073] Based on the same inventive concept, corresponding to any of the above-mentioned embodiments, the present application also provides a non-transitory computer-readable storage medium, wherein the non-transitory computer-readable storage medium stores computer instructions, and the computer instructions are used to enable the computer to execute the method described in any of the above embodiments.
[0074] The computer-readable medium of this embodiment includes permanent and non-permanent, removable and non-removable media, and information storage can be implemented by any method or technology. Information can be computer-readable instructions, data structures, modules of programs, or other data. Examples of computer storage media include, but are not limited to, phase change memory (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), flash memory or other memory technology, read-only compact disk read-only memory (CD-ROM), digital versatile disk (DVD) or other optical storage, magnetic cassettes, tape disk storage or other magnetic storage devices or any other non-transmission media that can be used to store information that can be accessed by a computing device.
[0075] The computer instructions stored in the storage medium of the above embodiments are used to enable the computer to execute the method described in any of the above embodiments, and have the beneficial effects of the corresponding method embodiments, which will not be repeated here.
[0076] Those skilled in the art should understand that the discussion of any of the above embodiments is merely illustrative and is not intended to imply that the scope of the present application (including the claims) is limited to these examples. In line with the concept of the present application, the technical features in the above embodiments or different embodiments may be combined, the steps may be implemented in any order, and there are many other variations of the different aspects of the embodiments of the present application as described above, which are not provided in detail for the sake of simplicity.
[0077] In addition, to simplify the description and discussion, and in order not to make the embodiments of the present application difficult to understand, the known power supply / ground connection with the integrated circuit (IC) chip and other components may or may not be shown in the provided drawings. In addition, the device can be shown in the form of a block diagram to avoid making the embodiments of the present application difficult to understand, and this also takes into account the fact that the details of the implementation of these block diagram devices are highly dependent on the platform to be implemented in the embodiments of the present application (that is, these details should be fully within the scope of understanding of those skilled in the art). In the case of elaborating specific details (e.g., circuits) to describe exemplary embodiments of the present application, it is obvious to those skilled in the art that the embodiments of the present application can be implemented without these specific details or when these specific details are changed. Therefore, these descriptions should be considered to be illustrative rather than restrictive.
[0078] Although the present application has been described in conjunction with specific embodiments of the present application, many replacements, modifications and variations of these embodiments will be apparent to those skilled in the art from the foregoing description. For example, other memory architectures (e.g., dynamic RAM (DRAM)) may use the embodiments discussed.
[0079] The embodiments of the present application are intended to cover all such substitutions, modifications and variations that fall within the broad scope of the appended claims. Therefore, any omissions, modifications, equivalent substitutions, improvements, etc. made within the spirit and principles of the embodiments of the present application should be included in the scope of protection of the present application.
Claims
1. A cross-domain proxy method, characterized in that: include: Configure the client's request header, wherein a custom field is added to the request header, the value of the field is the URL of the target resource, and parameter information; Configure an Nginx proxy server, where the Nginx proxy server is used to parse data of the custom field to determine whether to perform proxy forwarding; Request processing: in response to receiving a client request, the Nginx proxy server forwards the received request, and the target resource server processes the request according to the dynamic cross-domain request processing strategy; Response delivery: the target resource server generates a response after processing the request, and the Nginx proxy server delivers the response back to the client.
2. The method according to claim 1, characterized in that The custom field is added to the request header, the value of which is the URL of the target resource and parameter information, including: The URL and parameter information of the target resource are combined into a URL string through the stringifyUrl() function. The stringifyUrl() function is used to convert the parameter information into a query string and append it to the URL. The URL string includes the resource path and parameter information.
3. The method according to claim 1, characterized in that The Nginx proxy server is used to parse the custom field to determine whether to perform proxy forwarding, including: The Nginx proxy server uses the 'proxy_pass' instruction to proxy the request to the target resource address specified in the custom field according to whether the request header contains the custom field, and retains the parameter information in the original request in response to the existence of the custom field.
4. The method according to claim 1, characterized in that In response to receiving the client request, the Nginx proxy server forwards the received request through a proxy, and the target resource server processes the request according to the dynamic cross-domain request processing strategy, including: Dynamically setting a cross-origin response header on the Nginx proxy server, including dynamically generating a response header according to a request header of the client to allow the client to initiate access requests from different sources; In response to receiving all the information requested by the client, the Nginx proxy server proxies the request to the target resource server, and the target resource server performs data parsing on the request and executes corresponding data processing operations according to the parsed data content.
5. The method according to claim 1, characterized in that The target resource server generates a response after processing the request, and the Nginx proxy server transmits the response back to the client, including: After the target resource server completes the request processing, it returns the generated response data to the Nginx proxy server. After receiving the response data, the Nginx proxy server directly transmits the data back to the client.
6. The method according to claim 1, characterized in that Also includes: The URL and parameter information are used as cache keys, and whether to cache and the cache time are determined according to business logic and based on a dynamic cache strategy, wherein the dynamic cache strategy includes short-term caching on the client side and long-term caching on the server side.
7. The method according to claim 6, characterized in that: Dynamically adjust cache strategy based on request volume, response time, and server load.
8. A cross-domain proxy device, characterized in that: include: A first configuration module is configured to configure a request header of a client, wherein a custom field is added to the request header, the value of the field being a URL of a target resource and parameter information; The second configuration module is configured to configure an Nginx proxy server, and the Nginx proxy server is used to parse the data of the custom field to determine whether to perform proxy forwarding; The processing module is configured to process the request. In response to receiving a client request, the Nginx proxy server forwards the received request through a proxy, and the target resource server processes the request according to a dynamic cross-domain request processing strategy. The response module is configured to deliver the response. The target resource server generates a response after processing the request, and the Nginx proxy server delivers the response back to the client.
9. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that: When the processor executes the program, the method according to any one of claims 1 to 7 is implemented.
10. A non-transitory computer-readable storage medium, characterized in that: in, The non-transitory computer-readable storage medium stores computer instructions, and the computer instructions are used to cause a computer to execute the method according to any one of claims 1 to 7.