背景:
我的服务器安装的系统是CentOS 6.7,httpd是通过yum安装的,默认的版本是2.2,因为Tomcat处理http连接不是很在行,我们需要在前端添加一个WEB服务器,来反向代理后面的Tomcat,因为Tomcat有AJP协议,并且apache也支持AJP协议,所以我们就选定了httpd。
访问的效果是,访问域名http://www.wzlinux.com 自动重定向到https://www.wzlinux.com 然后再反向代理到后端的Tomcat。
部署:
1、首先配置tomcat虚拟主机,以及确认AJP开启
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
#开启AJP协议连接器
<
Connector
port
=
"8009"
protocol
=
"AJP/1.3"
maxHttpHeaderSize
=
"8192"
enableLookups
=
"false"
connectionTimeout
=
"20000"
URIEncoding
=
"utf-8"
acceptCount
=
"1000"
redirectPort
=
"8443"
/>
#配置虚拟主机
<
Host
name
=
"www.wzlinux.com"
appBase
=
"webapps"
unpackWARs
=
"true"
autoDeploy
=
"true"
>
<
Context
path
=
""
docBase= "/usr/local/tomcat/webapps/wzlinux"
reloadable
=
"true"
crossContext
=
"true"
/>
<
Valve
className
=
"org.apache.catalina.valves.AccessLogValve"
directory
=
"logs"
prefix
=
"wzlinux_log."
suffix
=
".txt"
pattern
=
"%h %l %u %t "%r" %s %b"
/>
</
Host
>
|
如果打算使用http协议反向代理,要获取用户的真实IP,需要把%h改为%{X-Forwarded-For}i即可。
2、安装httpd和ssl模块
1
|
yum
install
httpd mod_ssl -y
|
3、配置httpd
安装mod_ssl之后,会自动生成文件/etc/httpd/conf.d/ssl.conf,里面主要是SSL的参数设定,这里我们可以不用修改,使用默认即可。
为了实现我们的要求,我们创建配置文件/etc/httpd/conf.d/wzlinux.conf,内容如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
NameVirtualHost *:80
NameVirtualHost *:443
#在这段,我们开启重定向,使http转向https
<VirtualHost *:80>
ServerName www.wzlinux.com
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https:
//
%{SERVER_NAME}/$1 [R,L]
<
/VirtualHost
>
#真正的反向代理
<VirtualHost *:443>
ServerName www.wzlinux.com:443
SSLCertificateFile
/etc/httpd/cert/214226183630572
.pem
SSLCertificateKeyFile
/etc/httpd/cert/214226183630572
.key
ProxyVia On
ProxyRequests Off
ProxyPreserveHost On
#把主机名传给后端主机
ProxyPass / ajp:
//127
.0.0.1:8009/
#反向代理给后端主机
ProxyPassReverse / ajp:
//127
.0.0.1:8009/
<
/VirtualHost
>
|
4、启动服务
配置很简单,启动服务即可成功,如有问题可以查看日志,所有以上的功能都是由httpd的模块支持的,可以使用命令httpd -M查看支持的模块,确保以下模块都有即可。
1
2
3
4
|
proxy_module
proxy_balancer_module
proxy_ajp_module
ssl_module
|
附录:
如果在上一步的基础上,我们需要让httpd做负载均衡,配置文件只需修改成如下即可:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
NameVirtualHost *:80
NameVirtualHost *:443
<VirtualHost *:80>
ServerName www.wzlinux.com
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https:
//
%{SERVER_NAME}/$1 [R,L]
<
/VirtualHost
>
<VirtualHost *:443>
ServerName www.wzlinux.com:443
SSLCertificateFile
/etc/httpd/cert/214226183630572
.pem
SSLCertificateKeyFile
/etc/httpd/cert/214226183630572
.key
<Proxy balancer:
//wzlinux
>
BalancerMember ajp:
//127
.0.0.1:8009 loadfactor=1
BalancerMember ajp:
//10
.0.1.7:8009 loadfactor=2
ProxySet lbmethod=bytraffic
<
/Proxy
>
ProxyVia On
ProxyRequests Off
ProxyPreserveHost On
ProxyPass / balancer:
//wzlinux/
ProxyPassReverse / balancer:
//wzlinux/
<
/VirtualHost
>
|
本文转自 wzlinux 51CTO博客,原文链接:http://blog.51cto.com/wzlinux/1957312,如需转载请自行联系原作者