背景
haproxy对于后端的服务器就算请求没过来也有一直在健康检测,发现有故障的时候在请求没到来的时候会切掉,但如果在检测期间请求到达的话,会有异常。haproxy只会把请求转到后端的一台服务器。
nginx对于后端的服务器没有一直在健康检测,请求过来的时候,分发还是进行分发,只是请求不到数据的时候,会再向好的机器进行请求,直到请求正常为止。nginx请求转到后端一台不成功的话,还会转向另外一台服务器。同时我也测试了下squid,发现squid跟nginx的反向代理负载均衡很相似。
(改天再补充)
虚拟机:
192.168.122.4 hinxttpd
192.168.122.5 httpd
192.168.122.6 www.ooo.org nginx
安装配置
[root@server6 ~]#tar xvzf nginx-1.10.1.tar.gz ##解压
[root@server6 ~]# vim nginx-1.10.1/src/core/nginx.h ##删除版本号
14 #define NGINX_VER "nginx/"
[root@server6 ~]# vim auto/cc/gcc ##停用DEBUG模式
179 #CFLAGS="$CFLAGS -g"
[root@server6 ~]# yum install -y gcc ##安装gcc
[root@server6 ~]# yum install -y pcre-devel openssl-devel ##安装依赖性
[root@server6 ~]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-h ttp_stub_status_module ##编译的时候,指定安装后的位置,加入ssl模块,做https,加入一个监控的模块
[root@server6 ~]# make && make install ##完成
nginx是用来应对C10K问题的,当面对这个问题时,也是需要设置并发量的,所以nginx进程是需要以普通用户的身份去运行,然后给这个用户也设置并发量。
[root@server6 ~]# useradd -u 800 nginx
[root@server6 ~]# vim /etc/security/limits.conf
42 #* soft core 0
43 #* hard rss 10000
44 #@student hard nproc 20
45 #@faculty soft nproc 20
46 #@faculty hard nproc 50
47 #ftp hard nproc 0
48 #@student - maxlogins 4
49 nginx - nofile 65535
##nginx用户 所有 并发 并发数量
50 # End of file
[nginx@server6 root]$ ulimit -n 检查并发数
65535
配置文件 /usr/local/nginx/conf/nginx.conf
1
2 user nginx; ##绑定用户
3 worker_processes 1; ##cpu的绑定,这里比较复杂,文章末尾细说
4
5 #error_log logs/error.log; ##全局错误日志及PID文件
6 #error_log logs/error.log notice;
7 #error_log logs/error.log info;
8
9 #pid logs/nginx.pid;
10
11
12 events {
13 worker_connections 65535; ##单个后台worker process进程的最大并发链接数
14 }
15
16
17 http {
18
19 upstream linux{ ##定义一个负载均衡的集群,名字为linux
20 sticky; ##负载均衡的算法,这个算法原生的nginx不支持
21 server 192.168.122.4:80; ##两个RS
22 server 192.168.122.5:80;
23 }
24
25 include mime.types; ##设定mime类型,类型由mime.type文件定义
26 default_type application/octet-stream;
27 #设定日志格式
28 #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
29 # '$status $body_bytes_sent "$http_referer" '
30 # '"$http_user_agent" "$http_x_forwarded_for"';
31
32 #access_log logs/access.log main;
33
34 sendfile on; ##sendfile 指令指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文件,
##对于普通应用,必须设为 on,
##如果用来进行下载等应用磁盘IO重负载应用,可设置为 off,
##以平衡磁盘与网络I/O处理速度,降低系统的uptime.
35 tcp_nopush on;
36 tcp_nodelay on;
37
38 #keepalive_timeout 0;
39 keepalive_timeout 65; ##连接超时时间
40
41 #gzip on; ##是否开启gzip压缩
42
43 server { ##设定虚拟主机配置
44 listen 80; ##监听地址为80端口
45 server_name localhost; ##这里应该写域名
46
47 #charset koi8-r;
48
49 #access_log logs/host.access.log main; ##日志
50
51 location / {
52 root html; ##根目录
53 index index.html index.htm; ##默认访问的文件
54 }
55
56 #error_page 404 /404.html;
57
58 # redirect server error pages to the static page /50x.html
59 #
60 error_page 500 502 503 504 /50x.html; ## 定义错误提示页面
61 location = /50x.html {
62 root html;
63 }
64
65 location /status{ ##定义一个监控页面
66 stub_status on; ##状态开启
67 access_log off; ##日志关闭
68 allow 192.168.122.1; ##允许192.168.122.1访问
69 deny all; ##其他的全部禁止
70 }
71
72
73 # proxy the PHP scripts to Apache listening on 127.0.0.1:80
74 #
75 #location ~ \.php$ { ##定义php请求来访时的动作
76 # proxy_pass http://127.0.0.1;
77 #}
78
79 # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
80 #
81 #location ~ \.php$ {
82 # root html;
83 # fastcgi_pass 127.0.0.1:9000;
84 # fastcgi_index index.php;
85 # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
86 # include fastcgi_params;
87 #}
88
89 # deny access to .htaccess files, if Apache's document root
90 # concurs with nginx's one
91 #
92 #location ~ /\.ht {
93 # deny all;
94 #}
95 }
96
97
98 # another virtual host using mix of IP-, name-, and port-based configuration
99 #
100 #server {
101 # listen 8000;
102 # listen somename:8080;
103 # server_name somename alias another.alias;
104
105 # location / {
106 # root html;
107 # index index.html index.htm;
108 # }
109 #}
110
111
112 # HTTPS server
113 #
114 #server {
115 # listen 443 ssl;
116 # server_name localhost;
117
118 # ssl_certificate cert.pem;
119 # ssl_certificate_key cert.key;
120
121 # ssl_session_cache shared:SSL:1m;
122 # ssl_session_timeout 5m;
123
124 # ssl_ciphers HIGH:!aNULL:!MD5;
125 # ssl_prefer_server_ciphers on;
126
127 # location / {
128 # root html;
129 # index index.html index.htm;
130 # }
131 #}
132 server { ##定义一个虚拟主机
133 listen 80; ##监听80端口
134 server_name www.ooo.org; ##通过www.ooo.org访问
135 location / {
136 proxy_pass http://linux; ##转入linux的RS组内
137 }
138 }
139
140
141 }
cpu的绑定
默认情况下,Nginx 的多个进程有可能跑在某一个 CPU 或 CPU 的某一核上,导致 Nginx 进程使用硬件的资源不均,因此绑定 Nginx 进程到不同的 CPU 上是为了充分利用硬件的多 CPU 多核资源的目的
worker_processes 1; ##单核
worker_processes 2; ##2核CPU的配置
worker_cpu_affinity 01 10;
worker_processes 4; ##4核CPU的配置
worker_cpu_affinity 0001 0010 0100 1000;
worker_processes 8; ##8核CPU的配置
worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 1000000;
访问测试 RR轮叫算法
[root@server westos]# curl www.ooo.org
server4
[root@server westos]# curl www.ooo.org
server5
[root@server westos]# curl www.ooo.org
server4
[root@server westos]# curl www.ooo.org
server5
命令行和浏览器都是轮叫算法
第二次测试
基于浏览器cooike的
[root@server westos]# curl www.ooo.org
server4
[root@server westos]# curl www.ooo.org
server5
[root@server westos]# curl www.ooo.org
server4
[root@server westos]# curl www.ooo.org
server5
浏览器都是有cooike的,但是命令行没有,所以结果是那样的
注意nginx是不支持动态编译的,所以第二次加入新的算法的时候,应该全部重新编译。
[root@server6 ~]# make clean
[root@server6 ~]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-[root@server6 ~]# http_stub_status_module --add-module=/root/nginx-sticky-module-ng
[root@server6 ~]# make && make install