haproxy实验笔记

简介:

实验规划:

主机名 IP地址 角色
node01 192.168.112.128 web01
node02 192.168.112.129 web02
node03 192.168.112.130 haproxy

操作系统信息:

1
2
3
4
5
[root@node03 ~] # cat /etc/redhat-release 
CentOS release 6.6 (Final)
[root@node03 ~] # uname -a
Linux node03 2.6.32-504.el6.x86_64  #1 SMP Wed Oct 15 04:27:16 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
[root@node03 ~] #

安装haproxy:

1
[root@node03 ~] # yum -y install haproxy nginx php

安装nginx php:

1
2
[root@node01 ~] # yum -y install nginx
[root@node02 ~] # yum -y install nginx

node01和node02上nginx配置文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
user  nginx;
worker_processes  4;
#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;
server {
         listen       80;
         server_name  localhost;
         #charset koi8-r;
         #access_log  logs/host.access.log  main;
         location / {
             root   html;
             index  index.html index.htm;
         }
}

node03上haproxy和nginx配置文件

haproxy:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
global
     log         127.0.0.1 local2
     chroot      /var/lib/haproxy
     pidfile     /var/run/haproxy.pid
     maxconn     4000
     user        haproxy
     group       haproxy
     daemon
     # turn on stats unix socket
     stats socket /var/lib/haproxy/stats
defaults
     mode                    http
     log                     global
     option                  httplog
     option                  dontlognull
     option http-server-close
     option forwardfor       except 127.0.0.0/8
     option                  redispatch
     retries                 3
     timeout http-request    10s
     timeout queue           1m
     timeout connect         10s
     timeout client          1m
     timeout server          1m
     timeout http-keep-alive 10s
     timeout check           10s
     maxconn                 3000
listen s-monitor *:90
    mode http
    stats enable
    stats refresh 5s
    stats realm New0ldName
    stats uri /
    stats auth user:123456
frontend  main *:80
     acl url_static       path_beg       -i /static /images /javascript /stylesheets
     acl url_static       path_end       -i .jpg .gif .png .css .js
     use_backend static          if url_static
     default_backend             app
backend static
     balance     roundrobin
     server      static 127.0.0.1:8080 check
backend app
     balance     roundrobin
     server  app1 192.168.112.128:80 check
     server  app2 192.168.112.129:80 check

nginx配置文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
user  nginx;
worker_processes  4;
#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;
     sendfile        on;
user  nginx;
worker_processes  4;
#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       8080;
         server_name  localhost;
         #charset koi8-r;
         #access_log  logs/host.access.log  main;
         root    /data/website/app;
         location / {
             #root   html;
             index  index.php index.html index.htm;
         }
         #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;
         }
         # proxy the PHP scripts to Apache listening on 127.0.0.1:80
         #
         #location ~ \.php$ {
         #    proxy_pass   http://127.0.0.1;
         #}
         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
         #
         location ~ \.php$ {
             root           html;
             fastcgi_pass   127.0.0.1:9000;
             fastcgi_index  index.php;
             fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
             include        fastcgi_params;
         }


haproxy平滑重启操作:

(1)如果是编译安装 

1
[root@node03 ~] #/usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/conf/haproxy.conf  -sf `cat /usr/local/haproxy/var/run/haproxy.pid`

或者

1
[root@node03 ~] #/usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/conf/haproxy.conf  -st `cat /usr/local/haproxy/var/run/haproxy.pid`

(2)如果是yum安装

1
[root@node03 ~] # /etc/init.d/haproxy reload


开启日志记录:

编辑/etc/rsyslog.conf和/etc/sysconfig/syslog文件

1
2
3
4
5
6
7
8
9
10
[root@~] # vim  /etc/rsyslog.conf:
  13 $ModLoad imudp   #将注释去除
  14 $UDPServerRun 514  #将注释去除
local3.*          /var/log/haproxy .log  #最下方添加
local0.*          /var/log/haproxy .log  #最下方添加
[root@~] #vi /etc/sysconfig/syslog
#######################################
把SYSLOGD_OPTIONS= "-m 0"
改成 SYSLOGD_OPTIONS= "-r -m 0"
#######################################


相关解释说明:

1
2
3
4
5
-r:打开接受外来日志消息的功能,其监控514 UDP端口;
-x:关闭自动解析对方日志服务器的FQDN信息,这能避免DNS不完整所带来的麻烦;
-m:修改syslog的内部mark消息写入间隔时间(0为关闭),例如240为每隔240分钟写入一次"--MARK--"信息;
-h:默认情况下,syslog不会发送从远端接受过来的消息到其他主机,而使用该选项,则把该开关打开,所有
接受到的信息都可根据syslog.conf中定义的@主机转发过去


重启rsyslog服务

1
[root@~] # systemctl restart rsyslog.service





      本文转自027ryan  51CTO博客,原文链接:http://blog.51cto.com/ucode/1880704,如需转载请自行联系原作者






相关文章
|
Oracle Java 关系型数据库
LAMP 出现和工作原理介绍 | 学习笔记
快速学习LAMP 出现和工作原理介绍
169 0
LAMP 出现和工作原理介绍 | 学习笔记
|
缓存 负载均衡 监控
Nginx网站服务配置文件实操看这篇就够了(二)
Nginx网站服务配置文件实操看这篇就够了(二)
883 0
Nginx网站服务配置文件实操看这篇就够了(二)
|
应用服务中间件 网络安全 Apache
Nginx网站服务配置文件实操看这篇就够了(一)
Nginx网站服务配置文件实操看这篇就够了(一)
232 0
Nginx网站服务配置文件实操看这篇就够了(一)
|
域名解析 应用服务中间件 nginx
Nginx网站服务配置文件实操看这篇就够了(三)
Nginx网站服务配置文件实操看这篇就够了(三)
130 0
Nginx网站服务配置文件实操看这篇就够了(三)
|
存储 缓存 tengine
nginx基本理论和安装部署
Nginx (“engine x”) 是一个高性能的、轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器。并在一个BSD-like 协议下发行
226 0
nginx基本理论和安装部署
|
存储 负载均衡 前端开发
nginx从理论到实践超详细笔记总结
nginx从理论到实践超详细笔记总结
167 0
|
存储 缓存 负载均衡
nginx从理论到实践超详细笔记总结(下)
nginx从理论到实践超详细笔记总结(下)
227 0
|
存储 应用服务中间件 Linux
Nginx从理论到实践超详细笔记(上)
Nginx从理论到实践超详细笔记(上)
Nginx从理论到实践超详细笔记(上)
|
存储 缓存 负载均衡
Nginx从理论到实践超详细笔记(下)
Nginx从理论到实践超详细笔记(下)
Nginx从理论到实践超详细笔记(下)
|
应用服务中间件 nginx 测试技术
第八章:nginx常见问题
相同server_name多个虚拟主机优先级访问 优先读取第一个conf文件。 location匹配优先级 =进行普通字符精确匹配,也就是完全匹配优先级最高^~表示普通字符匹配,使用前缀匹配优先级最高~ \~*表示执行一个真个则匹配优先级最低 try_...
840 0