Nginx+tomcat繫結二級域名,部署多個應用
阿新 • • 發佈:2019-02-05
原文地址:https://blog.csdn.net/jrn1012/article/details/70598363
本文介紹在阿里雲上開通二級域名,並使用單個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 "%r" %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 "%r" %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,檢視效果