本文介绍在阿里云上开通二级域名,并使用单个tomcat部署多个应用和ngnix+tomcat(多个)两种方式实现多个应用的部署,以下为操作步骤。
通过CNAME开通二级域名解析
开通二级域名解析,如下图所示,通过CNAME解析后会生成blog.admineap.com的二级域名。
在本实验中,顶级域名和二级域名同时指向同一IP,如果单个tomcat绑定顶级域名和二级域名的应用可通过Tomcat的Host配置实现;
如果部署了多个tomcat,可通过ngnix的方式实现;
下面分别介绍这两种方法
方法1:tomcat通过host绑定多个域名
在tomcat的server.xml的配置文件中新增一处host配置,指向二级域名blog.admineap.com对应的应用
<Engine name="Catalina" defaultHost="localhost">
<Realm className="org.apache.catalina.realm.LockOutRealm">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"resourceName="UserDatabase"/>Realm>
<Host name="localhost" appBase="webapps"unpackWARs="true" autoDeploy="true" debug="0">
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs/main"prefix="localhost_access_log." suffix=".txt"pattern="%h %l %u %t "%r" %s %b" />
<Context path="" docBase="/AdminEAP-web" reloadable="true"/>
<Host name="blog.admineap.com" appBase="webapps"unpackWARs="true" autoDeploy="true" debug="0">
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs/blog"prefix="localhost_access_log." suffix=".txt"pattern="%h %l %u %t "%r" %s %b" />
<Context path="" docBase="/Blog" reloadable="true"/>
需要注意的是:在第一个host的name可以配置成www.admineap.com,也可以配置成localhost,此处配置成localhost是因为www.admineap.com以后,tomcat的热部署(通过客户端mvn tomcat7:redeploy)失败,因为连不上tomcat服务器。
方法2:nginx+tomcat绑定二级域名
为了使得单个tomcat的压力不要太大,可在服务器部署多个tomcat(可用不同的ip地址),nginx作为代理服务器既可以作为静态资源服务器,也可以作为负载均衡服务器,可以将同一域名的请求分发多个应用服务器,也可以将不同的域名的请求分发到不同的服务器(本文使用的方法);
(1) 安装nginx,修改配置
upstream admineap {
server localhost:8080; #多个服务器可部署集群
#server localhost:8081;}
upstream admineap_blog {
server localhost:8081;}
server {
listen 80; server_name www.admineap.com; #charset koi8-r; #access_log logs/host.access.log main; location / {
#root html; #index index.html index.htm; proxy_pass http://admineap; proxy_set_header Host $host:$server_port; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Scheme $scheme; proxy_connect_timeout 3; proxy_read_timeout 3; proxy_send_timeout 3; access_log off; break; }
#error_page 404 /404.html; # redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html; location = /50x.html {
root html; }
# proxy the PHP scripts to Apache listening on 127.0.0.1:80 #
#location ~ \.php$ {
# proxy_pass http://127.0.0.1; #}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 #
#location ~ \.php$ {
# root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #}
# deny access to .htaccess files, if Apache's document root # concurs with nginx's one #
#location ~ /\.ht {
# deny all; #}
}
server {
listen 80; server_name blog.admineap.com; #charset koi8-r; #access_log logs/host.access.log main; location / {
#root html; #index index.html index.htm; proxy_pass http://admineap_blog; proxy_set_header Host $host:$server_port; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Scheme $scheme; proxy_connect_timeout 3; proxy_read_timeout 3; proxy_send_timeout 3; access_log off; break; }
(2) 启动tomcat,查看效果
原文:https://blog.csdn.net/jrn1012/article/details/70598363
欢迎访问我的博客: www.upint.cn