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

本文介绍在阿里云上开通二级域名,并使用单个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">

  <!--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,修改配置

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

原文发布于微信公众号 - java工会(javagonghui)

原文发表时间:2019-04-29

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

扫码关注云+社区

领取腾讯云代金券