第二章 Nginx虚拟主机和负载均衡
1、虚拟主机:
虚拟主机(英语:virtual hosting)或称 共享主机(shared web hosting),又称虚拟服务器,是一种在单一主机或主机群上,实现多网域服务的方法,可以运行多个网站或服务的技术。虚拟主机之间完全独立,并可由用户自行管理,虚拟并非指不存在,而是指空间是由实体的服务器延伸而来,其硬件系统可以是基于服务器群,或者单个服务器。
其技术是互联网服务器采用的节省服务器硬件成本的技术,虚拟主机技术主要应用于HTTP,FTP,EMAIL等多项服务,将一台服务器的某项或者全部服务内容逻辑划分为多个服务单位,对外表现为多个服务器,从而充分利用服务器硬件资源。如果划分是系统级别的,则称为虚拟服务器。
2、优点
2.1费用低廉
1、相对于购买独立服务器,网站建设的费用大大降低,为普及中小型网站提供了极大便利。
2、虚拟主机之间完全独立,在外界看来,每一台虚拟主机和一台独立的主机完全一样。由于多台虚拟主机共享一台真实主机的资源,每个虚拟主机用户承受的硬件费用、网络维护费用、通信线路的费用均大幅度降低。
3、许多企业建立网站都采用这种方法,这样不仅大大节省了购买机器和租用专线的费用,网站服务器管理简单,诸如软件配置、防病毒、防攻击等安全措施都由专业服务商提供,大大简化了服务器管理的复杂性;同时也不必为使用和维护服务器的技术问题担心,更不必聘用专门的管理人员。
2.2提升效率
1、网站建设初期,购买服务器到安装操作系统和应用软件需要较长的时间,而租用虚拟主机通常只需要几分钟的时间就可以开通,因为主要的注册域名查询服务商都已经实现了整个业务流程的电子商务化,选择适合自己需要的虚拟主机,在线付款之后马上就可以开通了。
2、虚拟主机技术的出现,是对Internet技术和网络发展的重大贡献,由于多台虚拟主机共享一台真实主机的资源,大大增加了服务器和通讯线路的利用率,使得一台服务器上能够毫无冲突地配置多个网络IP地址,这意味着人们能够利用虚拟主机把若干个带有单独域名的站点建置在一台服务器上,不必再为建立一个站点而购置单独的服务器和用巨资申请专线作为网络信息出口。
3、缺点
3.1功能限制
某些功能受到服务商的限制,比如可能耗用系统资源的论坛程序、流量统计功能等。网站设计需要考虑服务商提供的功能支持,比如数据库类型、操作系统等。一般虚拟主机为了降低成本没有独立IP地址,就是用IP地址不能直接访问网站(因为同一个IP地址对应有多个网站)
3.2访问速度及流量限制
1、一台服务器上的不同虚拟主机是各自独立的,并由用户自行管理。但一台服务器主机只能够支持一定数量的虚拟主机,当超过这个数量时,用户将会感到性能急剧下降。
2、某些虚拟主机网站访问速度过慢,这可能是由于主机提供商将一台主机出租给数量众多的网站,或者服务器配置等方面的原因所造成的,这种状况网站自己无法解决,对于网站的正常访问会产生不利影响;
3、有些服务商对网站流量有一定限制,这样当网站访问量较大时将无法正常访问。
3.3自主性差
虚拟主机不能由客户随意的安装软件及远程桌面等操作。
4、负载均衡
负载均衡,英文名称为Load Balance,其含义就是指将负载(工作任务)进行平衡、分摊到多个操作单元上进行运行,例如FTP服务器、Web服务器、企业核心应用服务器和其它主要任务服务器等,从而协同完成工作任务。
负载均衡构建在原有网络结构之上,它提供了一种透明且廉价有效的方法扩展服务器和网络设备的带宽、加强网络数据处理能力、增加吞吐量、提高网络的可用性和灵活性。
现有的负载均衡算法主要分为静态和动态两类。静态负载均衡算法以固定的概率分配任务,不考虑服务器的状态信息,如轮转算法、加权轮转算法等;动态负载均衡算法以服务器的实时负载状态信息来决定任务的分配,如最小连接法、加权最小连接法等。
5、负载均衡的作用
负载均衡:分摊到多个操作单元上进行执行,和它的英文名称很匹配。就是我们需要一个调度者,保证所有后端服务器都将性能充分发挥,从而保持服务器集群的整体性能最优,这就是负载均衡。
负载均衡这里面涉及的东西相对也是比较多的,理论就不说太多了,网上,书上很多,今天我们就利用Nginx服务器来实现一个简单的负载均衡
6、分类
1、轮询法
轮询法,就是将用户的请求轮流分配给服务器,就像是挨个数数,轮流分配。这种算法比较简单,他具有绝对均衡的优点,但是也正是因为绝对均衡它必须付出很大的代价,例如它无法保证分配任务的合理性,无法根据服务器承受能力来分配任务。
2、随机法
随机法,是随机选择一台服务器来分配任务。它保证了请求的分散性达到了均衡的目的。同时它是没有状态的不需要维持上次的选择状态和均衡因子[5]。但是随着任务量的增大,它的效果趋向轮询后也会具有轮询算法的部分缺点。
3、最小连接法
最小连接法,将任务分配给此时具有最小连接数的节点,因此它是动态负载均衡算法。一个节点收到一个任务后连接数就会加1,当节点故障时就将节点权值设置为0,不再给节点分配任务。
4、散列:根据请求源的 IP 的散列(hash)来选择要转发的服务器。这种方式可以一定程度上保证特定用户能连接到相同的服务器。如果你的应用需要处理状态而要求用户能连接到和之前相同的服务器,可以考虑采取这种方式。
搭建Nginx虚拟主机实验步骤如下:
实验环境:Centos7.3操作系统,
实验步骤:
搭建Nginx虚拟主机
首先卸载系统内置的Apache服务
卸载httpd保证Apache服务和Nginx服务不发生冲突
[root@localhost ~]# yum -y remove httpd
安装相关依赖包:
[root@localhost ~]# yum -y install gcc gcc-c++ proc-devel zlib-devel openssl-devel
[root@localhost ~]# useradd -M -s /sbin/nologin nginx
解压Nginx包并且安装
[root@localhost yfr]# tar zxf nginx-1.11.5.tar.gz -C /usr/src/
[root@localhost yfr]# cd /usr/src/nginx-1.11.5/ 进入解压后的Nginx目录
[root@localhost nginx-1.11.5]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-stream
上述命令中:
prefix:将httpd安装到指定目录
User:指定用户
Group:指定用户组
with-http_stub_status_module:启动HTTP模块,并且兼容HTTPD
with-stream:开启反向代理模块
[root@localhost nginx-1.11.5]# make && make install 编译安装
对nginx进行调优,并检测
[root@localhost nginx-1.11.5]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
创建软连接,程序路径并不在默认的搜索路径中,为了是该服务在使用时更加方便,可以为相关的程序添加符号连接。
[root@localhost nginx-1.11.5]# ls -l /usr/local/sbin/nginx
lrwxrwxrwx. 1 root root 27 Nov 4 02:05 /usr/local/sbin/nginx -> /usr/local/nginx/sbin/nginx
[root@localhost nginx-1.11.5]# nginx -t 检测服务配置文件是否成功
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost nginx-1.11.5]# nginx 启动nginx
[root@localhost nginx-1.11.5]# netstat -anpt | grep nginx 查看nginx启动情况
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 7583/nginx: master
启动之后来编写nginx启动脚本,并且添加到系统进程中。
[root@localhost nginx-1.11.5]# vim /etc/init.d/nginx
#!/bin/bash
#####################Welcome to nginx##################
#chkconfig:2345 99 20
#descrition:this is a nginx web server
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
case "
1
"
i
n
s
t
a
r
t
)
1" in start)
1"in start) PROG
echo “startting nginx…”
;;
stop)
kill -s QUIT $(cat $PIDF)
echo “stopping nginx…”
;;
restart)
$0 stop
$0 start
;;
reload)
kill -s HUP $(cat $PIDF)
echo “reload nginx…”
;;
*)
echo "USAGE:KaTeX parse error: Expected 'EOF', got '#' at position 96: …t nginx-1.11.5]#̲ chmod +x /etc/… {
59 # proxy_pass http://127.0.0.1;
60 #}
61 }
62 server {
63 listen 80;
64 server_name www.taobao.com;
65
66 #charset koi8-r;
67
68 #access_log logs/host.access.log main;
69
70 location / {
71 root /var/www/taobao;
72 index index.html index.htm;
73 }
74
75 #error_page 404 /404.html;
76
77 # redirect server error pages to the static page /50x.html
78 #
79 error_page 500 502 503 504 /50x.html;
80 location = /50x.html {
81 root html;
82 }
83
84 # proxy the PHP scripts to Apache listening on 127.0.0.1:80
85 #
86 #location ~ .php$ {
87 # proxy_pass http://127.0.0.1;
88 #}
89
90 # pass the PHP scripts to FastCGI server listening on 127.0.0.1:90 00
91 #
92 #location ~ .php$ {
93 # root html;
创建网页目录,配置相关的网页文件
[root@localhost nginx-1.11.5]# mkdir -p /var/www/baidu
[root@localhost nginx-1.11.5]# mkdir -p /var/www/taobao
编辑虚拟主机的网页内容
[root@localhost nginx-1.11.5]# vim /var/www/baidu/index.html
www.baidu.com
[root@localhost nginx-1.11.5]# vim /var/www/taobao/index.html
www.taobao.com
[root@localhost nginx-1.11.5]# vim /etc/hosts配置本地缓存 192.168.199.52 www.baidu.com 192.168.199.52 www.taobao.com 启动Nginx服务并且访问 [root@localhost nginx-1.11.5]# systemctl restart nginx [root@localhost nginx-1.11.5]# netstat -anpt | grep nginx tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 7386/nginx: master [root@localhost nginx-1.11.5]# firefox www.baidu.com
[root@localhost baidu]# firefox www.taobao.com
负载均衡
首先我们需要准备三台虚拟机,其中一台搭建好Nginx,并且配置好系统启动项,其余两台使用YUM工具,安装HTTPD服务
[root@localhost ~]# vim 1.sh
#!/bin/bash
yum -y remove httpd
yum -y install gcc gcc-c++ proc-devel zlib-devel openssl-devel
useradd -M -s /sbin/nologin nginx
cd /home/yfr/
tar zxf nginx-1.11.5.tar.gz -C /usr/src/
cd /usr/src/nginx-1.11.5/
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
make && make install
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
ls -l /usr/local/sbin/nginx
nginx -t
nginx
netstat -anpt | grep nginx
~
~
~
:x
[root@localhost ~]# chmod +x 1.sh 给脚本加执行权限
[root@localhost ~]# . 1.sh 执行脚本
启动之后来编写nginx启动脚本,并且添加到系统进程中。
[root@localhost nginx-1.11.5]# vim /etc/init.d/nginx
#!/bin/bash
#####################Welcome to nginx##################
#chkconfig:2345 99 20
#descrition:this is a nginx web server
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
case "
1
"
i
n
s
t
a
r
t
)
1" in start)
1"in start) PROG
echo “startting nginx…”
;;
stop)
kill -s QUIT $(cat $PIDF)
echo “stopping nginx…”
;;
restart)
$0 stop
$0 start
;;
reload)
kill -s HUP $(cat $PIDF)
echo “reload nginx…”
;;
*)
echo “USAGE:$0 { start | stop | restart | reload }”
exit 1
esac
exit 0
[root@localhost nginx-1.11.5]# chmod +x /etc/init.d/nginx 给启动脚本加执行权限
[root@localhost nginx-1.11.5]# chkconfig --add nginx 添加Nginx启动脚本到系统进程中
[root@localhost nginx-1.11.5]# chkconfig nginx on 启动Nginx这个进程
[root@localhost nginx-1.11.5]# chkconfig --list nginx 查看Nginx的启动权限
Note: This output shows SysV services only and does not include native
systemd services. SysV configuration data might be overridden by native
systemd configuration.
If you want to list systemd services use ‘systemctl list-unit-files’.
To see services enabled on particular target use
‘systemctl list-dependencies [target]’.
nginx 0:off 1:off 2:on 3:on 4:on 5:on 6:off
剩下两台虚拟机安装HTTPD,并且去网页根目录,更改网页
[root@localhost ~]# yum -y install httpd
首先去第一台HTTPD服务器上去配置,并且启动
[root@localhost ~]# vim /var/www/html/index.html
This is httpd-server1
[root@localhost ~]# systemctl start httpd [root@localhost ~]# systemctl stop firewalld [root@localhost ~]# netstat -anpt | grep httpd tcp6 0 0 :::80 :::* LISTEN 14214/httpd 第二台HTTPD服务器上去配置 [root@localhost ~]# vim /var/www/html/index.html
This is httpd-server2
[root@localhost ~]# systemctl start httpd [root@localhost ~]# systemctl stop firewalld [root@localhost ~]# netstat -anpt | grep httpd tcp6 0 0 :::80 :::* LISTEN 14638/httpd 回到第一台服务器上去配置Nginx负载均衡的配置项 [root@localhost nginx-1.11.5]# vim /usr/local/nginx/conf/nginx.conf 33 #gzip on; 34 upstream web { 在这里配置相关的IP以及轮询 35 server 192.168.199.53:80 weight=5; 36 server 192.168.199.54:80; 37 } 38 server { 39 listen 80; 40 server_name localhost; 41 42 #charset koi8-r; 43 44 #access_log logs/host.access.log main; 45 46 location / { 47 proxy_pass http://web; 48 index index.html index.htm; 49 } 50 [root@localhost nginx-1.11.5]# nginx -t 检测配置文件是否出现问题 nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful 启动Nginx服务并且验证结果 [root@localhost nginx-1.11.5]# systemctl restart nginx 重启nginx服务 [root@localhost nginx-1.11.5]# netstat -anpt | grep nginx tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 7370/nginx: master [root@localhost nginx-1.11.5]# firefox 192.168.199.52 可以看到这里出现的是第一台HTTPD服务器的内容
使用F5刷新之后,就会变成第二个HTTPD服务器的内容