Tengine
1.什么是Tengine
Tengine是由淘宝网发起的Web服务器项目。它在Nginx的基础上,针对大访问量网站的需求,添加了很多高级功能和特性。它的目的是打造一个高效、安全的Web平台。
Nginx:是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器。Nginx是由伊戈尔•赛索耶夫为俄罗斯访问量第二的Rambler.ru站点(俄文:Рамблер)开发的。
Tengine官网: http://tengine.taobao.org/
Nginx官网:http://nginx.org/
2. Tengine的安装
这里使用centsOS 系统,连接工具xshell5,上传工具winSCP。
2.1:下载Tengin压缩包,地址:http://tengine.taobao.org/download_cn.html
2.2: 将下载好的压缩包上传到“/opt” 下,输入命令“tar zxvf 包”解压,解压后进入,出现下列文件:
2.3: 输入“./confingure”检查是否符合安装环境,一般要安装三个插件:gcc、prce、openssl。三个插件的在线安装方法:
Gcc: yum -y install gcc
Prce:yum -y install pcre pcre-devel
Openssl:yum -y install openssl openssl-devel
2.4: 安装完成之后继续输入“./confingure”,出现以下画面代表可以安装了,输入“make install”安装。
2.5:安装完成后进入安装目录,目录为:/usr/local/nginx,之后进入sbin目录,输入“./nginx”启动Nginx,然后去浏览器访问(输入ip就行,默认端口80),出现以下画面成功:
nginx -m 显示所有加载的模块
nginx -l 显示所有可以使用的指令
nginx -t 检查nginx的配置文件是否正确
nginx -s 启动nginx
nginx -s reload 重启nginx
nginx -s stop 停止nginx
3. Tengine 的配置
配置文件地址 :/usr/local/nginx/conf/nginx.conf
#user nobody;
#配置内核数,服务器内核数可在 /roce/cpuinfo 查询
worker_processes 1;
#错误日志,用来定位错误,默认开启,默认error级别,一般不建议更改
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid文件就是一个纯文本文件,里面记录的是进程的pid号
#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协议
http {
#用来判断mime类型
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"';
#用来记录访问记录,多用来做ip追踪、PV、UV、QPS,默认开启
#access_log logs/access.log main;
#流式文件传送
sendfile on;
#tcp_nopush on;
#长连接的等待时间
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
#用来做负载均衡
upstream backend {
server 192.168.136.128:8080;
server 192.168.136.1:8080;
#用来检查服务器是否正常运行,interval发送请求的事件,rise成功的次数,达到次数表示正常
#,fall失败的次数,达到次数表示停止,timeout超时时间
check interval=3000 rise=2 fall=5 timeout=1000 type=http;
check_http_send "HEAD / HTTP/1.0\r\n\r\n";
check_http_expect_alive http_2xx http_3xx;
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
proxy_pass http://backend;
if ( $uri ~* ^/admin.*$ ){
return 500;
}
}
location /status {
check_status;
auth_basic "请输入账号密码";
auth_basic_user_file /usr/local/nginx/conf/htpasswd;
access_log off;
allow 192.168.136.1;
deny all;
}
#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;
# }
#}
}
4. 配置upstream
#此模块提供了在运行时动态解析upstream中server域名的功能,不要在server{}中添加。
upstream backend {
server 192.168.136.128:8080;#server 你的访问地址
server 192.168.136.1:8080;
check interval=3000 rise=2 fall=5 timeout=1000 type=http;
check_http_send "HEAD / HTTP/1.0\r\n\r\n";
check_http_expect_alive http_2xx http_3xx;
}
#在server{}中找到 location / {},在里面加入proxy_pass http://backend;
location / {
proxy_pass http://backend;
}
更多参考:http://tengine.taobao.org/document_cn/http_upstream_dynamic_cn.html
5.配置健康管理
访问地址:你的tengineip地址/statis
在server{}中添加:
location /status {
check_status;
access_log off;
#设置允许谁访问
allow 192.168.136.1;
#设置不允许谁访问
deny all;
}
更多参考:http://tengine.taobao.org/document_cn/http_upstream_check_cn.html
配置账号密码登录:
首先去conf目录创建一个名为htpasswd的文件,然后安装httpd插件(命令:#yum install httpd -y),用命令“# htpasswd -nb 账号 密码”,生成后将生成的账号密码复制到htpasswd文件中,然后去访问地址访问出现输入账号密码框,即为成功。
6.session共享
用来解决多个tomcat session共享的问题
- 粘性session: 在uspstream中添加:session_sticky;
- 非粘性session:
用第三方插件保存seisson
参考地址:http://blog.youkuaiyun.com/liaomin416100569/article/details/72953938