nginx做负载均衡服务器

Linuxnetos

nginx做负载均衡服务器

准备3台机器
192.168.216.13  nginx
192.168.216.16  node1
192.168.216.17  node2
node1和node2做后端web服务器,nginx做负载均衡服务器

操作:
1.node1安装后端http服务
2.node2部署tomcat服务
3.nginx服务器配置负载均衡服务
# cat /usr/local/nginx/conf/nginx.conf

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log  warn;
#error_log  logs/error.log  info;

pid        /run/nginx.pid;

events {
    use epoll;
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    ssl_session_cache  shared:SSL:10m;
        proxy_buffer_size          512k;
        proxy_buffers            300  512k;
        proxy_busy_buffers_size    512k;
        proxy_temp_file_write_size 512k;
        proxy_connect_timeout 300s;
        proxy_send_timeout   600s;
        proxy_read_timeout   600s;
        send_timeout 600s;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" "$request_body" '
                      '"$http_user_agent" "$http_x_forwarded_for" "$http_cookie" ';

    access_log  /var/log/nginx/access.log  main;
    sendfile        on;
    keepalive_timeout  65;

    client_max_body_size 2048m; 
    client_body_buffer_size 500m;
    client_header_buffer_size 1024k;
    large_client_header_buffers 4 64k;
    include /usr/local/nginx/conf/conf.d/*.conf;

}

# mkdir /usr/local/nginx/conf/conf.d
# vim upstream.conf
upstream web {
   server 192.168.216.16:80 weight=1 max_fails=1 fail_timeout=30;
   server 192.168.216.17:8080 weight=2 max_fails=1 fail_timeout=30;
   server 192.168.216.18 down;  #后端服务器宕机
}
server {
	listen       80;
	server_name  localhost;
location / {
#	root  html;
	proxy_pass  http://web;
}
}

------------------------------------------------
2.设置相同客户端访问相同Web服务器
# vim upstream.conf
upstream web {
   ip_hash;
   server 192.168.216.16:80 weight=1 max_fails=1 fail_timeout=30;
   server 192.168.216.17:8080 weight=2 max_fails=1 fail_timeout=30;
}
server {
	listen       80;
	server_name  localhost;
location / {
#	root  html;
	proxy_pass  http://web;
}
}