Tomcat-虚拟主机配置
1、准备测试代码目录
mkdir -p /opt/{code1,code2,code} echo "tomcat code1" >/opt/code1/index.html echo "tomcat code2" >/opt/code2/index.html echo "/zh tomcat code " >/opt/code/index.html
2、配置tomcat
2.1、需求
# 配置2台虚拟主机,分别如下:
tomcat1.cyc.com
tomcat2.cyc.com
2.2、配置tomcat
2.2.1、配置server.xml
]# vi /usr/local/tomcat/conf/server.xml ... <Engine <Host name="code1.cyc.com" appBase="/opt/code1" unpackWARs="true" autoDeploy="true"> <Context docBase="/opt/code1" path="/" reloadable="true"/> <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="code1_access_log" suffix=".txt" pattern="%h %l %u %t "%r" %s %b" /> </Host> <Host name="code2.cyc.com" appBase="/opt/code2" unpackWARs="true" autoDeploy="true"> <Context docBase="/opt/code2" path="/" reloadable="true"/> <Context docBase="/opt/code" path="/zh" reloadable="true"/> <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="code1_access_log" suffix=".txt" pattern="%h %l %u %t "%r" %s %b" /> </Host> ... </Engine>
2.2.2、配置解析
1、context的使用方式(如果设定了context,没有创建目录则会造成无法启动Tomcat 2、Context属性解析 <Context docBase="/opt/code" path="/zh" reloadable="true"/> context: 可以理解是nginx的Location path: 虚拟目录 docBase: 网页实际存放位置的根目录,映射为path虚拟目录 reLoadable="true": 修改了jsp文件后不需要重启就可以实现显示的同步 理解:访问http://tomcat.cyc.com/zh --映射--> /opt/code
3、测试访问
3.1、配置hosts
cat >>/etc/hosts<<'EOF' 192.168.10.5 code1.cyc.com 192.168.10.5 code2.cyc.com EOF
3.2、重启tomcat
systemctl restart tomcat
3.2、测试访问
~]# curl http://code1.cyc.com:8080 tomcat code1
~]# curl http://code2.cyc.com:8080 tomcat code2
~]# curl http://code2.cyc.com:8080/zh/index.html /zh tomcat code