6

I want to test an early new version of a server, and to do so I want to mirror part of the real requests that come to the production server on my testing server.

The mirror module allows me do to mirror all the traffic, but I haven't found how to perform partial mirroring of the requests, for instance, I want to mirror 30% of the traffic.

Is this possible? If so, how?

2 Answers2

3

If you have two web servers and you want to send 30% of the requests to server A and 70% of the requests to server B you can put a load balancer in front of the two web server. You have to put a load balancer in front of the two web servers anyway otherwise all traffic will only go to one server or the other.

Using weights you can specify how much traffic should go to the first or the second server. A simple example is given below:

http {
  upstream myapp1 {
    server srv1.example.com weight=3;
    server srv2.example.com weight=7;
  }
  server {
    listen 80;
    location / {
      proxy_pass http://myapp1;
    }
  }
}

EDIT: With both servers being the production server you can use the mirror module on srv1.example.com to send 30% of the traffic to your test server. Agreed, this is a bit of a dirty hack so feel free to down-vote when better solutions are given.

EDIT 2: assign the samen IP address to both DNS A records if you only have one server.

Tommiie
  • 5,547
  • 2
  • 11
  • 45
  • That's nice but doesn't your code split the traffic? I want to keep 100% of the traffic on the production server and replicate (mirror) only 30% of the traffic on the test server. I don't want to split the traffic. – smellyarmpits Nov 27 '18 at 10:47
  • Indeed it splits the traffic. I wasn't aware that the `mirror` module duplicates traffic. I've edited my answer a bit. – Tommiie Nov 27 '18 at 10:50
  • Yes, I want to decouple the test and the production server. The mirror module duplicates the requests and ignores the responses from the mirror target server. Maybe I can mirror and then split the traffic? What happens in nginx if you put just one server with a weight less than 100% in an upstream? upstream myapp1 { server srv1.example.com weight=3; } – smellyarmpits Nov 27 '18 at 10:55
  • Nginx adds the weights of all the servers in an upstream block together, and then divides your own weight by the sum. In my example: 3 / (3+7), and in your example 3 / 3 = all traffic. – Tommiie Nov 27 '18 at 10:56
  • mirror first, then split might work, but split and then mirror might be better. Though you will need to production servers in that case. – Tommiie Nov 27 '18 at 10:57
  • "split and then mirror might be better" yes, you are right, but i only have one production server at the moment, so it's not feasible – smellyarmpits Nov 27 '18 at 11:00
2
http {
  ...
  split_clients "${remote_addr}AAA" $mirror_allowed {
    30% 1;
    * "";
  }
}

server {
  ...
  location / {
    ...
    mirror /mirror;
  }

  location /mirror {
    internal;
    if ($mirror_allowed = "") { 
      return 200; 
    }
    proxy_pass https://some.mirror.endpoint$request_uri;
  }
}
foo.jikura
  • 21
  • 1