nginx在centos & ubuntu上的安装

安装Centos

添加当前账号加入sudoers,具备sudo功能

安装编辑器vim

Yum install vim

Su root

Cd cp /etc/sudoers /etc/sudoers[date]

Chmod 777 /etc/sudoers

编辑文本

Vim /etc/sudoers

添加 [当前账号] All=(ALL) ALL

:wq 保存

Chmod 420 /etc/sudoers

安装nginx和stream模块

http://nginx.com/products/application-load-balancing/

直接用Centos环境测试

一. gcc 安装

Sudo yum install gcc-c++

二. PCRE pcre-devel 安装

Sudo yum install -y pcre pcre-devel

三. zlib 安装

Sudo yum install -y zlib zlib-devel

四. OpenSSL 安装

Sudo yum install -y openssl openssl-devel

http://hg.nginx.org/nginx/ 自己找相关的平台和版本

sudo wget http://nginx.org/download/nginx-1.12.0.tar.gz

sudo tar zxvf ./nginx-1.12.0.tar.gz

sudo cd nginx-1.12.0

/configure --with-stream

1、gcc、g++依赖库:sudo apt-get install build-essential,sudoapt-get install libtool

2、安装 pcre依赖库:sudo apt-get install libpcre3 libpcre3-dev

3、安装 zlib依赖库:sudo apt-get install zlib1g-dev

4、安装ssl依赖库:sudo apt-get install openssl

5、安装nginx:sudo apt-get install nginx

【Tips: 】.其他插件(含http NLB)

./configure --with-stream --with-http_stub_status_module --with-http_sub_module --prefix=/usr/local/nginx --with-debug 

./configure --with-stream --with-http_stub_status_module --with-http_sub_module --prefix=/data/nginx --with-debug --with-pcre=/data/pcre-8.40 --with-openssl=/usr/lib64/openssl

sudo make

sudo make install

【TIP2】:设置网络

修改/etc/network/interfacs

sudo nano(gedit) /etc/network/interfaces

确认里面

auto eth{0}

iface eth{0} inet DHCP

重启网卡:

sudo service networking restart

【tips】

开机自启动

即在rc.local增加启动代码就可以了。

vi /etc/rc.local

增加一行 /usr/local/nginx/sbin/nginx

设置执行权限:

chmod755 rc.local

修改 nginx.conf文件中的参数变量

#--在events

events {ls

worker_connections 1024;

}

#--之后添加

stream{

upstream 1666port{

hash $remote_addr consistent;

server 10.63.201.106:1666 max_fails=3 fail_timeout=3s;

server 10.63.201.141:1666 max_fails=3 fail_timeout=3s;

}

upstream 1888port{

hash $remote_addr consistent;

server 10.63.201.106:1888 max_fails=3 fail_timeout=3s;

server 10.63.201.141:1888 max_fails=3 fail_timeout=3s;

}

upstream 9396port{

hash $remote_addr consistent;

server 10.63.201.106:9396 max_fails=3 fail_timeout=3s;

server 10.63.201.141:9396 max_fails=3 fail_timeout=3s;

}

upstream 2999port{

hash $remote_addr consistent;

server 10.63.201.106:2999 max_fails=3 fail_timeout=3s;

server 10.63.201.141:2999 max_fails=3 fail_timeout=3s;

}

upstream 2000port{

hash $remote_addr consistent;

server 10.63.201.106:2000 max_fails=3 fail_timeout=3s;

server 10.63.201.141:2000 max_fails=3 fail_timeout=3s;

}

server {

listen 1666;

proxy_connect_timeout 1s;

proxy_timeout 3s;

proxy_pass 1666port;

}

server {

listen 1888;

proxy_connect_timeout 1s;

proxy_timeout 3s;

proxy_pass 1888port;

}

server {

listen 9396;

proxy_connect_timeout 1s;

proxy_timeout 3s;

proxy_pass 9396port;

}

server {

listen 2999;

proxy_connect_timeout 1s;

proxy_timeout 3s;

proxy_pass 2999port;

}

server {

listen 2000;

proxy_connect_timeout 1s;

proxy_timeout 3s;

proxy_pass 2000port;

}

}

------------------

安装ubuntu

开启root账号

sudo passwd root

安装nginx和stream模块

http://nginx.com/products/application-load-balancing/

直接用ubuntu环境测试

sudo apt-get install libpcre3 libpcre3-dev build-essential

http://hg.nginx.org/nginx/ 自己找相关的平台和版本

sudo wget http://nginx.org/download/nginx-1.12.0.tar.gz

sudo tar zxvf ./nginx-1.12.0.tar.gz

sudo cd nginx-1.12.0

/configure --with-stream

1、gcc、g++依赖库:sudo apt-get install build-essential,sudoapt-get install libtool

2、安装 pcre依赖库:sudo apt-get install libpcre3 libpcre3-dev

3、安装 zlib依赖库:sudo apt-get install zlib1g-dev

4、安装ssl依赖库:sudo apt-get install openssl

5、安装nginx:sudo apt-get install nginx

【Tips: 】.其他插件(含http NLB)

./configure --with-stream --with-http_stub_status_module --with-http_sub_module --prefix=/usr/local/nginx --with-debug 

sudo make

sudo make install

【TIP2】:设置网络

修改/etc/network/interfacs

sudo nano(gedit) /etc/network/interfaces

确认里面

auto eth{0}

iface eth{0} inet DHCP

重启网卡:

sudo service networking restart

修改 nginx.conf文件中的参数变量

#--在events

events {

worker_connections 1024;

}

#--之后添加

stream{

upstream 1666port{

hash $remote_addr consistent;

server 10.63.201.106:1666 max_fails=3 fail_timeout=3s;

server 10.63.201.141:1666 max_fails=3 fail_timeout=3s;

}

upstream 1888port{

hash $remote_addr consistent;

server 10.63.201.106:1888 max_fails=3 fail_timeout=3s;

server 10.63.201.141:1888 max_fails=3 fail_timeout=3s;

}

upstream 9396port{

hash $remote_addr consistent;

server 10.63.201.106:9396 max_fails=3 fail_timeout=3s;

server 10.63.201.141:9396 max_fails=3 fail_timeout=3s;

}

upstream 2999port{

hash $remote_addr consistent;

server 10.63.201.106:2999 max_fails=3 fail_timeout=3s;

server 10.63.201.141:2999 max_fails=3 fail_timeout=3s;

}

upstream 2000port{

hash $remote_addr consistent;

server 10.63.201.106:2000 max_fails=3 fail_timeout=3s;

server 10.63.201.141:2000 max_fails=3 fail_timeout=3s;

}

server {

listen 1666;

proxy_connect_timeout 1s;

proxy_timeout 3s;

proxy_pass 1666port;

}

server {

listen 1888;

proxy_connect_timeout 1s;

proxy_timeout 3s;

proxy_pass 1888port;

}

server {

listen 9396;

proxy_connect_timeout 1s;

proxy_timeout 3s;

proxy_pass 9396port;

}

server {

listen 2999;

proxy_connect_timeout 1s;

proxy_timeout 3s;

proxy_pass 2999port;

}

server {

listen 2000;

proxy_connect_timeout 1s;

proxy_timeout 3s;

proxy_pass 2000port;

}

}

stream core 一些变量

注意:变量支持是从 nginx 1.11.2版本开始的

$binary_remote_addr

二进制格式的客户端地址

$bytes_received

从客户端接收到的字节数

$bytes_sent

发往客户端的字节数

$hostname

连接域名

$msec

毫秒精度的当前时间

$nginx_version

nginx 版本

$pid

worker进程号

$protocol

通信协议(UDP or TCP)

$remote_addr

客户端ip

$remote_port

客户端端口

$server_addr

接受连接的服务器ip,计算此变量需要一次系统调用。所以避免系统调用,在listen指令里必须指定具体的服务器地址并且使用参数bind。

$server_port

接受连接的服务器端口

$session_time

毫秒精度的会话时间(版本1.11.4开始)

$status

会话状态(版本1.11.4开始), 可以是一下几个值:

200

成功

400

不能正常解析客户端数据

403

禁止访问

500

服务器内部错误

502

网关错误,比如上游服务器无法连接

503

服务不可用,比如由于限制连接等措施导致

$time_iso8601

ISO 8601时间格式

$time_local

普通日志格式的时间戳

stream 模块

目前官网上列出的第三方模块、简直就是http模块的镜像、比如access模块访问控制ip和ip段,map模块实现映射、 geo模块实现地理位置映射、等等。使用这些模块的时候一定要看是哪个版本才支持的、比如log模块,只有在nginx-1.11.4才支持。

ngx_stream_core_module

ngx_stream_access_module

ngx_stream_geo_module

ngx_stream_geoip_module

ngx_stream_js_module

ngx_stream_limit_conn_module

ngx_stream_log_module

ngx_stream_map_module

ngx_stream_proxy_module

ngx_stream_realip_module

ngx_stream_return_module

ngx_stream_split_clients_module

ngx_stream_ssl_module

ngx_stream_ssl_preread_module

ngx_stream_upstream_module

ngx_stream_upstream_hc_module

参考:

Nginx 的 TCP 负载均衡介绍

Nginx 始具有TCP负载均衡的功能。从Nginx 1.9版本开始加入的,Nginx除了以前常用的HTTP负载均衡外,Nginx增加基于TCP协议实现的负载均衡方法。

HTTP负载均衡,也就是我们通常所有“七层负载均衡”,工作在第七层“应用层”。而TCP负载均衡,就是我们通常所说的“四层负载均衡”,工作在“网络层”和“传输层”。例如,LVS(Linux Virtual Server,Linux虚拟服务)和F5(一种硬件负载均衡设备),也是属于“四层负载均衡”。

TCP负载均衡的配置方式

Nginx使用了一个新的stream模块来实现TCP负载均衡,这个模块,类似于http和mail模块,允许我们配置一组监听TCP连接的服务。允许你配置多个服务的TCP连接,通过在upstream的server组中配置proxy_pass指令。

修改nginx.conf文件,在http模块的统计目录,添加一个stream模块(和http等同级):

MySQL

stream {

server {

listen 1034;

proxy_pass app;

}

upstream app {

server 192.168.0.3:1034;

server 192.168.0.4:1034;

server 192.168.0.6:1034;

}

}

stream {

server {

listen 1034;

proxy_pass app;

}

upstream app {

server 192.168.0.3:1034;

server 192.168.0.4:1034;

server 192.168.0.6:1034;

}

}

TCP负载均衡的执行原理

当Nginx从监听端口收到一个新的客户端链接时,立刻执行路由调度算法,获得指定需要连接的服务IP,然后创建一个新的上游连接,连接到指定服务器。

TCP负载均衡支持Nginx原有的调度算法,包括Round Robin(默认,轮询调度),哈希(选择一致)等。同时,调度信息数据也会和健壮性检测模块一起协作,为每个连接选择适当的目标上游服务器。如果使用Hash负载均衡的调度方法,你可以使用$remote_addr(客户端IP)来达成简单持久化会话(同一个客户端IP的连接,总是落到同一个服务server上)。

和其他upstream模块一样,TCP的stream模块也支持自定义负载均和的转发权重(配置“weight=2”),还有backup和down的参数,用于踢掉失效的上游服务器。max_conns参数可以限制一台服务器的TCP连接数量,根据服务器的容量来设置恰当的配置数值,尤其在高并发的场景下,可以达到过载保护的目的。

Nginx监控客户端连接和上游连接,一旦接收到数据,则Nginx会立刻读取并且推送到上游连接,不会做TCP连接内的数据检测。Nginx维护一份内存缓冲区,用于客户端和上游数据的写入。如果客户端或者服务端传输了量很大的数据,缓冲区会适当增加内存的大小。

当Nginx收到任意一方的关闭连接通知,或者TCP连接被闲置超过了proxy_timeout配置的时间,连接将会被关闭。对于TCP长连接,我们更应该选择适当的proxy_timeout的时间,同时,关注监听socke的so_keepalive参数,防止过早地断开连接。

服务健壮性监控

TCP负载均衡模块支持内置健壮性检测,一台上游服务器如果拒绝TCP连接超过proxy_connect_timeout配置的时间,将会被认为已经失效。在这种情况下,Nginx立刻尝试连接upstream组内的另一台正常的服务器。连接失败信息将会记录到Nginx的错误日志中。

如果一台服务器,反复失败(超过了max_fails或者fail_timeout配置的参数),Nginx也会踢掉这台服务器。服务器被踢掉60秒后,Nginx会偶尔尝试重连它,检测它是否恢复正常。如果服务器恢复正常,Nginx将它加回到upstream组内,缓慢加大连接请求的比例。

之所“缓慢加大”,因为通常一个服务都有“热点数据”,也就是说,80%以上甚至更多的请求,实际都会被阻挡在“热点数据缓存”中,真正执行处理的请求只有很少的一部分。在机器刚刚启动的时候,“热点数据缓存”实际上还没有建立,这个时候爆发性地转发大量请求过来,很可能导致机器无法“承受”而再次挂掉。以mysql为例子,我们的mysql查询,通常95%以上都是落在了内存cache中,真正执行查询的并不多。

其实,无论是单台机器或者一个集群,在高并发请求场景下,重启或者切换,都存在这个风险,解决的途径主要是两种:

(1)请求逐步增加,从少到多,逐步积累热点数据,最终达到正常服务状态。

(2)提前准备好“常用”的数据,主动对服务做“预热”,预热完成之后,再开放服务器的访问。

TCP负载均衡原理上和LVS等是一致的,工作在更为底层,性能会高于原来HTTP负载均衡不少。但是,不会比LVS更为出色,LVS被置于内核模块,而Nginx工作在用户态,而且,Nginx相对比较重。

TCP负载均衡原理上和LVS等是一致的,工作在更为底层,性能会高于原来HTTP负载均衡不少。但是,不会比LVS更为出色,LVS被置于内核模块,而Nginx工作在用户态,而且,Nginx相对比较重。

---------------------

安装keepalived

1、sudo apt-get install libpopt-dev

cd /usr/local/keepalived

2、下载源码

sudo wget http://www.keepalived.org/software/keepalived-1.3.5.tar.gz

tar -zxvf keepalived-1.2.2.tar.gz

cd keepalived-1.2.2

./configure --prefix=/usr/local/keepalived

make

make install

cp /usr/local/keepalived/sbin/keepalived /usr/sbin/

cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/

cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/

mkdir -p /etc/keepalived/

vim /etc/keepalived/keepalived.conf

/usr/local/keepalived/sbin/keepalived

----------------------------

Tips:

应用nginx的stream模块实现的tcp的nlb,能够较好的支持wcf的透明负载均衡,结合keepalived实现多个linux服务器之间的float ip(virual IP-vip)高可用切换,但是在包转发的时候存在一定的延迟,性能在100多倍(从毫秒级到百毫秒级-貌似与业务的复杂度无关,允许多个并发同时进行,吞吐量很大,并发性能优异,但每个进程还是有握手、仲裁、建立iptable虚表、转发的性能损耗)