设为首页 收藏本站
查看: 1081|回复: 0

[经验分享] 构建Nginx+fastcgi+heartbeat高可用

[复制链接]
发表于 2019-1-7 10:15:31 | 显示全部楼层 |阅读模式
  一、准备实验环境
  二、安装nginx服务器(nginx1,nginx2)
  三、安装FastCgi服务器
  四、安装http服务器(用于静态服务器)
  五、测试nginx是否实现负载均衡以及动静分离
  六、配置Nginx的高可用服务
  一、准备实验环境
  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服务器
1234sed -i 's@\(HOSTNAME=\).*@\1nginx1.xiaodong.com@g'/etc/sysconfig/networkecho "172.16.10.2 nginx1.xiaodong.com nginx2">> /etc/hostsssh-keygen -t rsassh-copy-id .ssh/id_rsa.pub ngix2  nginx2服务器
  
1234sed -i 's@\(HOSTNAME=\).*@\1nginx2.xiaodong.com@g'/etc/sysconfig/networkecho "172.16.10.1 nginx1.xiaodong.com nginx1">> /etc/hostsssh-keygen -t rsassh-copy-id .ssh/id_rsa.pub ngix2  二、安装nginx服务器(nginx1,nginx2)
12345678910111213141516171819202122232425262728[root@nginx1 ~]# tar xf nginx-1.4.2.tar.gz -C /usr/local/[root@nginx1 ~]# cd /usr/local/[root@nginx1 local]# groupadd -r nginx[root@nginx1 local]# useradd -r -g nginx nginx[root@nginx1 nginx-1.4.2]# cd nginx-1.4.2/[root@nginx1 nginx-1.4.2]# ./configure \--prefix=/usr \--sbin-path=/usr/sbin/nginx \--conf-path=/etc/nginx/nginx.conf \--error-log-path=/var/log/nginx/error.log \--http-log-path=/var/log/nginx/access.log \--pid-path=/var/run/nginx/nginx.pid \--lock-path=/var/lock/nginx.lock \--user=nginx \--group=nginx \--with-http_ssl_module \--with-http_flv_module \--with-http_stub_status_module \--with-http_gzip_static_module \--http-client-body-temp-path=/var/tmp/nginx/client/ \--http-proxy-temp-path=/var/tmp/nginx/proxy/ \--http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \--http-scgi-temp-path=/var/tmp/nginx/scgi \--with-pcre[root@nginx1 nginx-1.4.2]# make && make install[root@nginx1 nginx-1.4.2]# chmod +x /etc/init.d/nginx[root@nginx1 nginx-1.4.2]# service nginx start  注意:在安装的过程中可能会缺少一些包,但是不必担心,只要使用yum install 就可用解决问题喽
  1、nginx支持php的配置(nginx1,nginx2)
12345678910111213141516171819[root@nginx1 ~]# vim /etc/nginx/fastcgi_paramsfastcgi_param GATEWAY_INTERFACE CGI/1.1;fastcgi_param SERVER_SOFTWARE  nginx;fastcgi_param QUERY_STRING    $query_string;fastcgi_param REQUEST_METHOD   $request_method;fastcgi_param CONTENT_TYPE    $content_type;fastcgi_param CONTENT_LENGTH   $content_length;fastcgi_param SCRIPT_FILENAME  $document_root$fastcgi_script_name;fastcgi_param SCRIPT_NAME    $fastcgi_script_name;fastcgi_param REQUEST_URI    $request_uri;fastcgi_param DOCUMENT_URI    $document_uri;fastcgi_param DOCUMENT_ROOT   $document_root;fastcgi_param SERVER_PROTOCOL  $server_protocol;fastcgi_param REMOTE_ADDR    $remote_addr;fastcgi_param REMOTE_PORT    $remote_port;fastcgi_param SERVER_ADDR    $server_addr;fastcgi_param SERVER_PORT    $server_port;fastcgi_param SERVER_NAME    $server_name;~  2、修改nginx配置文件(nginx1,nginx2),实现动静分离并记录访问者的IP
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758worker_processes 2;events {worker_connections 1024;}http {includemime.types;default_type application/octet-stream;sendfile    on;keepalive_timeout 65;proxy_connect_timeout 5;proxy_read_timeout 60;proxy_send_timeout 5;proxy_buffer_size 16k;proxy_buffers 464k;proxy_busy_buffers_size 128k;proxy_temp_file_write_size 128k;proxy_temp_path /home/temp_dir;proxy_cache_path /home/cache levels=1:2keys_zone=cache_one:200m inactive=1d max_size=30g;gzip on;gzip_min_length 1k;gzip_buffers   416k;gzip_http_version 1.1;gzip_comp_level 2;gzip_types    text/plain application/x-javascript text/css application/xml;gzip_vary on;gzip_disable "MSIE [1-6]\.";upstream web {server 172.16.10.3:9000max_fails=3fail_timeout=30s;server 172.16.10.4:9000max_fails=3fail_timeout=30s;server 172.16.10.1:80backup;}server {listen    80;server_name localhost;location / {root  html;index index.html index.htm;}error_page  500502503504/50x.html;location = /50x.html {root  html;}location ~ \.php$ {root      /web/htdoc;fastcgi_pass  web;fastcgi_index index.php;fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;includefastcgi_params;proxy_set_header X-Real-IP $remote_addr;}location ~ \.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ {proxy_pass http://172.16.10.6;proxy_set_header X-Real-IP $remote_addr;}}}  注释:
  第10行-18行 :开启代理缓存功能
  第19行-26行: 开启压缩功能
  第44行-51行: 转发动态网页
  第50 行: 修改头部信息,使得后端web服务器可以看到访问端的地址
  第53行—56行: 转发静态网页
  三、安装FastCgi服务器
  1、php1与php2服务器
12345[root@php1 ~]#yum install gcc libxml2-devel openssl-devel bzip2-devel libmcrypt-devel -y[root@php1 ~]# tar xf php-5.4.19.tar.bz2[root@php1 ~]# cd php-5.4.19[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[root@php1 php-5.4.19]# make && make install  2、为php提供配置文件 (php1与php2)
1[root@php1 php-5.4.19]# cp /usr/local/httpd/php/etc/php-fpm.conf.default/usr/local/httpd/php/etc/php-fpm.conf1[root@php1 php-5.4.19]# cp php.ini-production /etc/php.ini  3、为php-fpm提供Sysv init脚本,并将其添加至服务列表(php1与php2)
123[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[root@php1 php-5.4.19]# chkconfig --add php-fpm[root@php1 php-5.4.19]# chkconfig php-fpm on  4、修改配置文件(php1与php2)
12[root@php1 ~]# vim /usr/local/httpd/php/etc/php-fpm.conflisten = 172.16.10.3:9000  5、启动服务(php1与php2)
1root@php1 php-5.4.19]# service php-fpm start  6、创建php网址目录(php1)
123456[root@php1 ~]# mkdir -pv /web/htdoc/[root@php1 ~]# vim /web/htdoc/index.php php1   7、创建php网址目录(php2
123456[root@php2 ~]# mkdir -pv /web/htdoc/[root@php2 ~]# vim /web/htdoc/index.php php2   四、安装http服务器(用于静态服务器)
123[root@http ~]# yum install httpd -y[root@http ~]#echo "stati html 172.16.10.6 "> >/var/www/html/index.html[root@http ~]#service httpd start  五、测试nginx是否实现负载均衡以及动静分离
  1、访问动态页面测试


  2、访问静态页面测试

  此时虽然实现了Nginx的负载均衡以后动静分离,但是无法保证nginx服务器的高可用,下面配置nginx的高可用
  六、配置Nginx的高可用服务
  1、安装heartbeat(nginx1,nginx2)
1[root@nginx1 ~]# yum install heartbeat -y  2、复制模块文件
12[root@nginx1 ha.d]# cd /usr/share/doc/heartbeat-3.0.4/[root@nginx1 heartbeat-3.0.4]# cp authkeys ha.cf haresources /etc/ha.d/  注释:
  authkeys #是节点之间的认证key文件
  ha.cf #heartbeat的主配置文件
  haresources #集群资源管理配置文件
  3、修改authkeys配置文件
1234567[root@nginx1 ha.d]# openssl rand -hex 8>> /etc/ha.d/authkeys 生成随机数[root@nginx1 ha.d]# vim authkeysauth 2#1crc#2sha1 HI!#3md5 Hello!2sha1 07cc87ff210e92e0  4、修改权限
1[root@nginx1 ha.d]# chmod 600authkeys  5、修改主配置文件
12345678[root@nginx1 ha.d]# vim ha.cflogfile /var/log/ha-logkeepalive 2deadtime 30warntime 10ucast eth0 172.16.10.2#指向nginx2的IPnode nginx1.xiaodong.comnode nginx2.xiaodong.com  6、修改资源配置文件
12[root@nginx1 ~]# vim /etc/ha.d/haresourcesngnix1.xiaodong.com  172.16.10.8/16/eth0  nginx  注意:此处说明,nginx1为主节点
  7、复制配置文件到nginx2
12[root@nginx1 ~]# cd /etc/ha.d/[root@nginx1 ha.d]# scp -p authkeys haresources ha.cf nginx2:/etc/ha.d/  8、启动heartbeat服务
12[root@nginx1 ~]# service heartbeat start[root@nginx2 ~]# service heartbeat start  9、测试heartbeat与nginx是否结合
  查看nginx1的启动日志

  10、停止nginx1服务
1[root@nginx1 ~]# service heartbeat stop  当nginx1停掉之后,查看nginx2日志信息

  以上信息反馈出来了,当nginx1 down掉之后,nginx2立刻检测到,并启动nginx服务,保证了nginx的高可用性。


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-660246-1-1.html 上篇帖子: Know about Oracle RAC Heartbeat 下篇帖子: heartbeat v2 haresource配置高可用集群
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表