tengine反向代理

发布 : 2016-10-02 分类 : 大数据 浏览 :

什么是反向代理?

1
2
3
4
5
6
7
8
9
10
什么是反向代理?

• 通常的代理服务器,只用于代理内部网络对Internet的连接请求,客户机必须指定
代理服务器,并将本来要直接发送到Web服务器上的http请求发送到代理服务器中
由代理服务器向Internet上的web服务器发起请求,最终达到客户机上网的目的。

• 而反向代理(Reverse Proxy)方式是指以代理服务器来接受internet上的连
接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回
给internet上请求连接的客户端,此时代理服务器对外就表现为一个反向代理服务

反向代理如何配置?

1
2
查看tengine文档
http://tengine.taobao.org/

1.安装tomcat

1.1.安装ip为192.168.230.10的主机的tomcat

1
2
3
[root@node1 software]# tar -zxvf apache-tomcat-7.0.61.tar.gz -C /opt/modules
[root@node1 software]# cd /opt/modules/apache-tomcat-7.0.61/webapps/ROOT
[root@node2 ROOT]# vi index.jsp
1
2
3
<html>
<h1>Matrix</h1>
</html>
1
2
3
4
5
6
7
进入tomcat目录
[root@node1 ROOT]# cd /opt/modules/apache-tomcat-7.0.61/bin

启动tomcat
[root@node1 bin]# ./startup.sh

在地址行输入检查页面是否能正常显示:http://192.168.230.10:8080/

1.2.安装ip为192.168.230.11的主机的tomcat

1
2
3
[root@node2 software]# tar -zxvf apache-tomcat-7.0.61.tar.gz -C /opt/modules
[root@node2 software]# cd /opt/modules/apache-tomcat-7.0.61/webapps/ROOT
[root@node2 ROOT]# vi index.jsp
1
2
3
<html>
<h1>Matrix2</h1>
</html>
1
2
3
4
[root@node2 ROOT]# cd /opt/modules/apache-tomcat-7.0.61/bin
[root@node2 bin]# ./startup.sh

在地址行输入检查页面是否能正常显示:http://192.168.230.11:8080/

2.配置nginx访问禁止允许

1
2
进入tengine的conf目录下
[root@node1 ~]# cd /opt/modules/tengine-2.1.0/conf

2.1.编辑nginx.conf文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[root@node1 conf]# vi nginx.conf
server {
listen 80;
server_name www.sparsematrix.com;

location / {
deny 192.168.230.10;
allow 192.168.230.0/24;
deny all;

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

查看nginx访问禁止配置是否成功

1
在地址栏输入:http://192.168.230.10/

1
在地址栏输入:http://192.168.230.18/

3.配置nginx用户认证

1
2
3
4
5
6
7
8
9
10
用户认证访问
模块ngx_http_auth_basic_module 允许使用"HTTP基本认证"协议验证用户名和密
码来限制对资源的访问。
location / {
auth_basic "closed site";
auth_basic_user_file /var/users;
}
Apache发行包中的htpasswd命令来创建user_file 文件
htpasswd -c -m /var/users username
注:需要安装httpd才可以使用上面命令

3.1.使用yum安装httpd

1
[root@node1 ~]# yum install httpd

3.2.编辑nginx.conf文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[root@node1 conf]# vi nginx.conf
server {
listen 80;
server_name www.sparsematrix.com;

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

auth_basic "Hello!";
auth_basic_user_file /opt/modules/htpasswd;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}

location /basic_status {
stub_status on;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
Apache发行包中的htpasswd命令来创建user_file 文件

[root@node1 ~]# htpasswd -m -c /opt/modules/htpasswd tom
New password:
Re-type new password:
Adding password for user tom

[root@node1 ~]# cd /opt/modules/tengine-2.1.0/conf
[root@node1 conf]# rm -rf .nginx.conf.swp

重新加载nginx服务

[root@node1 init.d]# service nginx reload

1
在浏览器地址栏中输入:http://192.168.230.10/

1
身份认证成功页面显示:

4.配置nginx访问状态监控

1
进入tengine的conf目录
1
[root@node1 ~]# cd /opt/modules/tengine-2.1.0/conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
编辑nginx.conf文件

[root@node1 conf]# vi nginx.conf
server {
listen 80;
server_name www.sparsematrix.com;

location / {
root /opt/html;
index index.html index.htm;
}
location /status {
stub_status on;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;

}
}

1
在浏览器地址栏中输入:http://192.168.230.10/status

5.配置nginx反向代理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[root@node1 ~]# cd /opt/modules/tengine-2.1.0/conf

编辑nginx.conf文件

[root@node1 conf]# vi nginx.conf
server {
listen 80;
server_name www.sparsematrix.com;

location / {
proxy_pass http://192.168.230.11:8080;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;

}
}

5.1.重新加载nginx服务

1
2
[root@node1 init.d]# service nginx reload
![](http://7xqkk1.com1.z0.glb.clouddn.com/16-3-2/84583251.jpg)
1
2
在地址行输入检查页面是否能正常显示:http://192.168.230.10/
页面显示的是http://192.168.230.11中的内容

6.配置nginx负载均衡

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
1.保证192.168.230.10:8080.192.168.230.11:8080都能正常运行

[root@node1 ~]# cd /opt/modules/tengine-2.1.0/conf
[root@node1 conf]# vi nginx.conf

upstream sparsematrix {
server 192.168.230.10:8080 weight=1;
server 192.168.230.11:8080 weight=1;
}

server {
listen 80;
server_name www.sparsematrix.com;

location / {
proxy_pass http://sparsematrix;
}


}

6.1.重新启动nginx服务

1
2
3
4
5
[root@node1 init.d]# service nginx stop
Stopping nginx: [ OK ]
[root@node1 init.d]# service nginx start
Starting nginx: [ OK ]
![](http://7xqkk1.com1.z0.glb.clouddn.com/16-3-2/84583251.jpg)
1
2
3
2.在地址行输入检查页面是否能正常显示:http://192.168.230.10/

访问一次显示:

1
在访问一次显示:

7.tengine新增健康检查模块

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
[root@node1 ~]# cd /opt/modules/tengine-2.1.0/conf
[root@node1 conf]# vi nginx.conf

upstream sparsematrix {
server 192.168.230.10:8080 weight=1;
server 192.168.230.11:8080 weight=1;
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 www.sparsematrix.com;

location / {
proxy_pass http://sparsematrix;
}
location /status {
check_status;
}


}

1
2
3
2.在地址行输入:http://192.168.230.10/status对tomcat服务器进行健康检查

访问显示两台tomcat都没有响应:

1
2
3
重新启动两台tomcat

再次访问http://192.168.230.10/status显示两台tomcat运行正常:

本文作者 : Matrix
原文链接 : https://matrixsparse.github.io/2016/10/02/tengine反向代理/
版权声明 : 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明出处!

知识 & 情怀 | 二者兼得

微信扫一扫, 向我投食

微信扫一扫, 向我投食

支付宝扫一扫, 向我投食

支付宝扫一扫, 向我投食

留下足迹