Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test load balancing in nginx?

Tags:

nginx

I done congfiguration in nginx for redirection and it works successfully. But in that i want load balancing :- for that i already create load-balancer.conf as well as give server name into that file like :-

upstream backend {
  # ip_hash;

   server 1.2.3.4;
   server 5.6.7.8;
 }

server {
   listen 80;

   location / {
      proxy_pass http://backend;
   }
}

In both instances i did same configuration and it default uses round-robin algorithm so in that request transfer via one pc to another pc..... but it were not working

can any one suggest me anything that secong request going to another server 5.6.7.8

so i can check load balancing.

Thankyou so much.

like image 513
Vk Suhagiya Avatar asked Nov 29 '16 06:11

Vk Suhagiya


People also ask

How do I know if Nginx load balancing is working?

To test the Nginx load balancing, open a web browser and use the following address to navigate. Once the website interface loads, take note of the application instance that has loaded. Then continuously refresh the page. At some point, the app should be loaded from the second server indicating load balancing.

Can Nginx do load balancing?

It is possible to use nginx as a very efficient HTTP load balancer to distribute traffic to several application servers and to improve performance, scalability and reliability of web applications with nginx.

How load balancer works in Nginx?

A load balancer acts as the “traffic cop” sitting in front of your servers and routing client requests across all servers capable of fulfilling those requests in a manner that maximizes speed and capacity utilization and ensures that no one server is overworked, which could degrade performance.


1 Answers

Create a log file for upstream to check request is going to which server

http {
   log_format upstreamlog '$server_name to: $upstream_addr {$request} '
   'upstream_response_time $upstream_response_time'
   ' request_time $request_time';

upstream backend {
  # ip_hash;

   server 1.2.3.4;
   server 5.6.7.8;
 }

server {
   listen 80;
   access_log /var/log/nginx/nginx-access.log upstreamlog;
   location / {
      proxy_pass http://backend;
   }
}

and then check your log file sudo cat /var/log/nginx/nginx-access.log;

you will see log like

to: 5.6.7.8:80 {GET /sites/default/files/abc.png HTTP/1.1} upstream_response_time 0.171 request_time 0.171
like image 82
Bhupendra Avatar answered Nov 15 '22 02:11

Bhupendra