SoFunction
Updated on 2025-03-03

Nginx uses mirror instruction to implement interface replication

In Nginx, the replication of interface requests can be easily achieved using the mirror instruction. This feature is ideal for traffic monitoring, data collection or load balancing. Here is a specific example showing how to configure Nginx to implement mirroring of interface requests.

Nginx configuration example

The following example shows how to use itmirrorThe directive copies the request to another backend service.

server {
  listen 80; # Listen to port 80  server_name ; # Server Name  
  #Main request processing  location /api/ {
   proxy_pass http://backend:5000; # Forward the request to the main backend service   
   # Set the path to the mirror request   mirror /mirror_api; #Specify the mirror path   }
   #Processing mirror requests   location =/mirror_api{
   inrernal; # Only internal requests are allowed   proxy_pass http://backup_backend:6000;#Forward the mirror request to the alternate backend service   
   #Set the necessary proxy header   proxy_set_header Host $host;
   proxy_set_header X-Real-IP $remote_addr;
   proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
   proxy_set_header X-Forwarded-Proto $scheme;
   }
  }

Configuration instructions

1. Main request processing

  • location /api/: Handle all pairs/api/Request for path.
  • proxy_pass http://backend:5000;: Forward the request to the main backend service.

2. Mirror request

mirror /mirror_api;: Copy and forward the request to the specified mirror path/mirror_api

3. Processing of mirror requests

  • location = /mirror_api: Define an internal path that is only used to handle mirror requests.
  • proxy_pass http://backup_backend:6000;: Forward the mirror request to the alternate backend service.

4. Internal protection

internal;: Ensure that the path can only be called by Nginx internal requests to prevent direct external access.

5. Proxy header settings

Some common request headers are set to ensure that the backend service can correctly identify the request source.

Test and restart Nginx

After the configuration is complete, you can test the configuration file with the following command and restart Nginx to make it take effect:

sudo nginx -t  # Test configuration filesudo systemctl restart nginx  # Restart Nginx

Test the mirroring function

Can be accessed through a browser or API client/api/, you will find:

  • The normal request will be processed and a response from the main backend service will be returned.
  • At the same time, the same request will also be mirrored tohttp://backup_backend:6000, although the client is not aware of the process.

Things to note

  • Performance considerations: Mirror requests will increase the load on the backend service, please use it with caution as needed.
  • Error handling: The processing of mirror requests and the main requests are separated, ensuring that each backend service can properly handle the corresponding requests.
  • Applicable scenarios: Suitable for use in various scenarios such as data collection, performance monitoring, etc.

With this configuration, you can efficiently implement interface request mirroring for traffic monitoring and data analysis.

This is the article about Nginx using mirror instruction to implement interface copying. For more information about Nginx mirror interface copying, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!