热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

安装淘宝开源web服务器tengine替换nginx并使用proxy_cache做前端

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

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

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

目前稳定版[2013-11-22] Tengine-1.5.2
特性
继承Nginx-1.2.9的所有特性,100%兼容Nginx的配置;
动态模块加载(DSO)支持。加入一个模块不再需要重新编译整个Tengine;
流式上传到HTTP后端服务器或FastCGI服务器,大量减少机器的I/O压力;
更加强大的负载均衡能力,包括一致性hash模块、会话保持模块,还可以对后端的服务器进行主动健康检查,根据服务器状态自动上线下线;
输入过滤器机制支持。通过使用这种机制Web应用防火墙的编写更为方便;
动态脚本语言Lua支持。扩展功能非常高效简单;
支持管道(pipe)和syslog(本地和远端)形式的日志以及日志抽样;
组合多个CSS、Javascript文件的访问请求变成一个请求;
自动去除空白字符和注释从而减小页面的体积
自动根据CPU数目设置进程个数和绑定CPU亲缘性;
监控系统的负载和资源占用从而对系统进行保护;
显示对运维人员更友好的出错信息,便于定位出错机器;
更强大的防攻击(访问速度限制)模块;
更方便的命令行参数,如列出编译的模块列表、支持的指令等;
可以根据访问文件类型设置过期时间;
…

安装jemalloc可以增加性能

cd /root/src/toolkits/
wget http://www.canonware.com/download/jemalloc/jemalloc-3.4.1.tar.bz2
tar jxvf jemalloc-3.4.1.tar.bz2
cd jemalloc-3.4.1
./configure --prefix=/usr/local/jemalloc-3.4.1
make && make install
ldconfig

GeoIP白名单

wget http://geolite.maxmind.com/download/geoip/api/c/GeoIP.tar.gz
tar -zxvf GeoIP.tar.gz
cd GeoIP-1.4.6
./configure
make; make install
ldconfig

使用proxy_cache时增加purge模块

wget http://labs.frickle.com/files/ngx_cache_purge-2.1.tar.gz
tar zxvf ngx_cache_purge-2.1.tar.gz
--add-module=../ngx_cache_purge-2.1

后端nginx编译时需加上–with-http_realip_module以获取真实ip,并指定来源

set_real_ip_from   61.199.67.2; #前端ip
set_real_ip_from   192.168.0.111;#前端ip
real_ip_header     X-Real-IP;

编译安装tengine
jemalloc为编译路径

wget http://tengine.taobao.org/download/tengine-1.5.1.tar.gz
tar zxvf tengine-1.5.1.tar.gz
cd tengine-1.5.1
./configure --user=www --group=website --prefix=/opt/tengine-1.5.1 --add-module=../ngx_cache_purge-2.1 --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module  \
--with-http_concat_module=shared \
--with-http_sysguard_module=shared \
--with-http_limit_conn_module=shared \
--with-http_limit_req_module=shared \
--with-http_footer_filter_module=shared \
--with-http_upstream_ip_hash_module=shared \
--with-http_upstream_least_conn_module=shared \
--with-http_upstream_session_sticky_module=shared \
--with-jemalloc=/root/src/lempelf/packages/jemalloc-3.4.1
make
make install

GeoIp数据

cd  /opt/tengine-1.5.1/conf
wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
gunzip GeoIP.dat.gz
chgrp -R website /opt/tengine-1.5.1/conf
chmod -R 764 /opt/tengine-1.5.1/conf
chmod  774 /opt/tengine-1.5.1/conf

复制原nginx的配制文件于tengine

cd /opt/nginx/conf
cp awstats.conf fcgi.conf htpasswd block.conf nginx.conf /opt/tengine-1.5.1/conf/

检测配置文件

/opt/tengine-1.5.1/sbin/nginx -t -c /opt/tengine-1.5.1/conf/nginx.conf
nginx: [emerg] unknown directive "limit_zone" in /opt/tengine-1.5.1/conf/nginx.conf:71
nginx: [emerg] unknown directive "limit_conn" in /opt/tengine-1.5.1/conf/nginx.conf:136
如果有以上错误,需去掉limit_conn配置,ngx_http_limit_conn_module 模块在新版已使用新指令

增加新的功能
vi /opt/tengine-1.5.1/conf/nginx.conf
根据cpu数量自动设定Tengine的worker进程数量,并进行cpu绑定。

worker_processes     auto;
worker_cpu_affinity  auto;

关闭系统信息

server_info off;
server_tag off;

ngx_http_sysguard_module
系统过载保护

sysguard on;
        sysguard_load load=10.5 action=/loadlimit;
        sysguard_mem swapratio=20% action=/swaplimit;
        sysguard_mem free=100M action=/freelimit;
        location /loadlimit {
            return 503;
        }
        location /swaplimit {
            return 503;
        }
        location /freelimit {
            return 503;
        }

ngx_http_limit_req_module
并发限制模块

limit_req_zone $binary_remote_addr zOne=one:3m rate=1r/s;
    limit_req_zone $binary_remote_addr $uri zOne=two:3m rate=1r/s;
    limit_req_zone $binary_remote_addr $request_uri zOne=three:3m rate=1r/s;
    location / {
        limit_req zOne=one burst=5;
        limit_req zOne=two forbid_action=@test1;
        limit_req zOne=three burst=3 forbid_action=@test2;
    }
    location /off {
        limit_req off;
    }
    location @test1 {
        rewrite ^ /test1.html;
    }
    location @test2 {
        rewrite ^  /test2.html;
    }

删除旧的nginx软链接,给tengine增加软链接
rm /opt/nginx
ln -s /opt/tengine-1.5.1 /opt/nginx

一个完整的nginx.conf

user  www website;
worker_processes     auto;
worker_cpu_affinity  auto;
error_log  /var/log/nginx/nginx_error.log  error;
pid        /dev/shm/nginx.pid;
#Specifies the value for maximum file descriptors that can be opened by this process. 
worker_rlimit_nofile 51200;
dso {
     load ngx_http_footer_filter_module.so;
     load ngx_http_limit_conn_module.so;
     load ngx_http_limit_req_module.so;
     load ngx_http_sysguard_module.so;
     load ngx_http_upstream_ip_hash_module.so;
     load ngx_http_upstream_least_conn_module.so;
     load ngx_http_upstream_session_sticky_module.so;
}
events 
{
     use epoll;
     worker_connections 51200;
}
http 
{
     include       mime.types;
     default_type  application/octet-stream;
     log_format  access  '$remote_addr - $remote_user [$time_local] "$request" '
			   '$status $body_bytes_sent "$http_referer" '
			   '"$http_user_agent" $http_x_forwarded_for';
     open_log_file_cache max=1000 inactive=20s min_uses=2 valid=1m;
     server_names_hash_bucket_size 128;
     #linux 2.4+
     sendfile on;
     tcp_nopush     on;
     tcp_nodelay on;
     #tengine
     server_info off;
     server_tag off;
     #server_tag Apache;
     server_tokens off;
     server_name_in_redirect off;
     keepalive_timeout 60;
     client_header_buffer_size 16k;
     client_body_timeout 60;
     client_max_body_size 8m;
     large_client_header_buffers 4 32k;
     fastcgi_intercept_errors on;
     fastcgi_hide_header X-Powered-By;
     fastcgi_connect_timeout 180;
     fastcgi_send_timeout 180;
     fastcgi_read_timeout 180;
     fastcgi_buffer_size 128k;
     fastcgi_buffers 4 128K;
     fastcgi_busy_buffers_size 128k;
     fastcgi_temp_file_write_size 128k;
     fastcgi_temp_path /dev/shm;
     #open_file_cache max=51200 inactive=20s;
     #open_file_cache_valid    30s;
     #open_file_cache_min_uses 2;
     #open_file_cache_errors   off;
     gzip on;
     gzip_min_length  1k;
     gzip_comp_level 5;
     gzip_buffers     4 16k;
     gzip_http_version 1.1; 
     gzip_types       text/plain application/x-Javascript text/css application/xml;
     gzip_proxied any;
     limit_req_log_level error;
     limit_req_zone $binary_remote_addr $uri zOne=two:30m rate=10r/s;
     #访问限制白名单
     geo $white_ip {
          #ranges;
          default 0;
          127.0.0.1/32 1;
          182.55.21.28/32 1;
          192.168.0.0/16 1;
          61.199.67.0/24 1;
     }
     client_body_buffer_size  512k;
     proxy_connect_timeout    5;
     proxy_read_timeout       60;
     proxy_send_timeout       5;
     proxy_buffer_size        16k;
     proxy_buffers            4 32k;
     proxy_busy_buffers_size 64k;
     proxy_temp_file_write_size 64k;
     #注:proxy_temp_path和proxy_cache_path指定的路径必须在同一分区
     proxy_temp_path   /opt/nginx/proxy_temp_dir;
     #设置Web缓存区名称为cache_www,内存缓存空间大小为3000MB,1天没有被访问的内容自动清除,硬盘缓存空间大小为30GB。
     proxy_cache_path  /opt/nginx/proxy_cache_www  levels=1:2   keys_zOne=cache_www:3000m inactive=1d max_size=20g;
     upstream www_server {
     server   192.168.0.131:80;
     }
     server
     {
             listen       80 default;
             server_name  _;
             return 444;
             access_log  off;
     }
     server
     {
             listen       80;
             server_name  www.c1gstudio.com;
             index index.html index.htm index.php;
             root  /opt/htdocs/www;
             access_log  /var/log/nginx/proxy.www.c1gstudio.com.log  access buffer=24k;
		if (-d $request_filename){
		rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
		}
	     limit_req_whitelist geo_var_name=white_ip geo_var_value=1;
             limit_req zOne=two burst=50 forbid_action=/visitfrequently.html;
             location @visitfrequently {
	         rewrite ^ /visitfrequently.html;
             }
	     location ~/\.ht {
	         deny all;
	     }
	    #用于清除缓存,假设一个URL为http://192.168.8.42/test.txt,通过访问http://192.168.8.42/purge/test.txt就可以清除该URL的缓存。
	    location ~ /purge(/.*)
	    {
	     #设置只允许指定的IP或IP段才可以清除URL缓存。
	     allow            127.0.0.1;
	     allow            192.168.0.0/16;
	     deny            all;
	     proxy_cache_purge    cache_www   $host$1$is_args$args;
	     error_page 405 =200 /purge$1; #处理squidclient purge的时候出现的405错误
	    }    
	    if ( $request_method = "PURGE" ) {
	     rewrite ^(.*)$ /purge$1 last;
	    }
	    location /
	    {
		error_page 502 504 /502.html;
		 proxy_set_header Host  $host;
		 #proxy_set_header X-Forwarded-For  $remote_addr;
		 proxy_set_header X-Real-IP $remote_addr;
		 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
		 proxy_pass http://www_server;
		 add_header      X-Cache   Cache-Skip;
	    }
	    location ~ 404\.html$
	    {
		 proxy_set_header Host  $host;
		 #proxy_set_header X-Forwarded-For  $remote_addr;
		 proxy_set_header X-Real-IP $remote_addr;
		 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
		 proxy_pass http://www_server;
		 add_header      X-Cache   Cache-Skip;
	    }
             location ~ .*\.(htm|html|)?$
             {
		 #如果后端的服务器返回502、504、执行超时等错误,自动将请求转发到upstream负载均衡池中的另一台服务器,实现故障转移。
		 proxy_next_upstream http_502 http_504 error timeout invalid_header;
		 proxy_cache cache_www;
		 #对不同的HTTP状态码设置不同的缓存时间
		 proxy_cache_valid  200 304 5m;
		 #以域名、URI、参数组合成Web缓存的Key值,Nginx根据Key值哈希,存储缓存内容到二级缓存目录内
		 proxy_cache_key $host$uri$is_args$args;
		 proxy_set_header Host  $host;
		 proxy_http_version 1.1;
		 #proxy_set_header X-Forwarded-For  $remote_addr;
		 proxy_set_header X-Real-IP $remote_addr;
		 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
		 proxy_pass http://www_server;
		 #支持后台expires
		 proxy_ignore_headers "Cache-Control" "Expires";
		 add_header      X-Cache   Cache;
             }
	     location ~* ^.+\.(jpg|jpeg|gif|png|rar|zip|css|js)$ {
		valid_referers none blocked *.c1gstudio.com;
		if ($invalid_referer) {
		    rewrite ^/ http://leech.c1gstudio.com/leech.gif;
		    return 412;
		    break;
		}
                 access_log   off;
		 #如果后端的服务器返回502、504、执行超时等错误,自动将请求转发到upstream负载均衡池中的另一台服务器,实现故障转移。
		 proxy_next_upstream http_502 http_504 error timeout invalid_header;
		 proxy_cache cache_www;
		 #对不同的HTTP状态码设置不同的缓存时间
		 proxy_cache_valid  200 304 5m;
		 #以域名、URI、参数组合成Web缓存的Key值,Nginx根据Key值哈希,存储缓存内容到二级缓存目录内
		 proxy_cache_key $host$uri$is_args$args;
		 proxy_set_header Host  $host;
		 proxy_http_version 1.1;
		 #proxy_set_header X-Forwarded-For  $remote_addr;
		 proxy_set_header X-Real-IP $remote_addr;
		 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
		 proxy_pass http://www_server;
		 #支持后台expires
		 proxy_ignore_headers "Cache-Control" "Expires";
		 add_header      X-Cache   Cache;
	     }
     }
}

启动tengine
/opt/nginx/sbin/nginx

观察了下top降低了许多

===========2014/1/3更新=============
如果负载时大时小可能有io瓶颈,可以将proxy_cache放到/dev/shm 中来解决.(/dev/shm默认为内存一半大小)
创建目录并加入开机执行

mkdir /dev/shm/nginx

vi /etc/rc.local
在nginx启动前加入
mkdir /dev/shm/nginx

修改nginx.conf

proxy_temp_path   /dev/shm/nginx/proxy_temp_dir;
     #设置Web缓存区名称为cache_www,内存缓存空间大小为3000MB,1天没有被访问的内容自动清除,硬盘缓存空间大小为30GB。
     proxy_cache_path  /dev/shm/nginx/proxy_cache_www  levels=1:2   keys_zOne=cache_www:3000m inactive=1d max_size=20g;
推荐阅读
  • Linux下部署Symfoy2对app/cache和app/logs目录的权限设置,symfoy2logs
    php教程|php手册xml文件php教程-php手册Linux下部署Symfoy2对appcache和applogs目录的权限设置,symfoy2logs黑色记事本源码,vsco ... [详细]
  • 构建LNMP架构平台
    LNMP架构的组成:Linux、Nginx、MySQL、PHP关于NginxNginx与apache的作用一样,都是为了搭建网站服务器,由俄罗斯人lgorsysoev开发,其特点是 ... [详细]
  • 一句话解决高并发的核心原则
    本文介绍了解决高并发的核心原则,即将用户访问请求尽量往前推,避免访问CDN、静态服务器、动态服务器、数据库和存储,从而实现高性能、高并发、高可扩展的网站架构。同时提到了Google的成功案例,以及适用于千万级别PV站和亿级PV网站的架构层次。 ... [详细]
  • 本文讨论了Alink回归预测的不完善问题,指出目前主要针对Python做案例,对其他语言支持不足。同时介绍了pom.xml文件的基本结构和使用方法,以及Maven的相关知识。最后,对Alink回归预测的未来发展提出了期待。 ... [详细]
  • 目录浏览漏洞与目录遍历漏洞的危害及修复方法
    本文讨论了目录浏览漏洞与目录遍历漏洞的危害,包括网站结构暴露、隐秘文件访问等。同时介绍了检测方法,如使用漏洞扫描器和搜索关键词。最后提供了针对常见中间件的修复方式,包括关闭目录浏览功能。对于保护网站安全具有一定的参考价值。 ... [详细]
  • 本文介绍了在开发Android新闻App时,搭建本地服务器的步骤。通过使用XAMPP软件,可以一键式搭建起开发环境,包括Apache、MySQL、PHP、PERL。在本地服务器上新建数据库和表,并设置相应的属性。最后,给出了创建new表的SQL语句。这个教程适合初学者参考。 ... [详细]
  • 这是原文链接:sendingformdata许多情况下,我们使用表单发送数据到服务器。服务器处理数据并返回响应给用户。这看起来很简单,但是 ... [详细]
  • 在说Hibernate映射前,我们先来了解下对象关系映射ORM。ORM的实现思想就是将关系数据库中表的数据映射成对象,以对象的形式展现。这样开发人员就可以把对数据库的操作转化为对 ... [详细]
  • 本文介绍了在SpringBoot中集成thymeleaf前端模版的配置步骤,包括在application.properties配置文件中添加thymeleaf的配置信息,引入thymeleaf的jar包,以及创建PageController并添加index方法。 ... [详细]
  • 本文介绍了通过ABAP开发往外网发邮件的需求,并提供了配置和代码整理的资料。其中包括了配置SAP邮件服务器的步骤和ABAP写发送邮件代码的过程。通过RZ10配置参数和icm/server_port_1的设定,可以实现向Sap User和外部邮件发送邮件的功能。希望对需要的开发人员有帮助。摘要长度:184字。 ... [详细]
  • Java验证码——kaptcha的使用配置及样式
    本文介绍了如何使用kaptcha库来实现Java验证码的配置和样式设置,包括pom.xml的依赖配置和web.xml中servlet的配置。 ... [详细]
  • 本文介绍了使用cacti监控mssql 2005运行资源情况的操作步骤,包括安装必要的工具和驱动,测试mssql的连接,配置监控脚本等。通过php连接mssql来获取SQL 2005性能计算器的值,实现对mssql的监控。详细的操作步骤和代码请参考附件。 ... [详细]
  • nginx+多个tomcat
    学习nginx的时候遇到的问题:nginx怎么部署两台tomcat?upstream在网上找的资源,我在nginx配置文件(nginx.conf)中添加了两个server。结果只显 ... [详细]
  • Linux一键安装web环境全攻略
    摘自阿里云服务器官网,此处一键安装包下载:点此下载安装须知1、此安装包可在阿里云所有Linux系统上部署安装,此安装包包含的软件及版本为& ... [详细]
  • nginx 反向代理proxy参数讲解
    ![](http:i2.51cto.comimagesblog20180805c32a728954d93ee2a4e4fb59c150a15b.png?x-oss-processi ... [详细]
author-avatar
mobiledu2502927147
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有