加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

单一Nginx负载均衡+LNMP分布式架构

发布时间:2020-12-13 20:01:34 所属栏目:百科 来源:网络整理
导读:随着互联网的快速发展,我们的web站点访问量和数据流量的快速增长,对于我们服务器的处理能里的要求也越来越高,这样的情况下,单一的服务器根本无法承受,这样的话我们丢弃掉原有的设备,做硬件升级,会造成成本的浪费,如果再过一段时间,升级过后的硬件有负载不起

随着互联网的快速发展,我们的web站点访问量和数据流量的快速增长,对于我们服务器的处理能里的要求也越来越高,这样的情况下,单一的服务器根本无法承受,这样的话我们丢弃掉原有的设备,做硬件升级,会造成成本的浪费,如果再过一段时间,升级过后的硬件有负载不起了,怎么办呢?没关系,我们有负载均衡的技术,就不用担心了!

负载均衡通俗点说,就是一堆的计算机,或设备,同时为用户提供一个相同的服务,下面就来说说怎么实现的!

单一nginx负载均衡,见下图,

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1,首先用户发送请求访问bbs.andy.com,
2,当前端nginx负载均衡服务器(上图中的SVR1)收到用户的请求之后,nginx负载均衡器,会根据此前配置好的调度算法,代用户请求后端的应用程序服务器,
3,应用程序服务器(也就是上图中的SVR2,3,4) 收到前端nginx负载均衡器的请求的时候,它并不知道是nginx负载均衡器是代用户请求的,对于后端的应用程序服务器而言前端nginx负载均衡器就是一个用户,那么它收到请求之后,将对应请求的处理结果,再返回给前端nginx负载均衡器,
4,当前端的nginx负载均衡器,收到后端应用程序服务器返回的响应内容之后再讲结果返回给用户,
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
应该是这样工作的吧,个人的理解,呵呵,说的不好,还望大家伙指点,下面来说说LNMP分布式架构是怎么工作的,也就是上图啦,待会的配置也是根据上图的架构来配置的,
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
LNMP分布式架构的工作原理,根据上图来,
1,用户请求bbs.andy.com
2,前端nginx负载均衡器收到用户请求,根据此前设定的调度算法,假如说此时前端nginx负载均衡器,根据调度算法的结果,应该访问svr2,这台应用程序服务器,那么svr2这台服务器根据用户请求的内容的不同,而进行不同的处理机制,根据上图的架构一般有两种可能,
当用户请求的内容是一个静态的html页面的话,那么svr2,就直接将请求的结果响应给前端nginx负载均衡器,
当用户请求的内容是一个动态的页面内容(在上图中也就是PHP页面了)那么svr2会去找svr4上面的FastCGI程序来帮忙解析动态php页面,如果此次页面请求中需要访问数据库的话,fastcgi会通过mysql的接口访问数据库的,然后将其请求的结果,返回给svr2,
当svr2收到返回的请求结果后,svr2再将结果返回给前端的nginx负载均衡器,当前端的nginx负载均衡器收到请求的结果后,再将其返回给用户,
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
下面来配置下在nginx负载均衡的情况下,后端的LNMP应用服务器分布式架构,下面再简单介绍下上图中每台服务器的角色,已经IP地址,等信息,

服务器 角色 IP地址 DNS能解析的域名
SVR1 nginx负载均衡器 192.168.0.52/172.16.0.1 bbs.andy.com解析到192.168.0.52
SVR2 nginx 172.16.0.2
SVR3 nginx/NFS 172.16.0.3
SVR4 php(FastCGI)/MySQL 172.16.0.4
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
SVR1负责接受用户请求,根据调度算法,负载到SVR2 SVR3上面去,
SVR3上面有个NFS服务,是提供共享存储的,这里主要是共享网站的源码
SVR4 php(FastCGI)提供SVR2 SVR3的动态PHP页面的请求,
SVR4 MYSQL数据库提供SVR4上的PHP(FastCGI)的数据请求等,



一,安装配置SVR4
二,安装配置SVR3
三,安装配置SVR2
四,安装配置SVR1
注:配置过程中需要用到的源码包,自己在官方下载,所有包下载至系统的/usr/src目录
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1.安装基本开发库,等依赖软件包

 
 
  1. #yum-ygroupinstall"DevelopmentLibraries""DevelopmentTools""XSoftwareDevelopment"

2.安装配置mysql
2.1 编译安装mysql

 
 
  1. #cd/usr/src
  2. #tarxzvfmysql-5.0.56.tar.gz
  3. #cdmysql-5.0.56
  4. #./configure--prefix=/usr/local/mysql--sysconfdir=/usr/local/mysql/etc--with-ssl
  5. --localstatedir=/usr/local/mysql/database--enable-assembler--with-readline
  6. --with-extra-charsets=complex--enable-thread-safe-client--with-big-tables
  7. --with-embedded-server--enable-local-infile--with-plugins=innobase
  8. #make&&makeinstall

2.2 将mysql命令加入到系统搜索路径

 
 
  1. #vim/etc/profile
  2. PATH=/usr/local/mysql/bin:$PATH将次行添加到文件中的44行
  3. #exportPATH=/usr/local/mysql/bin:$PATH

2.3 将头文件添加到系统搜索路径

 
 
  1. #ln-s/usr/local/mysql/include/mysql/usr/include/mysql

2.4 将库文件添加到系统搜索路径

 
 
  1. #echo“/usr/local/mysql/lib/mysql”> /etc/ld.so.conf.d/mysql.conf
  2. #ldconfig

2.5 为mysql提供配置文件,服务启动脚本,用户,并初始化mysql

 
 
  1. #cpsupport-files/my-huge.cnf/etc/my.cnf
  2. #cpsupport-files/mysql.server/etc/init.d/mysqld
  3. #chmoda+x/etc/init.d/mysqld
  4. #useradd-s/sbin/nologinmysql
  5. #chown-Rmysql:mysql/usr/local/mysql
  6. #mysql_install_db--user=mysql
  7. #chkconfig--addmysqld
  8. #chkconfigmysqldon
  9. #servicemysqldrestart

3,安装配置php(FastCGI)
3.1 编译安装libiconv 编码转换库

 
 
  1. #cd/usr/src
  2. #tarxzvflibiconv-1.13.1.tar.gz
  3. #cdlibiconv-1.13.1
  4. #./configure--prefix=/usr/local
  5. #make&&makeinstall

3.2 编译安装libmcryp加密算法扩展库

 
 
  1. #cd/usr/src
  2. #tarxjvflibmcrypt-2.5.8.tar.bz2
  3. #cdlibmcrypt-2.5.8
  4. #./configure
  5. #make
  6. #makeinstall
  7. #ldconfig
  8. #cdlibltdl/
  9. #./configure--enable-ltdl-install
  10. #make&& makeinstall

3.3 编译安装mhash加密算法扩展库

 
 
  1. #cd/usr/src
  2. #tarxjvfmhash-0.9.9.9.tar.bz2
  3. #cdmhash-0.9.9.9
  4. #./configure
  5. #make&& makeinstall
  6. #ln-s/usr/local/lib/libmcrypt.la/usr/lib/libmcrypt.la
  7. #ln-s/usr/local/lib/libmcrypt.so/usr/lib/libmcrypt.so
  8. #ln-s/usr/local/lib/libmcrypt.so.4/usr/lib/libmcrypt.so.4
  9. #ln-s/usr/local/lib/libmcrypt.so.4.4.8/usr/lib/libmcrypt.so.4.4.8
  10. #ln-s/usr/local/lib/libmhash.a/usr/lib/libmhash.a
  11. #ln-s/usr/local/lib/libmhash.la/usr/lib/libmhash.la
  12. #ln-s/usr/local/lib/libmhash.so/usr/lib/libmhash.so
  13. #ln-s/usr/local/lib/libmhash.so.2/usr/lib/libmhash.so.2
  14. #ln-s/usr/local/lib/libmhash.so.2.0.1/usr/lib/libmhash.so.2.0.1

3.4 编译安装mcrypt 加密算法工具

 
 
  1. #cd/usr/src
  2. #tarxzvfmcrypt-2.6.8.tar.gz
  3. #cdmcrypt-2.6.8
  4. #./configure
  5. #make&&makeinstall

3.5 编译安装php(FastCGI) 这里使用的是5.4的版本,5.4的版本不需要打fpm补丁就支持FastCGI
3.5.1 编译安装php

 
 
  1. #cd/usr/src
  2. #tarxjvfphp-5.4.4.tar.bz2
  3. #cdphp-5.4.4
  4. #./configure--prefix=/usr/local/php--with-mysql=/usr/local/mysql/--with-openssl
  5. --enable-fpm--enable-sockets--enable-sysvshm--with-mysqli=/usr/local/mysql/bin/mysql_config
  6. --enable-mbstring--with-freetype-dir--with-jpeg-dir--with-png-dir--with-zlib-dir
  7. --with-libxml-dir=/usr/--enable-xml--with-mhash--with-mcrypt--with-config-file-path=/etc
  8. --with-config-file-scan-dir=/etc/php.d--with-bz2--with-curl--with-ldap--with-iconv-dir
  9. #makeZEND_EXTRA_LIBS='-liconv'
  10. #makeinstall

3.5.2 为php-fpm提供配置文件,服务启动脚本等,

 
 
  1. #cpphp.ini-production/etc/php.ini
  2. #cpsapi/fpm/init.d.php-fpm/etc/rc.d/init.d/php-fpm
  3. #chmod+x/etc/rc.d/init.d/php-fpm
  4. #chkconfig--addphp-fpm
  5. #chkconfigphp-fpmon
  6. #cp/usr/local/php/etc/php-fpm.conf.default/usr/local/php/etc/php-fpm.conf
  7. #vim/usr/local/php/etc/php-fpm.conf修改内容如下
  8. pid=/usr/local/php/var/run/php-fpm.pid
  9. listen = 172.16.0.4:9000
  10. pm.max_children=50
  11. pm.start_servers=5
  12. pm.min_spare_servers=2
  13. pm.max_spare_servers=8
  14. #servicephp-fpmstart
 
 
  1. #yumgroupinstall"DevelopmentLibraries""DevelopmentTools"
  2. #yum-yinstallpcre-devel

2.安装配置nginx
2.1 编译安装nginx

 
 
  1. #useradd-s/sbin/nologinnginx
  2. #cd/usr/src
  3. #tarxzvfnginx-1.2.2.tar.gz
  4. #cdnginx-1.2.2
  5. #./configure--prefix=/usr/local/nginx--pid-path=/var/run/nginx/nginx.pid
  6. --lock-path=/var/lock/nginx.lock--user=nginx--group=nginx--with-http_ssl_module
  7. --with-http_flv_module--with-http_stub_status_module--with-http_gzip_static_module
  8. --http-client-body-temp-path=/var/tmp/nginx/client/--http-proxy-temp-path=/var/tmp/nginx/proxy
  9. --http-fastcgi-temp-path=/var/tmp/nginx/fcgi--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi
  10. --http-scgi-temp-path=/var/tmp/nginx/scgi--with-pcre
  11. #make&& makeinstall

2.2 为nginx提供服务启动脚本

 
 
  1. #vim/etc/rc.d/init.d/nginx内容如下
  2. #!/bin/sh
  3. #
  4. #nginx-thisscriptstartsandstopsthenginxdaemon
  5. #
  6. #chkconfig:-8515
  7. #description:NginxisanHTTP(S)server,HTTP(S)reverse
  8. #proxyandIMAP/POP3proxyserver
  9. #processname:nginx
  10. #config:/etc/nginx/nginx.conf
  11. #config:/etc/sysconfig/nginx
  12. #pidfile:/var/run/nginx.pid
  13. #Sourcefunctionlibrary.
  14. ./etc/rc.d/init.d/functions
  15. #Sourcenetworkingconfiguration.
  16. ./etc/sysconfig/network
  17. #Checkthatnetworkingisup.
  18. ["$NETWORKING"="no"]&&exit0
  19. nginx="/usr/local/nginx/sbin/nginx"
  20. prog=$(basename$nginx)
  21. NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
  22. [-f/etc/sysconfig/nginx]&&./etc/sysconfig/nginx
  23. lockfile=/var/lock/subsys/nginx
  24. make_dirs(){
  25. #makerequireddirectories
  26. user=`nginx-V2>&1|grep"configurearguments:"|sed's/[^*]*--user=([^]*).*/1/g'-`
  27. options=`$nginx-V2>&1|grep'configurearguments:'`
  28. foroptin$options;do
  29. if[`echo$opt|grep'.*-temp-path'`];then
  30. value=`echo$opt|cut-d"="-f2`
  31. if[!-d"$value"];then
  32. #echo"creating"$value
  33. mkdir-p$value&&chown-R$user$value
  34. fi
  35. fi
  36. done
  37. }
  38. start(){
  39. [-x$nginx]||exit5
  40. [-f$NGINX_CONF_FILE]||exit6
  41. make_dirs
  42. echo-n$"Starting$prog:"
  43. daemon$nginx-c$NGINX_CONF_FILE
  44. retval=$?
  45. echo
  46. [$retval-eq0]&&touch$lockfile
  47. return$retval
  48. }
  49. stop(){
  50. echo-n$"Stopping$prog:"
  51. killproc$prog-QUIT
  52. retval=$?
  53. echo
  54. [$retval-eq0]&&rm-f$lockfile
  55. return$retval
  56. }
  57. restart(){
  58. configtest||return$?
  59. stop
  60. sleep1
  61. start
  62. }
  63. reload(){
  64. configtest||return$?
  65. echo-n$"Reloading$prog:"
  66. killproc$nginx-HUP
  67. RETVAL=$?
  68. echo
  69. }
  70. force_reload(){
  71. restart
  72. }
  73. configtest(){
  74. $nginx-t-c$NGINX_CONF_FILE
  75. }
  76. rh_status(){
  77. status$prog
  78. }
  79. rh_status_q(){
  80. rh_status>/dev/null2>&1
  81. }
  82. case"$1"in
  83. start)
  84. rh_status_q&&exit0
  85. $1
  86. ;;
  87. stop)
  88. rh_status_q||exit0
  89. $1
  90. ;;
  91. restart|configtest)
  92. $1
  93. ;;
  94. reload)
  95. rh_status_q||exit7
  96. $1
  97. ;;
  98. force-reload)
  99. force_reload
  100. ;;
  101. status)
  102. rh_status
  103. ;;
  104. condrestart|try-restart)
  105. rh_status_q||exit0
  106. ;;
  107. *)
  108. echo$"Usage:$0{start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
  109. exit2
  110. esac
  111. #chmoda+x/etc/init.d/nginx
  112. #chkconfig--addnginx
  113. #chkconfignginxon

2.3 编辑nginx的主配置文件

 
 
  1. #vim/usr/local/nginx/conf/nginx.conf内容如下
  2. usernginx;
  3. worker_processes10;
  4. error_loglogs/error.logcrit;
  5. pidlogs/nginx.pid;
  6. events
  7. {
  8. useepoll;
  9. worker_connections51000;
  10. }
  11. http{
  12. includemime.types;
  13. default_typeapplication/octet-stream;
  14. client_header_buffer_size32k;
  15. large_client_header_buffers432k;
  16. client_max_body_size10m;
  17. sendfileon;
  18. tcp_nopushon;
  19. keepalive_timeout60;
  20. tcp_nodelayon;
  21. fastcgi_connect_timeout300;
  22. fastcgi_send_timeout300;
  23. fastcgi_read_timeout300;
  24. fastcgi_buffer_size64k;
  25. fastcgi_buffers464k;
  26. fastcgi_busy_buffers_size128k;
  27. fastcgi_temp_file_write_size128k;
  28. gzipon;
  29. gzip_min_length1k;
  30. gzip_buffers416k;
  31. gzip_http_version1.0;
  32. gzip_comp_level2;
  33. gzip_typestext/plainapplication/x-javascripttext/ccsapplication/xml;
  34. gzip_varyon;
  35. server{
  36. listen80;
  37. server_name172.16.0.3;
  38. indexindex.htmlindex.htmindex.php;
  39. root/web/bbs;
  40. location~.*.(php|php5)?$
  41. {
  42. fastcgi_pass172.16.0.4:9000;
  43. fastcgi_indexindex.php;
  44. fastcgi_paramSCRIPT_FILENAME/web/bbs$fastcgi_script_name;
  45. includefastcgi.conf;
  46. }
  47. access_loglogs/bbs.log;
  48. }
  49. }

2.4 创建网站根目录以及测试文件

 
 
  1. #mkdir-pv/web/bbs
  2. #vim/web/bbs/index.php
  3. <h1>test</h1>
  4. <?php
  5. phpinfo();
  6. ?>

2.5 启动nginx服务

 
 
  1. #servicenginxrestart


3,配置NFS

 
 
  1. #vim/etc/exports
  2. /web172.16.0.*(rw,no_root_squash,sync)
  3. #chkconfigportmapon
  4. #chkconfignfson
  5. #serviceportmaprestart
  6. #servicenfsrestart


三,安装配置SVR2

1.安装基本开发库,及依赖软件包

 
 
  1. #yumgroupinstall"DevelopmentLibraries""DevelopmentTools"
  2. #yum-yinstallpcre-devel

  1. #useradd-s/sbin/nologinnginx
  2. #cd/usr/src
  3. #tarxzvfnginx-1.2.2.tar.gz
  4. #cdnginx-1.2.2
  5. #./configure--prefix=/usr/local/nginx--pid-path=/var/run/nginx/nginx.pid
  6. --lock-path=/var/lock/nginx.lock--user=nginx--group=nginx--with-http_ssl_module
  7. --with-http_flv_module--with-http_stub_status_module--with-http_gzip_static_module
  8. --http-client-body-temp-path=/var/tmp/nginx/client/--http-proxy-temp-path=/var/tmp/nginx/proxy
  9. --http-fastcgi-temp-path=/var/tmp/nginx/fcgi--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi
  10. --http-scgi-temp-path=/var/tmp/nginx/scgi--with-pcre
  11. #make&& makeinstall

  1. 见 二.2.2 的配置内容

  1. #vim/usr/local/nginx/conf/nginx.conf内容如下
  2. usernginx;
  3. worker_processes10;
  4. error_loglogs/error.logcrit;
  5. pidlogs/nginx.pid;
  6. events
  7. {
  8. useepoll;
  9. worker_connections51000;
  10. }
  11. http{
  12. includemime.types;
  13. default_typeapplication/octet-stream;
  14. client_header_buffer_size32k;
  15. large_client_header_buffers432k;
  16. client_max_body_size10m;
  17. sendfileon;
  18. tcp_nopushon;
  19. keepalive_timeout60;
  20. tcp_nodelayon;
  21. fastcgi_connect_timeout300;
  22. fastcgi_send_timeout300;
  23. fastcgi_read_timeout300;
  24. fastcgi_buffer_size64k;
  25. fastcgi_buffers464k;
  26. fastcgi_busy_buffers_size128k;
  27. fastcgi_temp_file_write_size128k;
  28. gzipon;
  29. gzip_min_length1k;
  30. gzip_buffers416k;
  31. gzip_http_version1.0;
  32. gzip_comp_level2;
  33. gzip_typestext/plainapplication/x-javascripttext/ccsapplication/xml;
  34. gzip_varyon;
  35. server{
  36. listen80;
  37. server_name172.16.0.2;
  38. indexindex.htmlindex.htmindex.php;
  39. root/web/bbs;
  40. location~.*.(php|php5)?$
  41. {
  42. fastcgi_pass172.16.0.4:9000;
  43. fastcgi_indexindex.php;
  44. fastcgi_paramSCRIPT_FILENAME/web/bbs$fastcgi_script_name;
  45. includefastcgi.conf;
  46. }
  47. access_loglogs/bbs.log;
  48. }
  49. }

3.重启nginx服务

 
4.使用NFS 共享的网站目录,以达到共享存储的目的

 
 
  1. #mkdir /web
  2. #mount-tnfs172.16.0.3:/web/web
  3. #echo "mount-tnfs172.16.0.3:/web/web" >> /etc/rc.local

四.安装配置SVR1
1. 安装基本开发库,以及依赖的软件包

 2.安装配置nginx

 
 
  1. useradd-s/sbin/nologinnginx
  2. #cd/usr/src
  3. #tarxzvfnginx-1.2.2.tar.gz
  4. #cdnginx-1.2.2
  5. #./configure--prefix=/usr/local/nginx--pid-path=/var/run/nginx/nginx.pid
  6. --lock-path=/var/lock/nginx.lock--user=nginx--group=nginx--with-http_ssl_module
  7. --with-http_flv_module--with-http_stub_status_module--with-http_gzip_static_module
  8. --http-client-body-temp-path=/var/tmp/nginx/client/--http-proxy-temp-path=/var/tmp/nginx/proxy
  9. --http-fastcgi-temp-path=/var/tmp/nginx/fcgi--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi
  10. --http-scgi-temp-path=/var/tmp/nginx/scgi--with-pcre
  11. #make&&makeinstall

3.为nginx提供服务启动脚本

 
 
  1. 见二.2.2的配置内容

4.编辑nginx主配置文件,配置负载均衡

 
 
  1. #vim/usr/local/nginx/conf/nginx.conf
  2. usernginx;
  3. worker_processes10;
  4. error_loglogs/error.logcrit;
  5. pidlogs/nginx.pid;
  6. events
  7. {
  8. useepoll;
  9. worker_connections51000;
  10. }
  11. http{
  12. includemime.types;
  13. default_typeapplication/octet-stream;
  14. keepalive_timeout60;
  15. tcp_nodelayon;
  16. #指定负载均衡的方式
  17. upstreambbs.andy.com{
  18. server172.16.0.2:80;
  19. server172.16.0.3:80;
  20. ip_hash;
  21. }
  22. server{
  23. listen80;
  24. server_namebbs.andy.com;
  25. indexindex.htmlindex.htmindex.php;
  26. location/{
  27. proxy_passhttp://bbs.andy.com;
  28. proxy_next_upstreamhttp_502http_504errortimeoutinvalid_header;
  29. proxy_set_headerHost$host;
  30. proxy_set_headerX-Real-IP$remote_addr;
  31. proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for;
  32. proxy_connect_timeout600;
  33. proxy_read_timeout600;
  34. proxy_send_timeout600;
  35. proxy_buffer_size8k;
  36. proxy_temp_file_write_size64k;
  37. }
  38. access_loglogs/bbs.log;
  39. }
  40. }

  1. #servicenginxrestart


五,收尾配置,需要在每台服务器上执行以下命令

 
 
  1. #chkconfigiptablesoff
  2. #serviceiptablesstop
  3. #setenforce0
  4. #echo"setenforce0">>/etc/rc.local

2.需要在SVR4上挂载nfs共享

 
 
  1. #mkdir/web
  2. #mount-tnfs172.16.0.3:/web/web
  3. #echo"mount-tnfs172.16.0.3:/web/web">>/etc/rc.local

OK.到这里配置就结束了。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读