文章目录
- gitee地址:大型网站高并发处理
大型网站高并发处理传送门:
Nginx的高并发处理
1.为什么需要Nginx?
比如:淘宝双11,在1秒中就会有上百万的并发。在做web项目时,通常通过Tomcat来做,在进程里启动线程的方式。服务器需要处理线程的创建与销毁、线程调度,这些都需要消耗一定的资源。所以,经过测试单个Tomcat支持最高并发如下:
怎么解决高并发问题,解决单个服务器过载问题?
答:Nginx起到反向代理的功能,实现负载均衡,解决高并发问题。通过轮询将请求转发到其他服务器节点上。Nginx可以做到50000左右的并发。
转发:
- 建立握手连接
- 接收请求.分析url
- 转发给对应的后端服务器
- 等待后端的响应
2.Nginx概述
2.1 Nginx介绍
-
Nginx (“engine x”) 是一个高性能的 静态 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器。
-
第一个公开版本 0.1.0 发布于 2004 年 10 月 4 日。
-
其将源代码以类 BSD 许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名
-
官方测试 nginx 能够支撑 5 万并发链接,并且 cpu、内存等资源消耗却非常低,运行非常稳定
-
2011 年 6 月 1 日,nginx 1.0.4 发布。apache httpd
-
Nginx 是一款轻量级的 Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个 BSD-like 协议下发行。由俄罗斯的程序设计师 Igor Sysoev 所开发,其特点是占有内存少,并发能力强,事实上 nginx 的并发能力确实在同类型的网页服务器中表现较好,中国大陆使用 nginx 网站用户有:新浪、网易、腾讯等。
2.2 Nginx和apache的优缺点
2.2.1 Nginx相对于apache的优点
-
轻量级,同样起 web 服务,比 apache 占用更少的内存及资源
-
抗并发 , nginx 处 理请求是异步非阻塞 ( 可 参 考 文 章 https://zhuanlan.zhihu.com/p/82935440)的,而 apache 则是阻塞型的, 在高并发下 nginx 能保持低资源低消耗高性能 NIO netty NIO
-
高度模块化的设计,编写模块相对简单
-
社区活跃,各种高性能模块出品迅速
2.2.2 apache相对于Nginx的优点
-
rewrite ,比 nginx 的 rewrite 强大
-
模块超多,基本想到的都可以找到
-
少 bug ,nginx 的 bug 相对较多
-
Nginx 配置简洁, Apache 复杂
-
最核心的区别在于apache是同步多进程模型,一个连接对应一个进程中线程;nginx是异步非阻塞的,多个连接(万级别)可以对应一个进程
2.2.3 Tengine介绍
-
tengine 网址:http://tengine.taobao.org/
Tengine 是由淘宝网发起的 Web 服务器项目。它在 Nginx 的基础上,针对大访问量网站的需求,添加了很多高级功能和特性。Tengine 的性能和稳定性已经在大型的网站如淘宝网,天猫商城等得到了很好的检验。它的最终目标是打造一个高效、稳定、安全、易用的Web 平台
从2011年12月开始,Tengine 成为一个开源项目,Tengine 团队在积极地开发和维护着它。Tengine 团队的核心成员来自于淘宝、搜狗等互联网企业。Tengine 是社区合作的成果,我们欢迎大家参与其中,贡献自己的力量
tengine和nginx性能测试 : http://tengine.taobao.org/document_cn/benchmark_cn.html
-
Tengine 相比 Nginx 默认配置,提升 200%的处理能力。
-
Tengine 相比 Nginx 优化配置,提升 60%的处理能力。
tengine 更详细参数解释详见: http://tengine.taobao.org/nginx_docs/cn/docs/
3. Nginx的下载与安装
3.1 Nginx下载
官网下载 Nginx 软件 http://nginx.org 。Nginx 官方提供了三个类型的版本:
-
Mainline Version:主线版,是最新版,但未经过过多的生产测试。
-
Stable Version:稳定版,生产环境使用版本。
-
Legacy Version:老版本。
我们需要下载的是 Stable Version。其中又分为两种版本:Linux 版与 Windows 版。 开发时这两个版本我们都下载。Linux 版用于生产环境,而 Windows 版用于开发测试。
3.2 Nginx的源码安装
3.2.1 安装前的准备工作
克隆一个没有安装其它软件的纯净主机。名为:nginx1。完成以下配置:
-
修改主机名
vim /etc/sysconfig/network
-
修改网络配置
vim /etc/sysconfig/network-scripts/ifcfg-eth0
3.2.2 安装Nginx
-
安装源码编译以及 Nginx 依赖的库
[root@nginx1 ~]# yum install gcc gcc-c++ pcre pcre-devel openssl openssl-devel zlib zlib-devel -y
-
创建存放源文件的文件夹
首先在目录/opt 下创建 apps 目录,用于存放源文件以及解压后的文件
[root@nginx1 ~]# mkdir /opt/apps/
-
上传 Nginx 到 2 创建的目录
-
解压 Nginx
[root@nginx1 apps]# pwd /opt/apps [root@nginx1 apps]# ls nginx-1.16.1.tar.gz [root@nginx1 apps]# tar -zxvf nginx-1.16.1.tar.gz
进入到/opt/apps 目录中的 Nginx 解压包目录,查看 Nginx 的目录。 其中各个目录中存放的文件作用为:
- auto:存放 Nginx 自动安装的相关文件
- conf:存放 Nginx 服务器配置文件
- configure:命令,用于对即将安装的软件的配置,完成 makefile 编译文件的生成
- contrib:存放由其他机构贡献的文档材料
- html:存放 Nginx 欢迎页面
- man:manual,手册,存放 Nginx 帮助文档
- src:存放 Nginx 源码
-
生成 makefile
在 Nginx 解压目录下运行 make 命令,用于完成编译。但此时会给出提示:没有指定目标,并且没有发现编译文件 makefile。编译命令 make 需要根据编译文件makefile 进行编译,所以在编译之前需要先生成编译文件 makefile。使用 configure命令可以生成该文件。那么,configure 命令需要配置些什么参数呢?使用
--help
可以查看到可以使用的参数说明。这些参数可以分为三类:- 第一类:基本信息的配置
- 第二类:默认没有安装,可以指定安装的模块,使用–with开头。Nginx 的高扩展性就体现在这里
- 第三类:默认已经安装,可以指定卸载的模块,使用–without开头
下面是简单配置的命令执行。命令中每一行的最后添加了反斜杠\表示当前命令并未结束,回车不会执行该命令。执行成功后,会给出配置报告。下面以安装对 https 访问协议支持的模块 http_ssl_module 为例。
--prefix
:用于指定 nginx 的安装目录。注意,安装目录与解压目录不一样--http_ssl_module
:https 访问协议需要安装 Http 安全连接协议模块 SSL(Secure SocketsLayer,安全套接层)。注意,在执行过 configure 命令后并不会立即生成/usr/local/nginx 目录,也不会马上开始安装指定的模块,而仅仅是将命令中指定的参数及默认配置写入到即将要生成的 Makefile 文件中
配置报告以两部分构成:第一部分给出了配置的系统库;第二部分给出了系统配置信息
- path prefix:Nginx 安装目录
- binary file:Nginx 命令文件
- modules path:Nginx 模块存放路径
- configuration prefix:Nginx 配置文件存放路径
- configuration file:Nginx 配置文件名
- pid file:Nginx 的进程 id 文件
- error log file:错误日志文件
- http access log file:http 访问日志文件;
- http xxx:其它 http 请求相关的文件。
配置成功后,再次查看 Nginx 解压目录,发现其中多出了一个文件 Makefile。后面的编译就是依靠该文件进行的。
复杂安装:
注意: /var/tmp/nginx/client目录需要手动创建,并且需要创建nginx用户与用户组
[root@nginx1 nginx-1.16.1]# mkdir –p /var/tmp/nginx/client [root@nginx1 nginx-1.16.1]# pwd /opt/apps/nginx-1.16.1 [root@nginx1 nginx-1.16.1]# ./configure \ --prefix=/opt/nginx \ --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 \ 10 --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
简单安装:使用简单安装,指定安装目录和 https 访问支持
[root@nginx1 nginx-1.16.1]# ./configure --prefix=/opt/nginx --with-http_ssl_module --with-http_gzip_static_module --error-log-path=/var/log/nginx/nginx.log --pid-path=/var/log/nginx/pid
-
编译安装
这是两个命令,make 为编译命令,make install 为安装命令,可以分别执行。这里使用&&将两个命令连接执行,会在前面命令执行成功的前提下才会执行第二个命令
[root@nginx1 nginx-1.16.1]# make && make install
-
nginx命令随处可用
在 Nginx 的安装目录/opt/nginx 中有一个 sbin 目录,其中存放着 nginx 的命令程序 nginx。默认情况下,若要使用 nginx 命令,则必须要在/opt/nginx/sbin 目录中,或指定命令路径,使用起来很不方便。
[root@nginx1 ~]# cd /opt/nginx/ [root@nginx1 nginx]# ls conf html logs sbin [root@nginx1 nginx]# cd sbin/ [root@nginx1 sbin]# ls nginx [root@nginx1 sbin]# ./nginx [root@nginx1 sbin]# ps aux|grep nginx root 1131 0.0 0.1 47332 1196 ? Ss 11:31 0:00 nginx: master process ./nginx nobody 1132 0.0 0.1 47772 1784 ? S 11:31 0:00 nginx: worker process root 1134 0.0 0.0 103256 864 pts/0 S+ 11:31 0:00 grep nginx [root@nginx1 sbin]# kill -9 1131 1132 [root@nginx1 sbin]# ps aux|grep nginx root 1136 0.0 0.0 103256 864 pts/0 S+ 11:32 0:00 grep nginx
为了能够在任意目录下均可直接执行 nginx 命令,通过下面的方式来完成。
-
添加安装的 nginx 到服务列表:将如下内容添加到/etc/init.d/nginx 脚 本中,nginx 需要具有可执行权限。
[root@nginx1 nginx-1.16.1]# vim /etc/init.d/nginx #!/bin/sh # # nginx - this script starts and stops the nginx daemon # # chkconfig: - 85 15 # description: Nginx is an HTTP(S) server, HTTP(S) reverse \ # proxy and IMAP/POP3 proxy server # processname: nginx # config: /etc/nginx/nginx.conf # config: /etc/sysconfig/nginx # pidfile: /var/run/nginx.pid # Source function library. . /etc/rc.d/init.d/functions # Source networking configuration. . /etc/sysconfig/network # Check that networking is up. [ "$NETWORKING" = "no" ] && exit 0 nginx="/opt/nginx/sbin/nginx" prog=$(basename $nginx) NGINX_CONF_FILE="/opt/nginx/conf/nginx.conf" [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx lockfile=/var/lock/subsys/nginx make_dirs() { # make required directories user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -` options=`$nginx -V 2>&1 | grep 'configure arguments:'` for opt in $options; do if [ `echo $opt | grep '.*-temp-path'` ]; then value=`echo $opt | cut -d "=" -f 2` if [ ! -d "$value" ]; then # echo "creating" $value mkdir -p $value && chown -R $user $value fi fi done } start() { [ -x $nginx ] || exit 5 [ -f $NGINX_CONF_FILE ] || exit 6 make_dirs echo -n $"Starting $prog: " daemon $nginx -c $NGINX_CONF_FILE retval=$? echo [ $retval -eq 0 ] && touch $lockfile return $retval } stop() { echo -n $"Stopping $prog: " killproc $prog -QUIT retval=$? echo [ $retval -eq 0 ] && rm -f $lockfile return $retval } restart() { configtest || return
-