编辑推荐: |
本文主要介绍LNMP架构负载均衡的一些概念,解决高并发思路,以及对nginx的详细介绍,服务搭建,希望对您的学习有所帮助。
来自于csdn,由火龙果软件Alice编辑、推荐。 |
|
一、几个基本概念
1、pv 值
pv 值(page views):页面的浏览量
概念:一个网站的所有页面,在一天内,被浏览的总次数。(大型网站通常是上千万的级别)
2、uv值
uv值(unique visitor)独立访客
概念:一个网站,在一天内的用户访问数。(大型网站通常达10万以上)
3、独立IP
概念:一个网站,在一天内,有多少个独立的ip地址来访问。
鉴于一个局域网对外访问时都是共用同一个公网IP的,因此UV值一般要略大于独立ip值

可以通过爱站网 来查询这些参数值

二、解决高并发思路
如果一个网站的uv,pv,独立ip变大,则会导致高的并发,这时要对网站分层布局架构,采用负载均衡。
负载均衡
硬件:立竿见影,效果非常好,价格非常昂贵,比如F5-BIGIP
软件:lvs(linux virtual server) ,nginx(web服务器,负载均衡)
负载均衡实现策略
(1)轮询,
负载均衡器把请求轮流转发给后面的web服务器。
(2)ip哈希,
同一个地址的客户端,始终请求同一台主机。
(3)最少连接
负载均衡器把请求给负载最小的哪台服务器。
架构图如下:

如果并发数继续增大,还可以借助CDN来架构

三、nginx的介绍
1、常用web服务器
apache:功能完善,历史悠久,模块支持非常丰富,属于重量级产品,比较耗费内存。
缺点:处理每一个php比较费资源,导致如果高并发时会耗费服务器资源无法处理更多请求。
lighttpd:内存开销低,cpu占用率低,效能好,模块丰富等特点,轻量级web服务器。
nginx:省资源,省cpu,所以在高并发时能够处理更多的请求,高端能达到3万到5万的并发量。
IIS windows自带的web服务器软件
Nginx的两大应用:
(1) http的服务器
(2)代理服务器
正向代理

反向代理

2、选择nginx理由
(1)它可以高并发连接,官方测试能够支撑5万并发连接,在实际生产环境中可以支撑2到4万并发连接。
(2)内存消耗少
Nginx+php(FastCGI)服务器在3万并发连接下,开启的10个Nginx进程消耗150MB内存(15MB*10=150MB)开启的64个php-cgi进程消耗1280MB内存(20MB*64=1280MB)
(3)成本低廉
购买F5 BIG-IP ,NetScaler等硬件负载均衡交换机需要10多万甚至几十万人民币。而Nginx为开源软件,可以免费试用,并且可用于商业用途。

(4)配置文件非常简单:通俗易懂,即使非专业管理员也能看懂。
(5)支持 rewrite重写规则:能根据域名、URL的不同,将HTTP请求分到不同的后端服务器群组。
(6)内置的健康检查功能:如果nginx proxy后端的某台服务器宕机了,不会影响前端访问。
(7)节省带宽,支持gzip压缩。
(8)稳定性高:用于反向代理,宕机的概率微乎其微。
(9)支持热部署。在不间断服务的情况下,对软件版本升级。

nginx在反向代理,rewrite规则,稳定性,静态化文件处理,内存消耗等方面,表现出了很强的优势,选用nginx取代传统的apache
服务器,将会获得多方面的性能提升。
(8)支持的操作系统
FreeBSD 3.x,4.x,5.x,6.x i386; FreeBSD 5.x,6.x amd64;Linux
2.2,2.4,2.6 i386; Linux 2.6 amd64;Solaris 8 i386;
Solaris 9 i386 and sun4u; Solaris 10 i386;MacOS X
(10.4) PPC;
四、nginx的搭建
1、安装
安装前注意:
模块依赖性:Nginx的一些模块需要其他第三方库的支持,例如gzip模块需要zlib 库,rewrite模块需要pcre库,ssl功能需要openssl库等。
(1) 确保底层库都已安装
运行 yum install zlib* pcre* openssl*

(2) 创建一个用户和用户组
创建www组与www用户 ,理论上来讲用户与用户组的名称无所谓,但尽量要有意义
groupadd www
useradd -g www -s /usr/sbin/nologin www
(3) 编译安装nginx
第一步:下载nginx
wget


第二步:解压并进入目录
tar ….

Cd …

第三步:配置
先查看configure的配置项,并保存成一个帮助文档

将常用的配置项保存成一个安装脚本文件
Vim nginx_install.sh
内容如下:
./configure --prefix=/usr/local/nginx --user=www
--group=www --with-http_ssl_module --with-http_flv_module
--with-http_stub_status_module --with-http_gzip_static_module
--with-http_realip_module --with-http_image_filter_module
--with-pcre --http-client-body-temp-path=/usr/local/nginx/tmp/client_body_temp
--http-fastcgi-temp-path=/usr/local/nginx/tmp/fastcgi_temp
--http-proxy-temp-path=/usr/local/nginx/tmp/proxy_temp
--http-uwsgi-temp-path=/usr/local/nginx/tmp/uwsgi_temp
--http-scgi-temp-path=/usr/local/nginx/tmp/scgi_temp
给sh文件加上可执行的权限

然后执行
第四步:编译安装
make && make install
2、管理
(1)查看帮助文档
Cd /usr/local/nginx/sbin
./nginx -h

(2)启动nginx
直接执行安装目录下的sbin目录下的nginx

(3)关闭nginx
可以执行 nginx -s stop 或者 执行 pkill nginx

(4)热启动
可以执行 nginx -s reload (前提是nginx已经在运行中)

扩展:如果想用service 服务名 选项来重启或停止nginx,可以按以下步骤操作:
知识点:service 服务名 能操作成功的前提是 /etc/rc.d/init.d目录下要有与服务名同名的可执行文件

所以我们可以在这个目录下新建nginx文件
vim /etc/rc.d/init.d/nginx |
内容:
#!/bin/sh
# chkconfig: - 85 15
# description: nginx is a World Wide Web server.
It is used to serve
start() {
echo 'Starting Nginx ...'
/usr/local/nginx/sbin/nginx > /dev/null
2>&1 &
}
stop() {
echo 'Stoping Nginx ...'
/usr/local/nginx/sbin/nginx -s stop > /dev/null
2>&1 &
}
reload() {
echo 'Reloading Nginx ...'
/usr/local/nginx/sbin/nginx -s reload
}
if [ $# -ne 1 ]
then
echo 'please input one params like start|restart|stop|reload'
exit 1
fi
case "$1" in
'start')
start
;;
'stop')
stop
;;
'restart')
stop
sleep 2
start
;;
'reload')
reload
;;
'*')
echo 'please input one params like start|restart|stop|reload'
;;
esac |
记得给这个文件添加可执行权限
chmod +x /etc/rc.d/init.d/nginx |
加入到开机自启动:
chkconfig
--add nginx
chkconfig --level 345 nginx on |
五、虚拟主机配置
1、配置文件介绍
Nginx的配置文件位于安装目录下的conf目录下
可以用以下命令查看非注释部分的内容
egrep -v '#|^$' nginx.conf
-v 代表取反(去掉匹配到的内容)

在配置文件中,http段里面的server段是配置虚拟主机使用的。如果想要配置多个虚拟主机,则就在http段里面创建多个
server段即可。
注意配置文件中,每一行要使用分号结束,指令与{之间有空格。
Listen 后面可以是端口号,也可以是IP:端口号
Server_name 后面可以是域名,多个域名之间空格隔开,也可以是IP地址
在 http{}代码段里添加 client_max_body_size 200m; 以支持 php
上传大文件。(请根据自己项目需求来定值)

六、编译 php与nginx整合
1、编译安装php
(1) 第一步:安装PHP
1) 下载php
shell># cd ~
shell># wget
2) 解压并进入目录
shell># tar zxf php-5.6.24.tar.gz
shell># cd php-5.6.24
3) 将configure参数及详情解析另存为一个文件,以供学习参考用:
shell># ./configure --help > php_configure.txt
4) 编写一个辅助shell文件,帮我们配置php
shell># vim php_install.sh
为了避免写错,建议大家直接复制以下内容(都写在一行上,不要换行):
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc
--enable-fpm --enable-opcache --with-zlib-dir --with-bz2
--with-libxml-dir=/usr --with-gd --with-freetype-dir
--with-jpeg-dir --with-png-dir --enable-mbstring --with-mysql=/usr/local/mysql
--with-mysqli=/usr/local/mysql/bin/mysql_config --with-pdo-mysql=/usr/local/mysql
--with-iconv --disable-ipv6 --enable-static --enable-inline-optimization
--enable-sockets --enable-soap --with-openssl --with-curl
以上内容的解释如图

把刚刚的shell文件加上可执行权限:
shell># chmod +x ./php_install.sh
5) 执行shell文件进行软件的配置和环境检测
shell># ./php_install.sh
6) 编译软件并且进行安装
shell># make && make install
(2) 第二步:配置
1) 复制配置文件
shell># cp php.ini-production /usr/local/php/etc/php.ini
shell># cp sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm
赋予其可执行权限
shell># chmod +x /etc/rc.d/init.d/php-fpm
拷贝产生php-fpm的配置文件
shell># cd /usr/local/php/etc
shell># cp php-fpm.conf.default php-fpm.conf
2) 配置php.ini
shell># vim php.ini
· 找到;date.timezone = 修改为 date.timezone
= Asia/Shanghai
· 根据自己的需求调整以下选项的值
error_reporting = E_ALL & ~E_NOTICE & ~E_STRICT
& ~E_DEPRECATED
display_errors = On
max_execution_time = 60
max_input_time = 60
memory_limit = 256M
post_max_size = 256M
upload_max_filesize = 256M
3) 配置php-fpm.conf
shell># vim php-fpm.conf
· 找到user = nobody 和 group = nobody,将nobody改成www
· 找到listen.owner=nobody和listen.group=
nobody,将nobody改成www
4) 将php-fpm加入服务并自动启动
shell># service php-fpm start
shell># chkconfig --add php-fpm
shell># chkconfig --level 345 php-fpm on
2、配置nginx支持php
第一步:在nginx.conf中找到location ~ .php$

复制并去掉注释,将root改成你的虚拟主机的路径
保存并重载配置文件,
shell># /usr/local/nginx/sbin/nginx -s reload
在html目录下新建一个PHP文件,代码如下:

在浏览器输入php页面的网址,可以发现并不能访问

原因是官方给的默认配置文件中,

要求要把php文件放到/scripts目录下。其实这个要求我们不一定要遵从。
改进的方案如下:

保存并重载

就可以让nginx支持访问类似http://www.a.com/index.php以及http://www.a.com/index.php?id=5这样的页面了。
3、优化nginx配置文件
第二步:优化
如果把所有的server代码段都放在nginx.conf里面的话,会让nginx.conf显得又臃肿又乱。建议把它们分离去。
例如,直接把所有的server代码段都剪切到/usr/local/nginx/conf/vhost.conf里面
然后在nginx.conf里面换成
include vhost.conf

虚拟主机的代码都移到了vhost.conf里面了

至此,nginx.conf已经很优化了,我们再来优化vhost.conf
第三步:优化vhost.conf
将root语句和 index语句都提取到location外面

第四步:再一次优化vhost.conf
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
这两个语句都剪切到fastcgi.conf里面去
只留下include fastcgi.conf;

fastcgi.conf文件中新增了以下两句:

至此,nginx已经支持访问php页面,并且nginx.conf和vhost.conf已经很优化了。
只是还无法支持像thinkphp的pathinfo形式的访问
http://www.a.com/index.php/Admin/public/login
4、与ThinkPHP整合
第一步:将vhost.conf的location ~ .php$ { 的$去掉

可以发现能支持pathinfo的访问方式,但是无法识别出模块、控制器和方法

然后将location代码段写成
location ~
.php {
fastcgi_split_path_info ^(.+.php)(/.*)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
include fastcgi.conf; |

保存并重载nginx
就可以支持thinkphp的pathinfo形式的访问

第二步:优化vhost.conf
将这两句
fastcgi_split_path_info ^(.+.php)(/.*)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
都剪切到 fastcgi.conf里面,只留下include fastcgi.conf; 语句即可

第三步:开启伪静态功能
在server里面的location / { 代码段里,写上
try_files $uri /index.php$uri;
保存并重载nginx
就可以支持thinkphp的rewrite访问
http://www.a.com/Admin/public/login
七、负载均衡
1、负载均衡原理图

2、实现一个简单的负载均衡
服务器列表
本机 192.168.81.1
VM 192.168.81.6
腾讯云 123.207.231.180
负载均衡器的配置
upstream demo
{
server 192.168.81.1;
server 123.207.231.180;
}
server {
listen 80;
server_name fzjh.com;
location / {
proxy_pass http://demo;
proxy_set_header Host $host;
proxy_set_header X-Peal-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; |

并且要确保 fzjh.com要解析到负载均衡器的IP地址

保存配置文件,并且重载
访问效果:


|