Nginx进阶之:Tengine


title: Nginx进阶之:Tengine
categories: Linux
tags:
- Tengine
- Nginx
timezone: Asia/Shanghai
date: 2019-01-08


Tengine简介

Tengine是由淘宝网发起的Web服务器项目。它在Nginx的基础上,针对大访问量网站的需求,添加了很多高级功能和特性。Tengine的性能和稳定性已经在大型的网站如淘宝网,天猫商城等得到了很好的检验。它的最终目标是打造一个高效、稳定、安全、易用的Web平台。

为什么叫他进阶版?因为之前在一个项目部署的时候本来是打算Nginx + 2台tomcat做负载均衡,但是这个项目是在一个内网环境。也就是所有需要访问服务的客户机基本都在一个192.168.0.x子网,结果反复测试Nginx的负载均衡发现无论如何都只是转发到其中一台tomcat。经过查资料得知Nginx的轮训算法是根据IP地址的前3位来计算hash。无意中发现了Tengine,经过测试Tengine的一致性hash模块功能完美的解决了我的问题。这部分内容的演示会在后边文章演示。

另外Tengine的健康检查模块功能自带前台查看页面,可以方便的看到后端tomcat的健康状态,虽然很多工具也可以解决这个问题。不过既然有这个很轻量并且好用的工具也不错。另外Tengine完全兼容Nginx,因此可以参照Nginx的方式来配置Tengine。

环境

REHL7
CentOS7

第一步:关闭系统默认防火墙

setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config

systemctl stop firewalld
systemctl disable firewalld
systemctl stop iptables
systemctl disable iptables

systemctl status firewalld
systemctl status iptables

第二步:安装Tengine(在线yum安装和离线源码方式安装)

安装方法1:离线安装

1.配置本地yum并安装开发工具

mkdir /mnt/cdrom
mount /dev/cdrom /mnt/cdrom

cat <<EOF >/etc/yum.repos.d/local.repo
[local]
name=local
baseurl=file:///mnt/cdrom
gpgcheck=0
enabled=1
EOF

yum clean all       #清理本地缓存
yum clean plugins   #清理插件缓存
yum makecache       #构建缓存

yum group install -y "Development Tools"

2.下载安装PCRE

curl -o /home/pcre-8.42.tar.gz https://ftp.pcre.org/pub/pcre/pcre-8.42.tar.gz
tar vxzf pcre-8.42.tar.gz
cd pcre-8.42
./configure
make
make install

3.下载安装zlib

wget http://www.zlib.net/zlib-1.2.11.tar.gz
tar vxzf zlib-1.2.11.tar.gz
cd zlib-1.2.11
./configure
make
make install

4.下载安装OpenSSL

wget https://www.openssl.org/source/openssl-1.1.1a.tar.gz
tar vxzf openssl-1.1.1a.tar.gz
cd openssl-1.1.1a
./config
make
make install

ln -s /usr/local/lib64/libssl.so.1.1 /usr/lib64/libssl.so.1.1
ln -s /usr/local/lib64/libcrypto.so.1.1 /usr/lib64/libcrypto.so.1.1

openssl version

5.下载并安装Tengine

curl -o /home/tengine-2.2.3.tar.gz http://tengine.taobao.org/download/tengine-2.2.3.tar.gz
tar -vxf tengine-2.2.3.tar.gz
cd tengine-2.2.3
./configure --with-http_ssl_module
make
make install

6.完成安装并启动

默认安装路径:/usr/local/nginx

# 启动Nginx
/usr/local/nginx/sbin/nginx

# 优雅的重启(重载配置文件,如果配置文件有错误的话,会继续使用之前配置运行)
/usr/local/nginx/sbin/nginx -s reload
    -s stop     快速停止
    -s quit     优雅的退出
    -s reopen   重新打开日志文件
    -s reload   重新加载配置文

# 测试配置文件是否正确
/usr/local/nginx/sbin/nginx -t

安装方法2:采用阿里云官方yum源方式安装

1.设置Tengine官方YUM源:CentOS7

curl -o /etc/yum.repos.d/opsx-centos7.repo https://mirrors.aliyun.com/opensource/149994924900000037/opsx/centos7/opsx-centos7.repo
yum clean all       #清理本地缓存
yum clean plugins   #清理插件缓存
yum makecache       #构建缓存

3.安装Tengine并启动

yum install -y tengine
/opt/tengine/sbin/nginx

4.配置文件路径

默认安装路径在:/opt/tengine

# 测试配置文件是否正确
/opt/tengine/sbin/nginx -t

# 优雅的重启(重载配置文件,如果配置文件有错误的话,会继续使用之前配置运行)
/opt/tengine/sbin/nginx -s reload
    -s stop     快速停止
    -s quit     优雅的退出
    -s reopen   重新打开日志文件
    -s reload   重新加载配置文

注意:在CentOS7下只配置Tengine官方yum源可以安装成功,REHL7下提示缺少依赖,所以这里同时配置了163的yum源。

cat <<EOF >/etc/yum.repos.d/163.repo
[163]
name=163
baseurl=http://mirrors.163.com/centos/7/os/x86_64/
gpgcheck=0
enabled=1
EOF

附录:Tengine & Nginx性能测试

http://tengine.taobao.org/document_cn/benchmark_cn.html

# 结论:
    Tengine相比Nginx默认配置,提升200%的处理能力。
    Tengine相比Nginx优化配置,提升60%的处理能力。

附录:Tengine建立虚拟目录

# 访问:http://10.0.1.131的时候访问的是html目录
# 访问:http://10.0.1.131/111的时候访问的是/opt/tengine/111目录
location / {
    root   html;
    index  index.html index.htm;
}

location /111 {
    alias   /opt/tengine/111;
    index  index.html index.htm;
}

附录:Tengine一台服务器建立多个虚拟主机(通过域名区分)

1./opt/tengine/的根目录下建立111和222两个文件夹并分别建立index.html主页文件

mkdir -p /opt/tengine/111
mkdir -p /opt/tengine/222
echo "111" > /opt/tengine/111/index.html
echo "222" > /opt/tengine/222/index.html

2.http下增加如下内容

server {
    listen       80;
    server_name  111.com;

    location / {
        root   111;
        index  index.html index.htm;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }

}


server {
    listen       80;
    server_name  222.com;

    location / {
        root   222;
        index  index.html index.htm;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }

}

3.重启Tengine服务生效

/opt/tengine/sbin/nginx -s reload

附录:Tengine一台服务器建立多个虚拟主机(通过端口区分)

只需要将以上listen       80;修改为自己想要的端口即可。

附录:Tengine一台服务器建立多个虚拟主机(绑定IP)

server_name  222.com;   将其中的222.com换成想要绑定的IP即可

附录:Tengine配置文件详解

/opt/tengine/sbin/nginx
/opt/tengine/sbin/nginx -s reload
/opt/tengine/sbin/nginx -t
vim /opt/tengine/conf/nginx.conf

[root@redhat73 conf]# cat nginx.conf

#user  nobody;          # 指定 Nginx Worker 进程运行用户以及用户组。
worker_processes  1;

# 在这里,很多人会误解worker_connections这个参数的意思,认为这个值就是nginx所能建立连接的最大值。
# 其实不然,这个值是表示每个worker进程所能建立连接的最大值
# 所以,一个nginx能建立的最大连接数,应该是worker_connections * worker_processes。
# 当然,这里说的是最大连接数,对于HTTP请求本地资源来说
# 能够支持的最大并发数量是worker_connections * worker_processes
# 而如果是HTTP作为反向代理来说,最大并发数量应该是worker_connections * worker_processes/2。
# 因为作为反向代理服务器,每个并发会建立与客户端的连接和与后端服务的连接,会占用两个连接。

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
#error_log  "pipe:rollback logs/error_log interval=1d baknum=7 maxsize=2G";

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}

# load modules compiled as Dynamic Shared Object (DSO)
#
#dso {
#    load ngx_http_fastcgi_module.so;
#    load ngx_http_rewrite_module.so;
#}

http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;
    #access_log  "pipe:rollback logs/access_log interval=1d baknum=7 maxsize=2G"  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
        #access_log  "pipe:rollback logs/host.access_log interval=1d baknum=7 maxsize=2G"  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

附录:以上所用到的安装包下载地址

pcre-8.42.tar.gz:https://pan.baidu.com/s/1qQge_iblLfHADBpB_mRp1A

zlib-1.2.11.tar.gz:https://pan.baidu.com/s/1Wf0CxcCQiM0-fhG7qiWhEQ

openssl-1.1.1a.tar.gz:https://pan.baidu.com/s/1sdKp4xrPG5T_TmoFXF040w

nginx-1.14.2.tar.gz:https://pan.baidu.com/s/1ehbcLctFso6VyL4UmSHUcg

附录:

Tengine官网:http://tengine.taobao.org/

PCRE官网:http://www.pcre.org/

zile官网:http://www.zlib.net/

OpenSSL官网:https://www.openssl.org/

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,117评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,328评论 1 293
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,839评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,007评论 0 206
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,384评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,629评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,880评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,593评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,313评论 1 243
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,575评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,066评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,392评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,052评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,082评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,844评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,662评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,575评论 2 270

推荐阅读更多精彩内容