前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >nginx+tomcat绑定二级域名,部署多个应用

nginx+tomcat绑定二级域名,部署多个应用

作者头像
三哥
发布2019-05-13 15:46:24
3.5K0
发布2019-05-13 15:46:24
举报
文章被收录于专栏:java工会

本文介绍在阿里云上开通二级域名,并使用单个tomcat部署多个应用和ngnix+tomcat(多个)两种方式实现多个应用的部署,以下为操作步骤。

通过CNAME开通二级域名解析

开通二级域名解析,如下图所示,通过CNAME解析后会生成blog.admineap.com的二级域名。

在本实验中,顶级域名和二级域名同时指向同一IP,如果单个tomcat绑定顶级域名和二级域名的应用可通过Tomcat的Host配置实现;

如果部署了多个tomcat,可通过ngnix的方式实现;

下面分别介绍这两种方法

方法1:tomcat通过host绑定多个域名

在tomcat的server.xml的配置文件中新增一处host配置,指向二级域名blog.admineap.com对应的应用

代码语言:javascript
复制
<Engine name="Catalina" defaultHost="localhost">

  <!--For clustering, please take a look at documentation at:
        /docs/cluster-howto.html  (simple how to)
        /docs/config/cluster.html (reference documentation) -->
  <!--
  <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
        -->

  <!-- Use the LockOutRealm to prevent attempts to guess user passwords
via a brute-force attack -->
  <Realm className="org.apache.catalina.realm.LockOutRealm">
    <!-- This Realm uses the UserDatabase configured in the global JNDI
resources under the key "UserDatabase".  Any edits
that are performed against this UserDatabase are immediately
available for use by the Realm.  -->
    <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
  </Realm>

  <Host name="localhost"  appBase="webapps"
unpackWARs="true" autoDeploy="true" debug="0">

    <!-- SingleSignOn valve, share authentication between web applications
Documentation at: /docs/config/valve.html -->
    <!--
    <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
        -->

    <!-- Access log processes all example.
Documentation at: /docs/config/valve.html
Note: The pattern used is equivalent to using pattern="common" -->
    <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs/main"
prefix="localhost_access_log." suffix=".txt"
pattern="%h %l %u %t &quot;%r&quot; %s %b" />
        <Context path="" docBase="/AdminEAP-web" reloadable="true"/>
    <!--<Context path="" docBase="AdminEAP" debug="0" reloadable="true"/>-->
    <!--<Context docBase="AdminEAP" path="/AdminEAP" reloadable="true" debug="0"/>-->
  </Host>

  <Host name="blog.admineap.com"  appBase="webapps"
unpackWARs="true" autoDeploy="true" debug="0">

    <!-- SingleSignOn valve, share authentication between web applications
Documentation at: /docs/config/valve.html -->
    <!--
    <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
        -->

    <!-- Access log processes all example.
Documentation at: /docs/config/valve.html
Note: The pattern used is equivalent to using pattern="common" -->
    <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs/blog"
prefix="localhost_access_log." suffix=".txt"
pattern="%h %l %u %t &quot;%r&quot; %s %b" />
        <Context path="" docBase="/Blog" reloadable="true"/>     
  </Host>      
</Engine>

需要注意的是:在第一个host的name可以配置成www.admineap.com,也可以配置成localhost,此处配置成localhost是因为www.admineap.com以后,tomcat的热部署(通过客户端mvn tomcat7:redeploy)失败,因为连不上tomcat服务器。

方法2:nginx+tomcat绑定二级域名

为了使得单个tomcat的压力不要太大,可在服务器部署多个tomcat(可用不同的ip地址),nginx作为代理服务器既可以作为静态资源服务器,也可以作为负载均衡服务器,可以将同一域名的请求分发多个应用服务器,也可以将不同的域名的请求分发到不同的服务器(本文使用的方法);

(1) 安装nginx,修改配置

代码语言:javascript
复制
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

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2019-04-29,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 java工会 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
负载均衡
负载均衡(Cloud Load Balancer,CLB)提供安全快捷的流量分发服务,访问流量经由 CLB 可以自动分配到云中的多台后端服务器上,扩展系统的服务能力并消除单点故障。负载均衡支持亿级连接和千万级并发,可轻松应对大流量访问,满足业务需求。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档