详解Linux系统配置nginx的负载均衡

详解Linux系统配置nginx的负载均衡

负载均衡的几种方式:

1.轮询:默认按照时间顺序对所有服务器一个一个的访问,如果有服务器宕机,会自动剔除;

2.weight:服务器的方位几率和weight成正比,这个可以在服务器配置不均的时候进行配置;

3.ip_hash:对每个请求的ip进行hash计算,并按照一定的规则分配对应的服务器(可解决session共享);

4.fair:按照每台服务器的响应时间(rt)来分配请求,rt知道优先分配;

5.url_hash:按照访问url的hash值来分配请求;

NGINX配置文件:

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

events {
  worker_connections 1024;
}

http {
  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;
  tcp_nodelay     on;
  keepalive_timeout  65;
  types_hash_max_size 2048;
  
  
  include       blockip.conf;    #过虑IP
  include       /etc/nginx/mime.types;
  default_type    application/octet-stream;

  # Load modular configuration files from the /etc/nginx/conf.d directory.
  # See http://nginx.org/en/docs/ngx_core_module.html#include
  # for more information.
  include /etc/nginx/conf.d/*.conf;

  upstream www.域名.com {
  server IP1:端口;            #服务器1
  server IP2:端口;            #服务器2
  ip_hash;               #负载均衡的规则
  }

  server {
    listen    80 default_server;   #NGINX 指向的服务域名
    listen    [::]:80 default_server;
    server_name IP;           #NGINX 指向的服务器 IP
    root     /usr/share/nginx/html; #NGINX HTML 目录

    # Load configuration files for the default server block.
    include /etc/nginx/default.d/*.conf;

    location / {
      proxy_pass  http://www.域名.com; #nginx指向的域名
   }

    error_page 404 /404.html;
      location = /40x.html {      #404页面
    }

    error_page 500 502 503 504 /500.html;
      location = /500.html {      #500页面
    }
  }
}

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

你可能感兴趣的:(详解Linux系统配置nginx的负载均衡)