Tengine
官网对Tengine的介绍
Tengine是由淘宝网发起的Web服务器项目。它在Nginx的基础上,针对大访问量网站的需求,添加了很多高级功能和特性。Tengine的性能和稳定性已经在大型的网站如淘宝网,天猫商城等得到了很好的检验。它的最终目标是打造一个高效、稳定、安全、易用的Web平台。
官网特性介绍中第一点就强调了Tengine对于原Nginx的兼容性,所以在使用Tengine时可以向使用Nginx一样。
1、快速安装
1.1、找到Tengine官网,下载tar包,本文使用Tengine-2.3.1.tar.gz版本。
1.2、jar包下好后,上传到linux服务器
1.3、解压
tar -zxvf tengine-2.3.1.tar.gz
cd tengine-2.3.1
1.4、nginx需要编译安装
./configure --prefix=/usr/local/tengine
prefix可指定安装目录
如果遇到如下报错,需要先安装C的编译器
checking for OS
+ Linux 2.6.32-431.el6.x86_64 x86_64
checking for C compiler ... not found
./configure: error: C compiler cc is not found
1.5、安装gcc
yum install gcc -y
安装成功后,再执行 ./configure --prefix=/usr/local/tengine,如果你又遇到如下错误
./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.
继续安装
yum install pcre-devel openssl openssl-devel -y
安装成功
再执行./configure --prefix=/usr/local/tengine,成功
1.6、make编译
make && make install
编译成功
1.7、启动
cd /usr/local/tengine/sbin/
直接执行./nginx,即可即启动
./nginx
1.8、访问测试
http://node06,成功
2、反向代理
反向代理服务器是位于用户与目标服务器之间,但对于用户来说,用户直接访问反向代理服务器就可以获得目标服务器的资源,用户不需要知道目标服务器的地址。
反向代理服务器通常可用来作为web加速,即使用反向代理作为web服务的前置机来降低网络和服务器的负载,提供访问效率。
2.1、重新启动一台虚拟机:node05(192.168.70.114),在node05上启动一个eureka服务,端口18001。
2.2、回到nginx虚拟机中,cd到nginx配置文件
cd /usr/local/tengine/sbin
新增如下配置。
upstream myeureka {
server 192.168.70.114:18001;
}
server {
listen 8888;
server_name node06;
location / {
proxy_pass http://myeureka;
}
}
2.3、重启nginx,访问 http://node06:8888
3、负载均衡
3.1、在node05上,再启动两个zuul服务,一个端口19001,一个端口19002
3.2、新增nginx配置
upstream myzuul {
server 192.168.70.114:19001;
server 192.168.70.114:19002;
}
server {
listen 80;
server_name node06;
location / {
proxy_pass http://myzuul;
index index.html index.htm;
}
}
3.3、zuul服务中,写一个测试接口
@RestController
public class MyZuulController {
@PostMapping("/test")
public void test(){
System.out.println("zuul test");
}
}
发起请求,可以看到默认是负载均衡策略是轮询
也可以配置其他负载策略
权重
upstream myzuul {
server 192.168.70.114:19001 weight=1;
server 192.168.70.114:19002 weight=5;
}
ip
upstream myzuul {
ip_hash;
server 192.168.70.114:19001;
server 192.168.70.114:19002;
}
4、动静分离
4.1、zuul配置文件添加如下配置,指定静态文件路径匹配
spring:
mvc:
static-path-pattern: /**
view:
prefix: /
suffix: .html
4.2、项目中就存放一个html文件,没有图片
4.3、直接访问zuul服务,找不到页面
4.4、现在把静态文件上传到nginx所在的服务器上。
4.5、新增静态文件路径配置
server {
listen 80;
server_name node06;
location / {
proxy_pass http://myzuul;
index index.html index.htm;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|html|htm|css|js)$ {
root /var/data/static/;
}
}
4.6、重启nginx,通过nginx访问,识别到了图片。