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,查看效果
二級域名指向的應用

頂級域名指向的應用

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章