为了简单就在windows 上介绍一下了

nginx

steam 模块 转发tcp 对socket做负载均衡

配置如下:

worker_processes  1;
error_log  logs/error.log;
error_log  logs/error.log  notice;
error_log  logs/error.log  info;
pid        logs/nginx.pid;
events {
    worker_connections  1024;
}
stream{
    log_format proxy '$remote_addr [$time_local] '
                 '$protocol $status $bytes_sent $bytes_received '
                 '$session_time "$upstream_addr" '
                 '"$upstream_bytes_sent" "$upstream_bytes_received" "$upstream_connect_time"';
    open_log_file_cache off;    
    access_log logs/tcp-access.log proxy ;
    upstream test{
        server xxx.xxx.xxx.xxx:443  weight=1 max_fails=1 fail_timeout=30s;
        server xxx.xxx.xxx.xxx:443 weight=1 max_fails=1 fail_timeout=30s;
    }
    server{
        listen 4438;
        proxy_pass test;
        proxy_connect_timeout 8s;
        proxy_timeout 7d;
    }
}

upstream 模块 转发web socket做负载均衡

配置如下:

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    access_log  logs/access.log  main;
    sendfile        on;
    keepalive_timeout  65;
    upstream wsbackend {
        server 127.0.0.1:9123;
    }
    upstream  service {      
      server 127.0.0.1:8080 weight=1 max_fails=3 fail_timeout=30s; 
    } 
    server {
        listen       443 ssl;
        server_name  localhost;

        ssl_certificate      ../ssl/icetech.com.cn.pem;
        ssl_certificate_key  ../ssl/icetech.com.cn.key;

        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;

        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;
        location /service { 
              proxy_pass http://service;# 反向代理         
              proxy_set_header  X-Real-IP $remote_addr;
              proxy_next_upstream http_502 http_504 error timeout invalid_header;
        } 
        location / {
               proxy_pass http://wsbackend;
               proxy_http_version 1.1;
               proxy_set_header Upgrade $http_upgrade;
               proxy_set_header Connection "upgrade";       
        }
    }
}

常用的配置

#user  nobody;       #用户
worker_processes  1; #工作进程,根据硬件调整,大于等于CPU核数

#error_log  logs/error.log;  #错误日志配置
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid; #pid放置位置

events {
    #use epoll;  

    #使用epoll的I/O 模型

    # 补充说明:

    #与apache相类,nginx针对不同的操作系统,有不同的事件模型

    #A)标准事件模型

    #Select、poll属于标准事件模型,如果当前系统不存在更有效的方法,nginx会选择select或poll

    #B)高效事件模型

    #Kqueue:使用于FreeBSD 4.1+, OpenBSD 2.9+, NetBSD 2.0 和 MacOS X.使用双处理器的MacOS #X系统使用kqueue可能会造成内核崩溃。

    #Epoll:使用于Linux内核2.6版本及以后的系统。

    #/dev/poll:使用于Solaris 7 11/99+, HP/UX 11.22+ (eventport), IRIX 6.5.15+ 和 Tru64 UNIX 5.1A+。

    #Eventport:使用于Solaris 10. 为了防止出现内核崩溃的问题, 有必要安装安全补丁  

    worker_connections  1024;

    #工作进程的最大连接数量,根据硬件调整,和前面工作进程配合起来用,尽量大,但是别把cpu跑到100%就行

    #每个进程允许的最多连接数, 理论上每台nginx服务器的最大连接数为worker_processes*worker_connections

    keepalive_timeout 60; 
}

#设定http服务器,利用它的反向代理功能提供负载均衡支持
http {
    include       mime.types;
    default_type  application/octet-stream;  

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    #配置两台Tomcat真实服务器
    upstream  B2C_WEB {      
      #ip_hash;     #目的保证客户端的ip地址不变,请求就只送到固定的一个Tomcat处理

      server 192.168.126.1:8080 weight=1 max_fails=3 fail_timeout=30s;       
      server 192.168.126.15:8080 weight=1 max_fails=3 fail_timeout=30s;

      #weight=1表示
      #这表示,如果服务器192.168.126.1 | 15在30秒内出现了3次错误,
      #那么就认为这个服务器工作不正常,从而在接下来的30秒内nginx不再去访问这个服务器。
    }  

    server {
        listen       80; #监听端口
        server_name  localhost;
        charset UTF-8;      
        index index.html index.htm index.jsp index.do;
        root E:\\JavaEE\\Apache-Tomcat-7.0.41\\wtpwebapps; #WEB服务的主目录

        location ~ ^/(WEB-INF)/ { 
          deny all; #禁止访问WEB-INF目录
        }           

        # 动态数据由代理服务器Tomcat处理
        location ~ .*\.(jsp|jspx|do)?$ { 
          proxy_pass http://B2C;# 反向代理         
          proxy_set_header  X-Real-IP $remote_addr;
          proxy_next_upstream http_502 http_504 error timeout invalid_header;
       }    

       # 静态数据直接读取不经过Tomcat服务器
       location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
       {        
         expires      30d; #在客户端保存时间
       }
       location ~ .*\.(js|css)?$
       {        
         expires      1h;
       }

       # 监控Nginx服务状态
       location /Nginxstatus {
         stub_status on;
         access_log   off;
       }        

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

    }

}

什么是nginx
Nginx是一款高性能的http 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器。由俄罗斯的程序设计师Igor Sysoev所开发,官方测试nginx能够支支撑5万并发链接,并且cpu、内存等资源消耗却非常低,运行非常稳定。

应用场景
1、http服务器。Nginx是一个http服务可以独立提供http服务。可以做网页静态服务器。
2、虚拟主机。可以实现在一台服务器虚拟出多个网站。例如个人网站使用的虚拟主机。
3、反向代理,负载均衡。当网站的访问量达到一定程度后,单台服务器不能满足用户的请求时,需要用多台服务器集群可以使用nginx做反向代理。并且多台服务器可以平均分担负载,不会因为某台服务器负载高宕机而某台服务器闲置的情况。

nginx安装
下载nginx:

官方网站:
http://nginx.org/

要求的安装环境
1、需要安装gcc的环境。yum install gcc-c++
2、第三方的开发包。
 PCRE
PCRE(Perl Compatible Regular Expressions)是一个Perl库,包括 perl 兼容的正则表达式库。nginx的http模块使用pcre来解析正则表达式,所以需要在linux上安装pcre库。
yum install -y pcre pcre-devel
注:pcre-devel是使用pcre开发的一个二次开发库。nginx也需要此库。
 zlib
zlib库提供了很多种压缩和解压缩的方式,nginx使用zlib对http包的内容进行gzip,所以需要在linux上安装zlib库。
yum install -y zlib zlib-devel

   openssl
    OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及SSL协议,并提供丰富的应用程序供测试或其它目的使用。
    nginx不仅支持http协议,还支持https(即在ssl协议上传输http),所以需要在linux安装openssl库。
yum install -y openssl openssl-devel

安装步骤

第一步:把nginx的源码包上传到linux系统
第二步:解压缩
[root@localhost ~]# tar zxf nginx-1.8.0.tar.gz 
第三步:使用configure命令创建一makeFile文件。
./configure \
--prefix=/usr/local/nginx \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi
注意:启动nginx之前,上边将临时文件目录指定为/var/temp/nginx,需要在/var下创建temp及nginx目录
[root@localhost sbin]# mkdir /var/temp/nginx/client -p
第四步:make
第五步:make install

启动nginx
进入sbin目录
[root@localhost sbin]# ./nginx
关闭nginx:
[root@localhost sbin]# ./nginx -s stop
推荐使用:
[root@localhost sbin]# ./nginx -s quit

重启nginx:
1、先关闭后启动。
2、刷新配置文件:
[root@localhost sbin]# ./nginx -s reload

配置虚拟主机
就是在一台服务器启动多个网站。
如何区分不同的网站:
1、域名不同
2、端口不同

通过端口区分不同虚拟机
Nginx的配置文件:
/usr/local/nginx/conf/nginx.conf

#user  nobody;
    worker_processes  1;

    #error_log  logs/error.log;
    #error_log  logs/error.log  notice;
    #error_log  logs/error.log  info;

    #pid        logs/nginx.pid;

    events {
        worker_connections  1024;
    }

    http {
        include       mime.types;
        default_type  application/octet-stream;

        #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  logs/access.log  main;

        sendfile        on;
        #tcp_nopush     on;

        #keepalive_timeout  0;
        keepalive_timeout  65;

        #gzip  on;
        #一个server节点就是一个虚拟主机 可以配置多个server,配置了多个虚拟主机。
        server {
            listen       80;
            server_name  localhost;

            #charset koi8-r;

            #access_log  logs/host.access.log  main;

            location / {
                #Html是nginx安装目录下的html目录
                root   html; 
                index  index.html index.htm;
            }
        }
    }
添加虚拟主机:

    #user  nobody;
    worker_processes  1;

    #error_log  logs/error.log;
    #error_log  logs/error.log  notice;
    #error_log  logs/error.log  info;

    #pid        logs/nginx.pid;

    events {
        worker_connections  1024;
    }

    http {
        include       mime.types;
        default_type  application/octet-stream;

        #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  logs/access.log  main;

        sendfile        on;
        #tcp_nopush     on;

        #keepalive_timeout  0;
        keepalive_timeout  65;

        #gzip  on;

        server {
            listen       80;
            server_name  localhost;

            #charset koi8-r;

            #access_log  logs/host.access.log  main;

            location / {
                root   html;
                index  index.html index.htm;
            }
        }
        server {
            listen       81;
            server_name  localhost;

            #charset koi8-r;

            #access_log  logs/host.access.log  main;

            location / {
                root   html-81;
                index  index.html index.htm;
            }
        }
    }

通过域名区分虚拟主机
什么是域名
域名就是网站。
www.baidu.com
www.taobao.com
www.jd.com
Tcp/ip

Dns服务器:把域名解析为ip地址。保存的就是域名和ip的映射关系。
    一级域名:
    Baidu.com
    Taobao.com
    Jd.com
    二级域名:
    www.baidu.com
    Image.baidu.com
    Item.baidu.com
    三级域名:
    1.Image.baidu.com
    Aaa.image.baidu.com

    一个域名对应一个ip地址,一个ip地址可以被多个域名绑定。
    本地测试可以修改hosts文件。
    修改window的hosts文件:(C:\Windows\System32\drivers\etc)
    可以配置域名和ip的映射关系,如果hosts文件中配置了域名和ip的对应关系,不需要走dns服务器。
Nginx的配置

    #user  nobody;
    worker_processes  1;

    #error_log  logs/error.log;
    #error_log  logs/error.log  notice;
    #error_log  logs/error.log  info;

    #pid        logs/nginx.pid;

    events {
        worker_connections  1024;
    }

    http {
        include       mime.types;
        default_type  application/octet-stream;

        #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  logs/access.log  main;

        sendfile        on;
        #tcp_nopush     on;

        #keepalive_timeout  0;
        keepalive_timeout  65;

        #gzip  on;

        server {
            listen       80;
            server_name  localhost;

            #charset koi8-r;

            #access_log  logs/host.access.log  main;

            location / {
                root   html;
                index  index.html index.htm;
            }
        }
        server {
            listen       81;
            server_name  localhost;

            #charset koi8-r;

            #access_log  logs/host.access.log  main;

            location / {
                root   html-81;
                index  index.html index.htm;
            }
        }
        server {
            listen       80;
            server_name  www.taobao.com;

            #charset koi8-r;

            #access_log  logs/host.access.log  main;

            location / {
                root   html-taobao;
                index  index.html index.htm;
            }
        }
        server {
            listen       80;
            server_name  www.baidu.com;

            #charset koi8-r;

            #access_log  logs/host.access.log  main;

            location / {
                root   html-baidu;
                index  index.html index.htm;
            }
        }
    }

    域名的配置:
        192.168.25.148 www.taobao.com
        192.168.25.148 www.baidu.com

反向代理
反向代理服务器决定哪台服务器提供服务 返回代理服务器不提供服务器。也是请求的转发。

Nginx实现反向代理
两个域名指向同一台nginx服务器,用户访问不同的域名显示不同的网页内容。
两个域名是www.sian.com.cn和www.sohu.com
nginx服务器使用虚拟机192.168.101.3

第一步:安装两个tomcat,分别运行在8080和8081端口。
第二步:启动两个tomcat。
第三步:反向代理服务器的配置

upstream tomcat1 {
server 192.168.25.148:8080;
}
server {
listen 80;
server_name www.sina.com.cn;
#charset koi8-r;

            #access_log  logs/host.access.log  main;

            location / {
                proxy_pass   http://tomcat1;
                index  index.html index.htm;
            }
        }
        upstream tomcat2 {
        server 192.168.25.148:8081;
        }
        server {
            listen       80;
            server_name  www.sohu.com;

            #charset koi8-r;

            #access_log  logs/host.access.log  main;

            location / {
                proxy_pass   http://tomcat2;
                index  index.html index.htm;
            }
        }

第四步:nginx重新加载配置文件
第五步:配置域名
在hosts文件中添加域名和ip的映射关系

192.168.25.148 www.sina.com.cn
192.168.25.148 www.sohu.com负载均衡
如果一个服务由多条服务器提供,需要把负载分配到不同的服务器处理,需要负载均衡。
upstream tomcat2 {
server 192.168.25.148:8081;
server 192.168.25.148:8082;
}
可以根据服务器的实际情况调整服务器权重。权重越高分配的请求越多,权重越低,请求越少。默认是都是1
upstream tomcat2 {
server 192.168.25.148:8081;
server 192.168.25.148:8082 weight=2;
}

Nginx的高可用
要实现nginx的高可用,需要实现备份机。

什么是负载均衡高可用
    nginx作为负载均衡器,所有请求都到了nginx,可见nginx处于非常重点的位置,如果nginx服务器宕机后端web服务将无法提供服务,影响严重。
    为了屏蔽负载均衡服务器的宕机,需要建立一个备份机。主服务器和备份机上都运行高可用(High Availability)监控程序,通过传送诸如“I am alive”这样的信息来监控对方的运行状况。当备份机不能在一定的时间内收到这样的信息时,它就接管主服务器的服务IP并继续提供负载均衡服务;当备份管理器又从主管理器收到“I am alive”这样的信息时,它就释放服务IP地址,这样的主服务器就开始再次提供负载均衡服务。

keepalived+nginx实现主备

什么是keepalived   
    keepalived是集群管理中保证集群高可用的一个服务软件,用来防止单点故障。
    Keepalived的作用是检测web服务器的状态,如果有一台web服务器死机,或工作出现故障,Keepalived将检测到,并将有故障的web服务器从系统中剔除,当web服务器工作正常后Keepalived自动将web服务器加入到服务器群中,这些工作全部自动完成,不需要人工干涉,需要人工做的只是修复故障的web服务器。

keepalived工作原理
    keepalived是以VRRP协议为实现基础的,VRRP全称Virtual Router Redundancy Protocol,即虚拟路由冗余协议。
    虚拟路由冗余协议,可以认为是实现路由器高可用的协议,即将N台提供相同功能的路由器组成一个路由器组,这个组里面有一个master和多个backup,master上面有一个对外提供服务的vip(VIP = Virtual IP Address,虚拟IP地址,该路由器所在局域网内其他机器的默认路由为该vip),master会发组播,当backup收不到VRRP包时就认为master宕掉了,这时就需要根据VRRP的优先级来选举一个backup当master。这样的话就可以保证路由器的高可用了。
    keepalived主要有三个模块,分别是core、check和VRRP。core模块为keepalived的核心,负责主进程的启动、维护以及全局配置文件的加载和解析。check负责健康检查,包括常见的各种检查方式。VRRP模块是来实现VRRP协议的。 

keepalived+nginx实现主备过程  
    初始状态:
                                        user
                                            |(0)
                                            |
                                        vip 192.168.101.100
                                        /
                                    /   
                                /(1)        
                            /           
                        /                   
                    /
        nginx                                                       nginx
            负载均衡服务器(主)                                          负载均衡服务器(备)
            192.168.101.3                <--心跳-->                       192.168.101.3  
            keepalived                                                  keepalived
                    \
                        \
                            \   (2)
                                \       
                                    \
                                        tomcat 集群

            0 用户访问虚拟ip
            1 导入nginx 主
            2 nginx 导入tomcat集群

    主机宕机:                                   

                                        user
                                            |(2)
                                            |
                                        vip 192.168.101.100
                                                \           
                                                    \
                        (1)                             \(3)
                                                            \
                                                                \
                                                                    \
        nginx                                                       nginx
            负载均衡服务器(主)                                          负载均衡服务器(备)
            192.168.101.3          (0)   <--心跳-->                       192.168.101.3  
            keepalived                                                  keepalived
                                                                            /
                                                                        /
                                                                    /   
                                                                /(4)
                                                            /
                                           tomcat 集群

            0 心跳检查 主挂了
            1 切换到备线路
            2 用户访问虚拟ip
            3 导入nginx 备
            4 nginx 导入tomcat集群

    主机恢复:
                                        user
                                            |(0)
                                            |
                                        vip 192.168.101.100
                                        /
                                    /   
                                /(1)        
                            /           
                        /                   
                    /
        nginx                                                       nginx
            负载均衡服务器(主)                                          负载均衡服务器(备)
            192.168.101.3                <--心跳-->                       192.168.101.3  
            keepalived                                                  keepalived
                    \
                        \
                            \   (2)
                                \       
                                    \
                                        tomcat 集群

            0 心跳检查 主活了
            1 切换到主线路
            2 用户访问虚拟ip
            3 导入nginx 备
            4 nginx 导入tomcat集群

高可用环境
    两台nginx,一主一备:192.168.101.3和192.168.101.4
    两台tomcat服务器:192.168.101.5、192.168.101.6

安装keepalived
    分别在主备nginx上安装keepalived,参考keepalived.txt进行安装:

https://blog.51cto.com/lmdtx/2156654