How to build a multi-person test environment by using Nginx reverse proxy and load balancing

09-12-2023

Realization principle

When we visit a website, there will be a user-agent header in the request header, such as mozilla/5.0 (macintosh;  intel mac os x 10.12; RV: 50.0) Gecko/2010101 Firefox/50.0, this ua can be customized, and many browser plug-ins also support adding custom ua, such as the user agent swicher plug-in of Firefox.

As shown in the figure, a custom ua has been added here, and the content has been changed to its own name. Through this ua request website, we can see that ua has become our own in the request header, which is a logo.

According to this ua, judging by nginx, different UAs are forwarded to different ports, and each port corresponds to a website directory.

Nginx judges ua

Nginx can be used to get the ua of each request, that is, the $http_user_agent variable.

For example, in the location domain, you can judge ua and set an identifier as follows:

if ( $http_user_agent ~ "dashen" ) { set $flag "01"; }

For example, judging that ua content is dashen and setting $flag to 01, you can set many such flags.

Nginx reverse proxy and load balancing

By judging different UAs, we can forward them to different machines and ports through the reverse proxy. Here, the same testing machine can forward to different ports of this machine, listen to different ports and set different website directories.

The details are as follows:

server{ listen 192.168.1.251:80;  server_name *.example.com;  index  index.html  index.htm  index.php ;  charset utf-8;  location / { set $flag "00";  if ( $http_user_agent ~ "dashen" ) { set $flag "01";  } if ( $http_user_agent ~ "mianwo" ) { set $flag "02";  } if ( $http_user_agent ~ "bingkuai" ) { set $flag "03";  } if ( $http_user_agent ~ "hadoop" ) { set $flag "04";  } proxy_set_header host $host;  proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for;  if ( $flag = "00" ){ add_header z-server mobile;  proxy_pass http://webserver_mobile;  } if ( $flag = "01" ){ add_header z-server dashen;  proxy_pass http://webserver_dashen;  } if ( $flag = "02" ){ add_header z-server mianwo;  proxy_pass http://webserver_mianwo;  } if ( $flag = "03" ){ add_header z-server bingkuai;  proxy_pass http://webserver_bingkuai;  } if ( $flag = "04" ){ add_header z-server hadoop;  proxy_pass http://webserver_hadoop; } }}

This configuration will reverse proxy different ua requests to different load balancing servers. See the specific load balancing configuration below.

Nginx load balancing configuration

Several load balancing configurations are defined here, and each load balancing configuration is actually equipped with only one machine, that is, different ports of the machine.

upstream webserver_mobile{ server 127.0.0.1:8900 max_fails=2 weight=2 fail_timeout=10s; }upstream webserver_dashen{ server 127.0.0.1:8901 max_fails=2 weight=2 fail_timeout=10s; }upstream webserver_mianwo{ server 127.0.0.1:8902 max_fails=2 weight=2 fail_timeout=10s; }upstream webserver_bingkuai{ server 127.0.0.1:8903 max_fails=2 weight=2 fail_timeout=10s; }upstream webserver_hadoop{ server 127.0.0.1:8904 max_fails=2 weight=2 fail_timeout=10s; }

So, it's not over yet. What needs to be done next is to configure multiple nginx virtual hosts like a normal website. The difference is that you need to listen to several different ports here, so you need several virtual host configurations, but the website directory is different, such as ua is dashen, and the corresponding website directory is /vhosts/example.com/dashen, and ua is mianwo, and the corresponding website directory is/vhosts/.

In this way, different testers pull down branches from the website directory corresponding to their own ua, and configure their own ua through browser plug-ins, so that many people can pull different branches on one machine at the same time, and access the same domain name, and realize the separation of website root directories according to different ua, and they do not affect each other.

Copyright Description:No reproduction without permission。

Knowledge sharing community for developers。

Let more developers benefit from it。

Help developers share knowledge through the Internet。

Follow us