【问题标题】:How to test load balancing in nginx?如何在 nginx 中测试负载均衡?
【发布时间】:2019-10-16 15:47:06
【问题描述】:

我在 nginx 中完成了重定向配置,它成功运行。 但在那我想要负载平衡: - 为此,我已经创建了 load-balancer.conf 并将服务器名称添加到该文件中,例如:-

upstream backend {
  # ip_hash;

   server 1.2.3.4;
   server 5.6.7.8;
 }

server {
   listen 80;

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

在这两种情况下,我都做了相同的配置 并且它默认使用循环算法,因此在该请求中通过一台电脑传输到另一台电脑..... 但它不起作用

任何人都可以建议我任何 secong 请求转到另一台服务器 5.6.7.8

所以我可以检查负载平衡。

非常感谢。

【问题讨论】:

  • 您可以使用 Apache Jmeter 来测试负载平衡。它应该为您提供有关正在发生的事情和失败的更多信息。这里有教程:digitalocean.com/community/tutorials/…
  • @BrunoB.Carvalho 我已经在 jmeter 中对其进行了测试,但我希望第二个请求发送到 5.6.7.8 服务器

标签: nginx


【解决方案1】:

为上游创建日志文件以检查请求将发送到哪个服务器

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;
   }
}

然后检查您的日志文件 sudo cat /var/log/nginx/nginx-access.log;

你会看到类似的日志

to: 5.6.7.8:80 {GET /sites/default/files/abc.png HTTP/1.1} upstream_response_time 0.171 request_time 0.171

【讨论】:

    猜你喜欢
    • 2013-04-28
    • 1970-01-01
    • 1970-01-01
    • 2017-01-17
    • 1970-01-01
    • 1970-01-01
    • 2016-05-12
    • 2012-08-27
    • 2019-04-10
    相关资源
    最近更新 更多