使用Tengine替代Nginx实战部署
作者:尹正杰
版权声明:原创作品,谢绝转载!否则将追究法律责任。
大概是2019年3月11日,F5 Networks宣布将以6.7亿美元收购Nginx,消息一出,网上很多小伙伴开始炸锅了,纷纷议论Nginx后续的版本会不会收费之类的话。Nginx的确是一款优秀的web服务器,不过咱们中国还有一款基于Nginx二次开发的软件叫Tengine,它是由阿里巴巴集团开源的,目前阿里巴巴的电商网站使用的就是Tengine,据说性能在Nginx之上。
淘宝网,天猫网使用就是阿里巴巴公司自己开源的Tengine这款web服务器,当然,还有其它一些公司也在使用Tengine。
一.Tengine概述
1>.什么是Tenine
Tengine是由淘宝网发起的Web服务器项目。它在Nginx的基础上,针对大访问量网站的需求,添加了很多高级功能和特性。Tengine的性能和稳定性已经在大型的网站如淘宝网,天猫商城等得到了很好的检验。它的最终目标是打造一个高效、稳定、安全、易用的Web平台。 从2011年12月开始,Tengine成为一个开源项目,Tengine团队在积极地开发和维护着它。Tengine团队的核心成员来自于淘宝、搜狗等互联网企业。 官网地址:http://tengine.taobao.org/
2>.下载Tenine
下载地址:
http://tengine.taobao.org/download.html
二.编译安装Tengine
1>.停用现有的nginx服务
[root@node101.yinzhengjie.org.cn ~]# netstat -ntalp | grep nginx tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 24954/nginx: master tcp 0 0 0.0.0.0:443 0.0.0.0:* LISTEN 24954/nginx: master [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# nginx -s stop [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# netstat -ntalp | grep nginx [root@node101.yinzhengjie.org.cn ~]#
2>.下载Tengine的安装包
[root@node101.yinzhengjie.org.cn ~]# cd /usr/local/src/ [root@node101.yinzhengjie.org.cn /usr/local/src]# [root@node101.yinzhengjie.org.cn /usr/local/src]# ll total 992 drwxr-xr-x 6 root root 186 Dec 23 22:35 echo-nginx-module drwxr-xr-x 9 1001 1001 186 Dec 22 08:19 nginx-1.14.2 -rw-r--r-- 1 root root 1015384 Dec 4 2018 nginx-1.14.2.tar.gz [root@node101.yinzhengjie.org.cn /usr/local/src]# [root@node101.yinzhengjie.org.cn /usr/local/src]# wget http://tengine.taobao.org/download/tengine-2.3.2.tar.gz --2019-12-24 20:55:13-- http://tengine.taobao.org/download/tengine-2.3.2.tar.gz Resolving tengine.taobao.org (tengine.taobao.org)... 140.205.230.4 Connecting to tengine.taobao.org (tengine.taobao.org)|140.205.230.4|:80... connected. HTTP request sent, awaiting response... 200 OK Length: 2835884 (2.7M) [application/octet-stream] Saving to: ‘tengine-2.3.2.tar.gz’ 100%[===========================================================================================================>] 2,835,884 482KB/s in 5.9s 2019-12-24 20:55:19 (470 KB/s) - ‘tengine-2.3.2.tar.gz’ saved [2835884/2835884] [root@node101.yinzhengjie.org.cn /usr/local/src]# [root@node101.yinzhengjie.org.cn /usr/local/src]# ll total 3764 drwxr-xr-x 6 root root 186 Dec 23 22:35 echo-nginx-module drwxr-xr-x 9 1001 1001 186 Dec 22 08:19 nginx-1.14.2 -rw-r--r-- 1 root root 1015384 Dec 4 2018 nginx-1.14.2.tar.gz -rw-r--r-- 1 root root 2835884 Sep 5 16:58 tengine-2.3.2.tar.gz [root@node101.yinzhengjie.org.cn /usr/local/src]#
3>.编译安装Tengine
[root@node101.yinzhengjie.org.cn /usr/local/src]# [root@node101.yinzhengjie.org.cn /usr/local/src]# tar -xf tengine-2.3.2.tar.gz [root@node101.yinzhengjie.org.cn /usr/local/src]# [root@node101.yinzhengjie.org.cn /usr/local/src]# cd tengine-2.3.2/ [root@node101.yinzhengjie.org.cn /usr/local/src/tengine-2.3.2]# [root@node101.yinzhengjie.org.cn /usr/local/src/tengine-2.3.2]# nginx -V #由于Tengine是兼容Nginx的,咱们直接复制Nginx的编译参数来安装Tengine,但千万别忘记修改安装路径哈~ nginx version: nginx/1.14.2 built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) built with OpenSSL 1.0.2k-fips 26 Jan 2017 TLS SNI support enabled configure arguments: --prefix=/yinzhengjie/softwares/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip _module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module --add-module=/usr/local/src/echo-nginx-module [root@node101.yinzhengjie.org.cn /usr/local/src/tengine-2.3.2]# [root@node101.yinzhengjie.org.cn /usr/local/src/tengine-2.3.2]# ./configure --prefix=/yinzhengjie/softwares/tengine --user=nginx --group=nginx --with-h ttp_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream
--with-stream_ssl_module --with-stream_realip_module --add-module=/usr/local/src/echo-nginx-module ...... [root@node101.yinzhengjie.org.cn /usr/local/src/tengine-2.3.2]# echo $? 0 [root@node101.yinzhengjie.org.cn /usr/local/src/tengine-2.3.2]# [root@node101.yinzhengjie.org.cn /usr/local/src/tengine-2.3.2]# make -j 4 && make install [root@node101.yinzhengjie.org.cn /usr/local/src/tengine-2.3.2]# [root@node101.yinzhengjie.org.cn /usr/local/src/tengine-2.3.2]# echo $? 0 [root@node101.yinzhengjie.org.cn /usr/local/src/tengine-2.3.2]# [root@node101.yinzhengjie.org.cn /usr/local/src/tengine-2.3.2]# ll /yinzhengjie/softwares/ #很显然,安装完成啦~ total 0 drwxr-xr-x 13 nginx nginx 178 Dec 22 09:21 nginx drwxr-xr-x 6 root root 54 Dec 24 21:14 tengine [root@node101.yinzhengjie.org.cn /usr/local/src/tengine-2.3.2]#
4>.检查Tengine的版本
[root@node101.yinzhengjie.org.cn ~]# /yinzhengjie/softwares/tengine/sbin/nginx -V Tengine version: Tengine/2.3.2 nginx version: nginx/1.17.3 built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) built with OpenSSL 1.0.2k-fips 26 Jan 2017 TLS SNI support enabled configure arguments: --prefix=/yinzhengjie/softwares/tengine --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_real ip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module
--add-module=/usr/local/src/echo-nginx-module
[root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]#
三.使用Nginx的配置文件启动Tengine
1>.修改Tengine的子配置文件(此处请允许我先买个坑)
[root@node101.yinzhengjie.org.cn ~]# cd /yinzhengjie/softwares/tengine/ [root@node101.yinzhengjie.org.cn /yinzhengjie/softwares/tengine]# [root@node101.yinzhengjie.org.cn /yinzhengjie/softwares/tengine]# ll total 0 drwxr-xr-x 2 root root 333 Dec 24 21:14 conf drwxr-xr-x 2 root root 40 Dec 24 21:14 html drwxr-xr-x 2 root root 6 Dec 24 21:14 logs drwxr-xr-x 2 root root 19 Dec 24 21:14 sbin [root@node101.yinzhengjie.org.cn /yinzhengjie/softwares/tengine]# [root@node101.yinzhengjie.org.cn /yinzhengjie/softwares/tengine]# vim conf/nginx.conf #在Tengine的主配置文件末尾加载Nginx的配置文件,如下图所示
2>.我们发现检查语法时报错啦,因为我们是在Tengine中加载的Nginx的子配置文件,而Nginx中子配置文件使用Nginx主配置文件中的变量
[root@node101.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/nginx/conf.d/node101_yinzhengjie_org.cn.conf server { listen 80; listen 443 ssl; server_name node101.yinzhengjie.org.cn; access_log /yinzhengjie/softwares/nginx/logs/node101_yinzhengjie_org_cn_access.log my_access_json; error_log /yinzhengjie/softwares/nginx/logs/node101_yinzhengjie_org_cn_error.log; location / { root /yinzhengjie/data/web/nginx/static/cn; index index.html; #定义有效的请求referer,用空格隔开即可 valid_referers none blocked server_names *.baidu.com example.* ~\.google\.; #如果没有在上面的有效链接定义那么均属于无效请求referer if ($invalid_referer) { return 403; } #如果是一些常见的压测试工具,咱们直接进给他拒绝访问 if ($http_user_agent ~ "ApacheBench|WebBench|TurnitinBot|Sougou web spider|Grid Server"){ return 403; } } location = /favicon.ico { root /yinzhengjie/data/web/nginx/images/jd; } } [root@node101.yinzhengjie.org.cn ~]#