1. Home
  2. Docs
  3. 秒杀系统
  4. Nginx负载均衡

Nginx负载均衡

负载均衡是高可用网络基础架构的关键组件,通常用于将工作负载分布到多个服务器来提高网站、应用、数据库或其他服务的性能和可靠性

源地址哈希法、轮询法、随机法、加权轮询法、加权随机法、最小连接数法

nginx.conf

user www-data;
worker_processes 4;
pid /run/nginx.pid;
daemon off;

events {
  worker_connections  2048;
  multi_accept on;
  use epoll;
}

http {
  server_tokens off;
  sendfile on;
  tcp_nopush on;
  tcp_nodelay on;
  keepalive_timeout 20;

  client_header_buffer_size 5120k;
  large_client_header_buffers 16 5120k;

  types_hash_max_size 10240;
  client_max_body_size 100M;
  include /etc/nginx/mime.types;
  default_type application/octet-stream;
  access_log /dev/stdout;
  error_log /dev/stderr;
  gzip on;
  gzip_disable "msie6";

  ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  ssl_ciphers 'ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA:ECDHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA256:DHE-RSA-AES256-SHA:ECDHE-ECDSA-DES-CBC3-SHA:ECDHE-RSA-DES-CBC3-SHA:EDH-RSA-DES-CBC3-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:DES-CBC3-SHA:!DSS';

  include /etc/nginx/conf.d/*.conf;
  include /etc/nginx/sites-available/*.conf;
  open_file_cache off; # Disabled for issue 619
  charset UTF-8;
}

test.conf

upstream myproject{
    # ip_hash指令,将同一用户引入同一服务器。
    server 192.168.3.89:9501 max_fails=3 fail_timeout=5s weight=1;
    server 192.168.3.67:9501 max_fails=3 fail_timeout=5s weight=1;
    keepalive 256;
}

server {
    #root /var/www/ybsocket;
    server_name eseckill.test;

    # large_client_header_buffers 4 10M;

    location / {
        proxy_http_version 1.1;
        proxy_set_header Connection "Keep-Alive";
        proxy_set_header X-Real-IP $remote_addr;

        if (!-f $request_filename) {
             #proxy_pass http://php-fpm:9501;
             proxy_pass http://myproject;
        }
    }

}

关于出现110、502内部错误与 proxy_set_header Connection "Keep-Alive"; 有关系

Q:压力测试出现 failed (111: Connection refused)?

2020/09/24 11:29:18 [error] 7304#7304: *996894 connect() failed (111: Connection refused) while connecting to upstream, client: 192.168.3.114, server: eseckill.test, request: "GET /Api/Seckill/Order/testHttp HTTP/1.1", upstream: "http://192.168.3.132:9501/Api/Seckill/Order/testHttp", host: "eseckill.test"
2020/09/24 11:29:20 [error] 7307#7307: *1006701 connect() failed (111: Connection refused) while connecting to upstream, client: 192.168.3.114, server: eseckill.test, request: "GET /Api/Seckill/Order/testHttp HTTP/1.1", upstream: "http://192.168.3.143:9501/Api/Seckill/Order/testHttp", host: "eseckill.test"
2020/09/24 11:29:20 [error] 7307#7307: *1006707 connect() failed (111: Connection refused) while connecting to upstream, client: 192.168.3.114, server: eseckill.test, request: "GET /Api/Seckill/Order/testHttp HTTP/1.1", upstream: "http://192.168.3.132:9501/Api/Seckill/Order/testHttp", host: "eseckill.test"

相关资料

Nginx实现负载均衡记一次生产环境Nginx间歇性502的事故分析过程

为什么用 Nginx 做了负载均衡之后 QPS 还不如直接压测 Tomcat?压测引起的 nginx报错 502 no live upstreams while connecting to upstream解决

Tags
Was this article helpful to you? Yes No

How can we help?