Nginx+Keepalived主主负载均衡服务器

时间:2023-03-09 18:53:47
Nginx+Keepalived主主负载均衡服务器

Nginx+keepalived主主负载均衡服务器
测试实验环境:

主Nginx之一:192.168.11.27
主Nginx之二:192.168.11.28
Web服务器一:192.168.11.37
Web服务器二:192.168.11.38
VIP地址一:192.168.11.208
VIP地址二:192.168.11.209

环境:
以下环境均使用的是CentOS 5.7 x86_64位系统
[root@localhost ~]# lsb_release  -a
LSB
Version:   
:core-4.0-amd64:core-4.0-ia32:core-4.0-noarch:graphics-4.0-amd64:graphics-4.0-ia32:graphics-4.0-noarch:printing-4.0-amd64:printing-4.0-ia32:printing-4.0-noarch
Distributor ID:    CentOS
Description:    CentOS release 5.7 (Final)
Release:    5.7
Codename:    Final
[root@localhost ~]# uname  -a
Linux localhost 2.6.18-274.el5 #1 SMP Fri Jul 22 04:43:29 EDT 2011 x86_64 x86_64 x86_64 GNU/Linux

安装nginx负载均衡服务器

#添加运行nginx的用户和组www 
groupadd www  
useradd -g www www  
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-7.8.tar.gz 
tar zxvf pcre-7.8.tar.gz 
cd pcre-7.8/ 
./configure 
make && make install 
wget http://sysoev.ru/nginx/nginx-0.7.51.tar.gz 
tar zxvf nginx-0.7.51.tar.gz 
cd nginx-0.7.51/ 
./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module 
make && make install

配置nginx负载均衡器的配置文件vim /usr/local/nginx/conf/nginx.conf,此篇文章仅仅只是我的某项目的配置文档,纯80转发;
主Nginx之一:192.168.11.27
主Nginx之二:192.168.11.28  均需要安装,相同即可
[root@localhost conf]# vim /usr/local/nginx/conf/nginx.conf

user www www;
worker_processes ;
pid /usr/local/nginx/logs/nginx.pid;
worker_rlimit_nofile ;
events
{
use epoll;
worker_connections ;
}
http{
include mime.types;
default_type application/octet-stream;
server_names_hash_bucket_size ;
client_header_buffer_size 32k;
large_client_header_buffers 32k;
client_max_body_size 8m;
sendfile on;
tcp_nopush on;
keepalive_timeout ;
tcp_nodelay on;
fastcgi_connect_timeout ;
fastcgi_send_timeout ;
fastcgi_read_timeout ;
fastcgi_buffer_size 64k;
fastcgi_buffers 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
gzip on;
gzip_min_length 1k;
gzip_buffers 16k;
gzip_http_version 1.0;
gzip_comp_level ;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on; upstream backend
{
ip_hash;
server 192.168.11.37:;
server 192.168.11.38:;
}
server {
listen ;
server_name www.linuxidc.com;
location / {
root /var/www/html ;
index index.php index.htm index.html;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://backend;
} location /nginx {
access_log off;
auth_basic "NginxStatus";
#auth_basic_user_file /usr/local/nginx/htpasswd;
} log_format access '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for';
access_log /usr/local/nginx/logs/access.log access;
}
}

linuxidc配置文件  /usr/local/nginx/sbin/nginx -t
启动 /usr/local/nginx/sbin/nginx