Nginx负载均衡实现:
需求:两台Web :192.168.1.2/192.168.1.3,nginx负载均衡服务器192.168.1.10
Nginx服务器上实现web负载。
配置步骤:
1.安装Nginx
(1)安装依赖包
yum install -y pcre pcre-devel openssl openssl-devel gcc gcc gcc-c++ ncurses-devel perl
useradd www -M -s /sbin/nologin --创建用户
(2)编译安装Nginx
wget http://nginx.org/download/nginx-1.8.0.tar.gz --下载Nginx软件包
tar -zxvf nginx-1.8.0.tar.gz
vim auto/cc/gcc --取消Debug编译模式,179行
#CFLAGS="$CFLAGS -g" (加上注释)
cd nginx-1.8.0
./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_stub_status_module --with-http_ssl_module
make && make install --编译安装
(3)nginx启动
cd /usr/local/nginx
/usr/local/nginx/sbin/nginx --启动Nginx
/usr/local/nginx/sbin/Nginx -s reload/stop --关闭、重启
2.负载均衡配置
#vi /usr/local/nginx/nginx.conf
sendfile on;
keepalive_timeout 65; --这两行下面添加下列四行内容
upstream myserv {
server 192.168.1.2:80 max_fails=3 fail_timeout=3s weight=1;
server 192.168.1.3:8090 max_fails=3 fail_timeout=3s weight=2;
}
server {
listen 80;
server_name www1.lansaipu.com;
location / {
root html;
index index.html index.htm;
proxy_pass http://myserv; --添加这一行
}
总共修改5行,共两处
3.重启Nginx:
/usr/local/nginx/sbin/nginx -s reload //测试是否配置成功。安装完成
4.负载均衡实现https转发
server {
listen 80;
server_name www1.lansaipu.com;
location / {
root html;
index index.html index.htm;
proxy_pass http://myserv; --添加这一行
}
改为:
server {
listen 443 ssl;
server_name localhost;
ssl_certificate ca.crt;
ssl_certificate_key ca.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
root html;
proxy_pass http://myserv/;
index index.html index.htm;
}
}
即可