您的位置:首页 > 运维架构 > Nginx

构建Nginx+heartbeat高可用web站点

2016-06-14 10:16 477 查看
一、准备实验环境

1、IP地址规划

VIP: 172.16.10.8

nginx1:172.16.10.1

nginx2:172.16.10.2

php1:172.16.10.3

php2:172.16.10.4

web:172.16.10.6

2、网络拓扑图





3、服务器配置

nginx1服务器

view
sourceprint?

1.
sed
-i
's@\(HOSTNAME=\).*@\1nginx1.xiaodong.com@g'  /etc/sysconfig/network

2.
echo
"172.16.10.2
nginx1.xiaodong.com nginx2"
&
gt
;&
gt
;
/etc/hosts

3.
ssh
-keygen
-t rsa

4.
ssh
-copy-
id
.
ssh
/id_rsa.pub
ngix2


nginx2服务器

view
sourceprint?

1.
sed
-i
's@\(HOSTNAME=\).*@\1nginx2.xiaodong.com@g'  /etc/sysconfig/network

2.
echo
"172.16.10.1
nginx1.xiaodong.com nginx1"
&
gt
;&
gt
;
/etc/hosts

3.
ssh
-keygen
-t rsa

4.
ssh
-copy-
id
.
ssh
/id_rsa.pub
ngix2


二、安装nginx服务器(nginx1,nginx2)

view
sourceprint?

01.
[root@nginx1
~]
#
tar xf nginx-1.4.2.tar.gz -C /usr/local/

02.
[root@nginx1
~]
#
cd /usr/local/

03.
[root@nginx1
local
]
#
groupadd -r nginx

04.
[root@nginx1
local
]
#
useradd -r -g nginx nginx

05.
[root@nginx1
nginx-1.4.2]
#
cd nginx-1.4.2/

06.
[root@nginx1
nginx-1.4.2]
#
./configure \

07.
--prefix=/usr
\

08.
--sbin-path=/usr/sbin/nginx
\

09.
--conf-path=/etc/nginx/nginx.conf
\

10.
--error-log-path=/var/log/nginx/error.log
\

11.
--http-log-path=/var/log/nginx/access.log
\

12.
--pid-path=/var/run/nginx/nginx.pid
\

13.
--lock-path=/var/lock/nginx.lock
\

14.
--user=nginx
\

15.
--group=nginx
\

16.
--with-http_ssl_module
\

17.
--with-http_flv_module
\

18.
--with-http_stub_status_module
\

19.
--with-http_gzip_static_module
\

20.
--http-client-body-temp-path=/var/tmp/nginx/client/
\

21.
--http-proxy-temp-path=/var/tmp/nginx/proxy/
\

22.
--http-fastcgi-temp-path=/var/tmp/nginx/fcgi/
\

23.
--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi
\

24.
--http-scgi-temp-path=/var/tmp/nginx/scgi
\

25.
--with-pcre

26.
[root@nginx1
nginx-1.4.2]
#
make && make install

27.
[root@nginx1
nginx-1.4.2]
#
chmod +x /etc/init.d/nginx

28.
[root@nginx1
nginx-1.4.2]
#
service nginx start


注意:在安装的过程中可能会缺少一些包,但是不必担心,只要使用yum install 就可用解决问题喽

1、nginx支持php的配置(nginx1,nginx2)

view
sourceprint?

01.
[root@nginx1
~]
#
vim /etc/nginx/fastcgi_params

02.
fastcgi_param
GATEWAY_INTERFACE  CGI/1.1;

03.
fastcgi_param
SERVER_SOFTWARE    nginx;

04.
fastcgi_param
QUERY_STRING       $query_string;

05.
fastcgi_param
REQUEST_METHOD     $request_method;

06.
fastcgi_param
CONTENT_TYPE       $content_type;

07.
fastcgi_param
CONTENT_LENGTH     $content_length;

08.
fastcgi_param
SCRIPT_FILENAME    $document_root$fastcgi_script_name;

09.
fastcgi_param
SCRIPT_NAME        $fastcgi_script_name;

10.
fastcgi_param
REQUEST_URI        $request_uri;

11.
fastcgi_param
DOCUMENT_URI       $document_uri;

12.
fastcgi_param
DOCUMENT_ROOT      $document_root;

13.
fastcgi_param
SERVER_PROTOCOL    $server_protocol;

14.
fastcgi_param
REMOTE_ADDR        $remote_addr;

15.
fastcgi_param
REMOTE_PORT        $remote_port;

16.
fastcgi_param
SERVER_ADDR        $server_addr;

17.
fastcgi_param
SERVER_PORT        $server_port;

18.
fastcgi_param
SERVER_NAME        $server_name;

19.
~


2、修改nginx配置文件(nginx1,nginx2),实现动静分离并记录访问者的IP

view
sourceprint?

01.
worker_processes
2;

02.
events
{

03.
worker_connections
1024;

04.
}

05.
http
{

06.
include
mime.types;

07.
default_type
application/octet-stream;

08.
sendfile
on;

09.
keepalive_timeout
65;

10.
proxy_connect_timeout
5;

11.
proxy_read_timeout
60;

12.
proxy_send_timeout
5;

13.
proxy_buffer_size
16k;

14.
proxy_buffers
4 64k;

15.
proxy_busy_buffers_size
128k;

16.
proxy_temp_file_write_size
128k;

17.
proxy_temp_path
/home/temp_dir;

18.
proxy_cache_path
/home/cache levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g;

19.
gzip
on;

20.
gzip_min_length
1k;

21.
gzip_buffers
4 16k;

22.
gzip_http_version
1.1;

23.
gzip_comp_level
2;

24.
gzip_types
text/plain application/x-javascript text/css application/xml;

25.
gzip_vary
on;

26.
gzip_disable
"MSIE
[1-6]\."
;

27.
upstream
web {

28.

29.
server
172.16.10.3:9000   max_fails=3 fail_timeout=30s;

30.
server
172.16.10.4:9000    max_fails=3 fail_timeout=30s;

31.
server
172.16.10.1:80 backup;

32.
}

33.
server
{

34.
listen
80;

35.
server_name
localhost;

36.
location
/ {

37.
root
html;

38.
index
index.html index.htm;

39.
}

40.
error_page
500 502 503 504  /50x.html;

41.
location
= /50x.html {

42.
root
html;

43.
}

44.
location
~ \.php$ {

45.
root
/web/htdoc;

46.
fastcgi_pass
web;

47.
fastcgi_index
index.php;

48.
fastcgi_param
SCRIPT_FILENAME  /scripts$fastcgi_script_name;

49.
include
fastcgi_params;

50.
proxy_set_header
X-Real-IP $remote_addr;

51.
}

52.

53.
location
~ \.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ {

54.
proxy_pass http://172.16.10.6;

55.
proxy_set_header
X-Real-IP $remote_addr;

56.
}

57.
}

58.
}


注释:

第10行-18行 :开启代理缓存功能

第19行-26行: 开启压缩功能

第44行-51行: 转发动态网页

第50 行: 修改头部信息,使得后端web服务器可以看到访问端的地址

第53行—56行: 转发静态网页

三、安装FastCgi服务器

1、php1与php2服务器

view
sourceprint?

1.
[root@php1
~]
#yum
install gcc libxml2-devel openssl-devel bzip2-devel libmcrypt-devel  -y

2.
[root@php1
~]
#
tar xf php-5.4.19.tar.bz2

3.
[root@php1
~]
#
cd php-5.4.19

4.
[root@php1
php-5.4.19]
#
./configure --prefix=/usr/local/httpd/php --with-mysql=mysqlnd --with-openssl --with-mysqli=mysqlnd --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml  --enable-sockets --enable-fpm --with-mcrypt
--with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2

5.
[root@php1
php-5.4.19]
#
make && make install


2、为php提供配置文件 (php1与php2)

view
sourceprint?

1.
[root@php1
php-5.4.19]
#
cp /usr/local/httpd/php/etc/php-fpm.conf.default/usr/local/httpd/php/etc/php-fpm.conf

2.

3.
[root@php1
php-5.4.19]
#
cp php.ini-production /etc/php.ini


3、为php-fpm提供Sysv init脚本,并将其添加至服务列表(php1与php2)

view
sourceprint?

1.
[root@php1
php-5.4.19]
#
cp sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm[root@php1 php-5.4.19]# chmod +x /etc/rc.d/init.d/php-fpm

2.
[root@php1
php-5.4.19]
#
chkconfig --add php-fpm

3.
[root@php1
php-5.4.19]
#
chkconfig php-fpm on


4、修改配置文件(php1与php2)

view
sourceprint?

1.
[root@php1
~]
#
vim /usr/local/httpd/php/etc/php-fpm.conf

2.
listen
= 172.16.10.3:9000


5、启动服务(php1与php2)

view
sourceprint?

1.
root@php1
php-5.4.19]
#
service php-fpm start


6、创建php网址目录(php1)

view
sourceprint?

1.
[root@php1
~]
#
mkdir -pv /web/htdoc/

2.
[root@php1
~]
#
vim /web/htdoc/index.php

3.
&
lt
;h1&
gt
;
php1 &
lt
;/h1&
gt
;

4.
&
lt
;?php

5.
phpinfo();

6.
?&
gt
;


7、创建php网址目录(php2)

view
sourceprint?

1.
[root@php2
~]
#
mkdir -pv /web/htdoc/

2.
[root@php2
~]
#
vim /web/htdoc/index.php

3.
&
lt
;h1&
gt
;
php2 &
lt
;/h1&
gt
;

4.
&
lt
;?php

5.
phpinfo();

6.
?&
gt
;


四、安装http服务器(用于静态服务器)

view
sourceprint?

1.
[root@http
~]
#
yum install httpd -y

2.
[root@http
~]
#echo
"<h1>stati html 172.16.10.6 </h1>" > >/var/www/html/index.html

3.
[root@http
~]
#service
httpd start


五、测试nginx是否实现负载均衡以及动静分离

1、访问动态页面测试









2、访问静态页面测试





此时虽然实现了Nginx的负载均衡以后动静分离,但是无法保证nginx服务器的高可用,下面配置nginx的高可用

六、配置Nginx的高可用服务

1、安装heartbeat(nginx1,nginx2)

view
sourceprint?

1.
[root@nginx1
~]
#
yum install heartbeat -y


2、复制模块文件

view
sourceprint?

1.
[root@nginx1
ha.d]
#
cd /usr/share/doc/heartbeat-3.0.4/

2.
[root@nginx1
heartbeat-3.0.4]
#
cp authkeys ha.cf haresources  /etc/ha.d/


注释:

authkeys #是节点之间的认证key文件

ha.cf #heartbeat的主配置文件

haresources #集群资源管理配置文件

3、修改authkeys配置文件

view
sourceprint?

1.
[root@nginx1
ha.d]
#
openssl rand -hex 8>> /etc/ha.d/authkeys  生成随机数

2.
[root@nginx1
ha.d]
#
vim authkeys

3.
auth
2

4.
#1crc

5.
#2sha1
HI!

6.
#3md5
Hello!

7.
2sha1
07cc87ff210e92e0


4、修改权限

view
sourceprint?

1.
[root@nginx1
ha.d]
#
chmod 600authkeys


5、修改主配置文件

view
sourceprint?

1.
[root@nginx1
ha.d]
#
vim ha.cf

2.
logfile
/var/log/ha-log

3.
keepalive
2

4.
deadtime
30

5.
warntime
10

6.
ucast
eth0 172.16.10.2
#指向nginx2的IP

7.
node
nginx1.xiaodong.com

8.
node
nginx2.xiaodong.com


6、修改资源配置文件

view
sourceprint?

1.
[root@nginx1
~]
#
vim /etc/ha.d/haresources

2.
ngnix1.xiaodong.com
172.16.10.8/16/eth0   nginx


注意:此处说明,nginx1为主节点

7、复制配置文件到nginx2

view
sourceprint?

1.
[root@nginx1
~]
#
cd /etc/ha.d/

2.
[root@nginx1
ha.d]
#
scp -p authkeys  haresources ha.cf nginx2:/etc/ha.d/


8、启动heartbeat服务

view
sourceprint?

1.
[root@nginx1
~]
#
service heartbeat start

2.
[root@nginx2
~]
#
service heartbeat start


9、测试heartbeat与nginx是否结合

查看nginx1的启动日志





10、停止nginx1服务

view
sourceprint?

1.
[root@nginx1
~]
#
service heartbeat stop


当nginx1停掉之后,查看nginx2日志信息





以上信息反馈出来了,当nginx1 down掉之后,nginx2立刻检测到,并启动nginx服务,保证了nginx的高可用性。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: