How to replicate nginx data to two servers?
I'm trying to replicate the traffic that one specific nginx server receives to two servers. The goal is not to load-balance, but to replay the same input on all nginx servers.
An example: Nginx receives a HTTP POST. I want to send this same POST to other servers.
** UPDATE **
The situation is easy and non-complex. I just need to resend the POST data (or GET or any request data) to another server IP (it also running a nginx instance). Just it.
USER -> POST DATA -> NGINX INSTANCE ----REDIRECT ---> SERVER 1 AND SERVER 2
I was able to replicate using post_action state.
upstream main_upstream {
least_conn;
server 192.168.9.10:80;
keepalive 1024;
}
server {
listen 80;
server_name _;
client_body_buffer_size 1512k;
client_max_body_size 10m;
location /1/ {
fastcgi_pass main_upstream;
post_action @replayevent ;
}
# Send the post_action request to a FastCGI backend for logging.
location @replayevent {
fastcgi_pass 192.168.9.14:80;
}
Now it send data two servers.
If your upstream do not support fastcgi (happened in my case), replace with proxy_pass.
I don't believe you can do this with nginx by itself; a quick perusal of the relevant bits of the nginx documentation (upstream and proxy directives) doesn't suggest you can. As noted in comments, this also breaks HTTP, as there's no clarity on which of the two rear servers will respond.
One alternative is to use something like varnish and do a replay to the second rear server using varnishreplay:
https://www.varnish-cache.org/docs/2.1/reference/varnishreplay.html
I haven't used it, so I don't know if you can make it replay the traffic nearly simultaneously with the first rear server.
What you want to use is something like EM-Proxy[1]. It easily handles splitting http requests across any number of servers. It also correctly handles returning data from only the live server and blocking the others so the user doesn't get multiple responses.
[1] https://github.com/igrigorik/em-proxy/
Use central storage like an NFS server and each nginx web node mounts the NFS share (file-level). Or use a cluster file system like OCFS2 and each web node mounts the LUN/partition (block-level).