nginx apache负载均衡测试

时间:2023-12-10 08:33:50

apache配置

(监听内网ip和端口)

Listen 10.163.170.8:8001
Listen 10.163.170.8:8002
Listen 10.163.170.8:8003 <VirtualHost *:8001>
DocumentRoot /website/111.com
ServerName localhost:8001
DirectoryIndex index.html index.php
ErrorLog logs/111.com-error_log
CustomLog logs/111.com-access_log common
</VirtualHost> <VirtualHost *:8002>
DocumentRoot /website/222.com
ServerName localhost:8002
DirectoryIndex index.html index.php
ErrorLog logs/222.com-error_log
CustomLog logs/222.com-access_log common
</VirtualHost> <VirtualHost *:8003>
DocumentRoot /website/333.com
ServerName localhost:8003
DirectoryIndex index.html index.php
ErrorLog logs/333.com-error_log
CustomLog logs/333.com-access_log common
</VirtualHost>

nginx配置

http {
include /etc/nginx/mime.types;
default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on;
#tcp_nopush on; #keepalive_timeout 0;
keepalive_timeout 65; #gzip on; # Load config files from the /etc/nginx/conf.d directory
# The default server is in conf.d/default.conf
# include /etc/nginx/conf.d/*.conf; upstream webservers {
server 10.163.170.8:8001 weight=1;
server 10.163.170.8:8002 weight=2;
server 10.163.170.8:8003 weight=3;
} server {
listen 80;
server_name localhost;
location / {
proxy_pass http://webservers;
proxy_set_header X-Real-IP $remote_addr;
}
} }

php测试

(nginx_upstream_test.php)

<?php
$url = 'http://114.215.107.17/';
$count = 100; $webservers = array();
for($i=1; $i<=$count; $i++){
echo($i."\n");
$data = test($url);
$webservers[$data]++;
}
var_dump($webservers); function test($url){
$data = file_get_contents($url);
return trim($data);
}
?>

测试结果1

array(3) {
[]=>
int(34)
[]=>
int(49)
[]=>
int(17)
}

测试结果2

array(3) {
[]=>
int(51)
[]=>
int(33)
[]=>
int(16)
}