<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></title> 
<link>http://jackxiang.com/index.php</link> 
<description><![CDATA[赢在IT，Playin' with IT,Focus on Killer Application,Marketing Meets Technology.]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></copyright>
<item>
<link>http://jackxiang.com/post//</link>
<title><![CDATA[[转]构建Nginx+heartbeat高可用web站点]]></title> 
<author>jack &lt;xdy108@126.com&gt;</author>
<category><![CDATA[Web服务器]]></category>
<pubDate>Wed, 05 Nov 2014 11:58:55 +0000</pubDate> 
<guid>http://jackxiang.com/post//</guid> 
<description>
<![CDATA[ 
	<br/>一、准备实验环境<br/>1、IP地址规划<br/>VIP： 172.16.10.8<br/>nginx1:172.16.10.1<br/>nginx2:172.16.10.2<br/>php1:172.16.10.3<br/>php2:172.16.10.4<br/>web:172.16.10.6<br/>2、网络拓扑图<br/>212133877.png<br/>3、服务器配置<br/>nginx1服务器<br/>view sourceprint?<br/>sed -i &#039;s@&#92;(HOSTNAME=&#92;).*@&#92;1nginx1.xiaodong.com@g&#039;&nbsp;&nbsp;/etc/sysconfig/network<br/>echo &quot;172.16.10.2 nginx1.xiaodong.com nginx2&quot; &amp;gt;&amp;gt; /etc/hosts<br/>ssh-keygen -t rsa<br/>ssh-copy-id .ssh/id_rsa.pub ngix2<br/>nginx2服务器<br/>view sourceprint?<br/>sed -i &#039;s@&#92;(HOSTNAME=&#92;).*@&#92;1nginx2.xiaodong.com@g&#039;&nbsp;&nbsp;/etc/sysconfig/network<br/>echo &quot;172.16.10.1 nginx1.xiaodong.com nginx1&quot; &amp;gt;&amp;gt; /etc/hosts<br/>ssh-keygen -t rsa<br/>ssh-copy-id .ssh/id_rsa.pub ngix2<br/>二、安装nginx服务器（nginx1，nginx2）<br/>view sourceprint?<br/>[root@nginx1 ~]# tar xf nginx-1.4.2.tar.gz -C /usr/local/<br/>[root@nginx1 ~]# cd /usr/local/<br/>[root@nginx1 local]# groupadd -r nginx<br/>[root@nginx1 local]# useradd -r -g nginx nginx<br/>[root@nginx1 nginx-1.4.2]# cd nginx-1.4.2/<br/>[root@nginx1 nginx-1.4.2]# ./configure &#92;<br/>--prefix=/usr &#92;<br/>--sbin-path=/usr/sbin/nginx &#92;<br/>--conf-path=/etc/nginx/nginx.conf &#92;<br/>--error-log-path=/var/log/nginx/error.log &#92;<br/>--http-log-path=/var/log/nginx/access.log &#92;<br/>--pid-path=/var/run/nginx/nginx.pid&nbsp;&nbsp;&#92;<br/>--lock-path=/var/lock/nginx.lock &#92;<br/>--user=nginx &#92;<br/>--group=nginx &#92;<br/>--with-http_ssl_module &#92;<br/>--with-http_flv_module &#92;<br/>--with-http_stub_status_module &#92;<br/>--with-http_gzip_static_module &#92;<br/>--http-client-body-temp-path=/var/tmp/nginx/client/ &#92;<br/>--http-proxy-temp-path=/var/tmp/nginx/proxy/ &#92;<br/>--http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ &#92;<br/>--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi &#92;<br/>--http-scgi-temp-path=/var/tmp/nginx/scgi &#92;<br/>--with-pcre<br/>[root@nginx1 nginx-1.4.2]# make &amp;&amp; make install<br/>[root@nginx1 nginx-1.4.2]# chmod +x /etc/init.d/nginx<br/>[root@nginx1 nginx-1.4.2]# service nginx start<br/>注意：在安装的过程中可能会缺少一些包，但是不必担心，只要使用yum install 就可用解决问题喽<br/>1、nginx支持php的配置（nginx1，nginx2）<br/>view sourceprint?<br/>[root@nginx1 ~]# vim /etc/nginx/fastcgi_params<br/>fastcgi_param&nbsp;&nbsp;GATEWAY_INTERFACE&nbsp;&nbsp;CGI/1.1;<br/>fastcgi_param&nbsp;&nbsp;SERVER_SOFTWARE&nbsp;&nbsp;&nbsp;&nbsp;nginx;<br/>fastcgi_param&nbsp;&nbsp;QUERY_STRING&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $query_string;<br/>fastcgi_param&nbsp;&nbsp;REQUEST_METHOD&nbsp;&nbsp;&nbsp;&nbsp; $request_method;<br/>fastcgi_param&nbsp;&nbsp;CONTENT_TYPE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $content_type;<br/>fastcgi_param&nbsp;&nbsp;CONTENT_LENGTH&nbsp;&nbsp;&nbsp;&nbsp; $content_length;<br/>fastcgi_param&nbsp;&nbsp;SCRIPT_FILENAME&nbsp;&nbsp;&nbsp;&nbsp;$document_root$fastcgi_script_name;<br/>fastcgi_param&nbsp;&nbsp;SCRIPT_NAME&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$fastcgi_script_name;<br/>fastcgi_param&nbsp;&nbsp;REQUEST_URI&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$request_uri;<br/>fastcgi_param&nbsp;&nbsp;DOCUMENT_URI&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $document_uri;<br/>fastcgi_param&nbsp;&nbsp;DOCUMENT_ROOT&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$document_root;<br/>fastcgi_param&nbsp;&nbsp;SERVER_PROTOCOL&nbsp;&nbsp;&nbsp;&nbsp;$server_protocol;<br/>fastcgi_param&nbsp;&nbsp;REMOTE_ADDR&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$remote_addr;<br/>fastcgi_param&nbsp;&nbsp;REMOTE_PORT&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$remote_port;<br/>fastcgi_param&nbsp;&nbsp;SERVER_ADDR&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$server_addr;<br/>fastcgi_param&nbsp;&nbsp;SERVER_PORT&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$server_port;<br/>fastcgi_param&nbsp;&nbsp;SERVER_NAME&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$server_name;<br/>~<br/>2、修改nginx配置文件（nginx1，nginx2），实现动静分离并记录访问者的IP<br/>view sourceprint?<br/>worker_processes&nbsp;&nbsp;2;<br/>events &#123;<br/>worker_connections&nbsp;&nbsp;1024;<br/>&#125;<br/>http &#123;<br/>include&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mime.types;<br/>default_type&nbsp;&nbsp;application/octet-stream;<br/>sendfile&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;on;<br/>keepalive_timeout&nbsp;&nbsp;65;<br/>proxy_connect_timeout 5;<br/>proxy_read_timeout 60;<br/>proxy_send_timeout 5;<br/>proxy_buffer_size 16k;<br/>proxy_buffers 4 64k;<br/>proxy_busy_buffers_size 128k;<br/>proxy_temp_file_write_size 128k;<br/>proxy_temp_path /home/temp_dir;<br/>proxy_cache_path /home/cache levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g;<br/>gzip&nbsp;&nbsp;on;<br/>gzip_min_length&nbsp;&nbsp;1k;<br/>gzip_buffers&nbsp;&nbsp;&nbsp;&nbsp; 4 16k;<br/>gzip_http_version 1.1;<br/>gzip_comp_level 2;<br/>gzip_types&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; text/plain application/x-javascript text/css application/xml;<br/>gzip_vary on;<br/>gzip_disable &quot;MSIE [1-6]&#92;.&quot;;<br/>upstream&nbsp;&nbsp;web &#123;<br/>server 172.16.10.3:9000&nbsp;&nbsp; max_fails=3 fail_timeout=30s;<br/>server 172.16.10.4:9000&nbsp;&nbsp;&nbsp;&nbsp;max_fails=3 fail_timeout=30s;<br/>server 172.16.10.1:80 backup;<br/>&#125;<br/>server &#123;<br/>listen&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 80;<br/>server_name&nbsp;&nbsp;localhost;<br/>location / &#123;<br/>root&nbsp;&nbsp; html;<br/>index&nbsp;&nbsp;index.html index.htm;<br/>&#125;<br/>error_page&nbsp;&nbsp; 500 502 503 504&nbsp;&nbsp;/50x.html;<br/>location = /50x.html &#123;<br/>root&nbsp;&nbsp; html;<br/>&#125;<br/>location ~ &#92;.php$ &#123;<br/>root&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /web/htdoc;<br/>fastcgi_pass&nbsp;&nbsp; web;<br/>fastcgi_index&nbsp;&nbsp;index.php;<br/>fastcgi_param&nbsp;&nbsp;SCRIPT_FILENAME&nbsp;&nbsp;/scripts$fastcgi_script_name;<br/>include&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fastcgi_params;<br/>proxy_set_header X-Real-IP $remote_addr;<br/>&#125;<br/>location ~ &#92;.(html&#124;htm&#124;gif&#124;jpg&#124;jpeg&#124;bmp&#124;png&#124;ico&#124;txt&#124;js&#124;css)$ &#123;<br/>proxy_pass http://172.16.10.6;<br/>proxy_set_header X-Real-IP $remote_addr;<br/>&#125;<br/>&#125;<br/>&#125;<br/>注释：<br/>第10行-18行 ：开启代理缓存功能<br/>第19行-26行： 开启压缩功能<br/>第44行-51行： 转发动态网页<br/>第50 行： 修改头部信息，使得后端web服务器可以看到访问端的地址<br/>第53行—56行： 转发静态网页<br/>三、安装FastCgi服务器<br/>1、php1与php2服务器<br/>view sourceprint?<br/>[root@php1 ~]#yum install gcc libxml2-devel openssl-devel bzip2-devel libmcrypt-devel&nbsp;&nbsp;-y<br/>[root@php1 ~]# tar xf php-5.4.19.tar.bz2<br/>[root@php1 ~]# cd php-5.4.19<br/>[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&nbsp;&nbsp;--enable-sockets --enable-fpm --with-mcrypt&nbsp;&nbsp;--with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2<br/>[root@php1 php-5.4.19]# make &amp;&amp; make install<br/>2、为php提供配置文件 （php1与php2）<br/>view sourceprint?<br/>[root@php1 php-5.4.19]# cp /usr/local/httpd/php/etc/php-fpm.conf.default/usr/local/httpd/php/etc/php-fpm.conf<br/>[root@php1 php-5.4.19]# cp php.ini-production /etc/php.ini<br/>3、为php-fpm提供Sysv init脚本，并将其添加至服务列表（php1与php2）<br/>view sourceprint?<br/>[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<br/>[root@php1 php-5.4.19]# chkconfig --add php-fpm<br/>[root@php1 php-5.4.19]# chkconfig php-fpm on<br/>4、修改配置文件（php1与php2）<br/>view sourceprint?<br/>[root@php1 ~]# vim /usr/local/httpd/php/etc/php-fpm.conf<br/>listen = 172.16.10.3:9000<br/>5、启动服务（php1与php2）<br/>view sourceprint?<br/>root@php1 php-5.4.19]# service php-fpm start<br/>6、创建php网址目录（php1）<br/>view sourceprint?<br/>[root@php1 ~]# mkdir -pv /web/htdoc/<br/>[root@php1 ~]# vim /web/htdoc/index.php<br/>&amp;lt;h1&amp;gt; php1 &amp;lt;/h1&amp;gt;<br/>&amp;lt;?php<br/>phpinfo();<br/>?&amp;gt;<br/>7、创建php网址目录（php2）<br/>view sourceprint?<br/>[root@php2 ~]# mkdir -pv /web/htdoc/<br/>[root@php2 ~]# vim /web/htdoc/index.php<br/>&amp;lt;h1&amp;gt; php2 &amp;lt;/h1&amp;gt;<br/>&amp;lt;?php<br/>phpinfo();<br/>?&amp;gt;<br/>四、安装http服务器（用于静态服务器）<br/>view sourceprint?<br/>[root@http ~]# yum install httpd -y<br/>[root@http ~]#echo &quot;&lt;h1&gt;stati html 172.16.10.6 &lt;/h1&gt;&quot; &gt; &gt;/var/www/html/index.html<br/>[root@http ~]#service httpd start<br/>五、测试nginx是否实现负载均衡以及动静分离<br/>1、访问动态页面测试<br/>200202922.png<br/>200206459.png<br/>2、访问静态页面测试<br/>200252219.png<br/>此时虽然实现了Nginx的负载均衡以后动静分离，但是无法保证nginx服务器的高可用，下面配置nginx的高可用<br/>六、配置Nginx的高可用服务<br/>1、安装heartbeat（nginx1，nginx2）<br/>view sourceprint?<br/>[root@nginx1 ~]# yum install heartbeat -y<br/>2、复制模块文件<br/>view sourceprint?<br/>[root@nginx1 ha.d]# cd /usr/share/doc/heartbeat-3.0.4/<br/>[root@nginx1 heartbeat-3.0.4]# cp authkeys ha.cf haresources&nbsp;&nbsp;/etc/ha.d/<br/>注释：<br/>authkeys #是节点之间的认证key文件<br/>ha.cf #heartbeat的主配置文件<br/>haresources #集群资源管理配置文件<br/>3、修改authkeys配置文件<br/>view sourceprint?<br/>[root@nginx1 ha.d]# openssl rand -hex 8&gt;&gt; /etc/ha.d/authkeys&nbsp;&nbsp;生成随机数<br/>[root@nginx1 ha.d]# vim authkeys<br/>auth 2<br/>#1crc<br/>#2sha1 HI!<br/>#3md5 Hello!<br/>2sha1 07cc87ff210e92e0<br/>4、修改权限<br/>view sourceprint?<br/>[root@nginx1 ha.d]# chmod 600authkeys<br/>5、修改主配置文件<br/>view sourceprint?<br/>[root@nginx1 ha.d]# vim ha.cf<br/>logfile /var/log/ha-log<br/>keepalive 2<br/>deadtime 30<br/>warntime 10<br/>ucast eth0 172.16.10.2#指向nginx2的IP<br/>node nginx1.xiaodong.com<br/>node nginx2.xiaodong.com<br/>6、修改资源配置文件<br/>view sourceprint?<br/>[root@nginx1 ~]# vim /etc/ha.d/haresources<br/>ngnix1.xiaodong.com&nbsp;&nbsp;&nbsp;&nbsp;172.16.10.8/16/eth0&nbsp;&nbsp; nginx<br/>注意：此处说明,nginx1为主节点<br/>7、复制配置文件到nginx2<br/>view sourceprint?<br/>[root@nginx1 ~]# cd /etc/ha.d/<br/>[root@nginx1 ha.d]# scp -p authkeys&nbsp;&nbsp;haresources ha.cf nginx2:/etc/ha.d/<br/>8、启动heartbeat服务<br/>view sourceprint?<br/>[root@nginx1 ~]# service heartbeat start<br/>[root@nginx2 ~]# service heartbeat start<br/>9、测试heartbeat与nginx是否结合<br/>查看nginx1的启动日志<br/>210931144.png<br/>10、停止nginx1服务<br/>view sourceprint?<br/>[root@nginx1 ~]# service heartbeat stop<br/>当nginx1停掉之后，查看nginx2日志信息<br/>210953889.png<br/>以上信息反馈出来了，当nginx1 down掉之后，nginx2立刻检测到，并启动nginx服务，保证了nginx的高可用性。<br/><br/>来自：http://www.it165.net/admin/html/201309/1831.html
]]>
</description>
</item><item>
<link>http://jackxiang.com/post//#blogcomment</link>
<title><![CDATA[[评论] [转]构建Nginx+heartbeat高可用web站点]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>http://jackxiang.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>