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

本文介绍如何在阿里云上配置二级域名并通过单个Tomcat部署多个应用,以及使用Nginx+Tomcat集群的方式实现负载均衡。具体包括通过CNAME记录设置二级域名,配置Tomcat的server.xml文件支持不同域名访问不同应用,以及Nginx的代理配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >




本文介绍在阿里云上开通二级域名,并使用单个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>
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58

需要注意的是:在第一个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</span>:<span class="hljs-variable">$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</span>:<span class="hljs-variable">$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;
        }
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90

(2) 启动tomcat,查看效果
二级域名指向的应用

顶级域名指向的应用




版权申明,本文转载于: https://blog.youkuaiyun.com/jrn1012/article/details/70598363
<think>嗯,用户问的是如何配置Tomcat的一级域名。我需要先理解什么是一级域名,通常指的是主域名,比如example.com这样的,不带www或者其他子域名。然后,配置Tomcat的一级域名可能涉及将域名指向服务器IP,并设置Tomcat来处理这个域名的请求。 首先,我应该考虑DNS设置,用户需要将域名解析到服务器的IP地址。这一步虽然不属于Tomcat的配置,但必须完成,否则后续步骤无法生效。可能需要提到A记录或者CNAME记录的设置。 接下来是Tomcat本身的配置。用户可能需要在server.xml文件中修改Connector和Host的配置。Connector部分可能需要调整端口,比如默认的8080改为80,但更常见的是使用反向代理如Nginx或Apache来处理80和443端口,然后转发到Tomcat的8080。不过用户可能想直接在Tomcat上配置,所以需要说明如何修改端口。 然后Host元素中的name属性应该设置为一级域名,比如example.com。同时,appBase要指定对应的应用目录,比如webapps/example。可能需要部署对应的WAR包到该目录,或者放置应用程序。 另外,可能需要配置Context元素,指定docBase和其他参数。不过默认情况下,Tomcat会自动部署appBase下的应用,所以可能不需要额外配置,除非有特殊需求。 还有可能涉及Engine的defaultHost属性,确保当请求的域名不匹配任何Host时,由哪个Host处理。需要确保defaultHost设置为正确的一级域名,避免错误路由。 另外,可能需要关闭自动部署和重新加载,特别是在生产环境中,以提高性能。这涉及到Host元素的autoDeploy和reloadable属性。 如果用户想通过域名直接访问ROOT应用,可能需要将应用部署为ROOT,或者修改Context的path为空。这需要说明如何重命名WAR文件为ROOT.war,或者配置Context的docBase和path。 还有防火墙和端口的考虑,如果直接使用Tomcat监听80端口,可能需要root权限,或者使用authbind等工具。不过更安全的做法是使用反向代理,所以可能需要简要提及这一点,但用户的问题可能更关注Tomcat本身的配置。 最后,可能需要重启Tomcat服务使配置生效,并检查日志是否有错误。还要确保SELinux或防火墙没有阻止端口访问。 总结步骤应该是:DNS解析→修改server.xml中的Host→部署应用→调整端口或使用反向代理→重启Tomcat→测试访问。需要分步骤说明,并给出示例配置,比如server.xml的具体修改部分,以及注意事项,比如权限问题、反向代理建议等。</think>配置 Tomcat 的一级域名(如 `example.com`)需要结合 **DNS解析** 和 **Tomcat服务配置**,以下是详细步骤: --- ### 一、DNS 解析设置 1. **域名解析** 将一级域名(如 `example.com`)通过 DNS 解析到你的服务器 IP: - 添加一条 `A 记录`:主机记录为 `@`,指向服务器 IP。 - 可选:添加 `CNAME` 记录将 `www.example.com` 也指向同一 IP。 -- ### 二、Tomcat 服务配置 修改 Tomcat 的 `conf/server.xml` 文件,配置域名绑定和虚拟主机。 #### 1. **修改 Connector 端口(可选)** - 如果直接通过 Tomcat 监听 80 端口(需 root 权限): ```xml <Connector port="80" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> ``` - **推荐方式**:使用 Nginx/Apache 反向代理到 Tomcat 的 8080 端口(更安全,无需 root 权限)。 #### 2. **配置虚拟主机(Host)** ```xml <Engine name="Catalina" defaultHost="example.com"> <Host name="example.com" <!-- 一级域名 --> appBase="webapps/example" <!-- 应用存放目录 --> unpackWARs="true" autoDeploy="false"> <!-- 生产环境建议关闭自动部署 --> <Context path="" <!-- 根路径访问应用 --> docBase="myapp" <!-- 应用的实际目录(位于appBase下) --> reloadable="false"/> <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="example_access_log" suffix=".txt" pattern="%h %l %u %t "%r" %s %b" /> </Host> </Engine> ``` #### 3. **部署应用程序** - 在 `webapps/example` 目录下放置你的应用(如 `myapp.war`)。 - 若需通过根域名直接访问(如 `example.com` 而不是 `example.com/myapp`): - 将应用重命名为 `ROOT.war`,或通过 `<Context path="" ...>` 指定路径。 --- ### 三、反向代理配置(以 Nginx 为例) 1. **安装 Nginx** ```bash sudo apt install nginx ``` 2. **配置反向代理** 创建文件 `/etc/nginx/sites-available/example.com`: ```nginx server { listen 80; server_name example.com www.example.com; location / { proxy_pass http://localhost:8080; # 转发到 Tomcat 的 8080 端口 proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-For $proxy_add_x_forwarded_for; } } ``` 启用配置并重启 Nginx: ```bash sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/ sudo systemctl restart nginx ``` --- ### 四、验证配置 1. **重启 Tomcat** ```bash sudo systemctl restart tomcat ``` 2. **访问测试** 浏览器输入 `http://example.com`,确认能正常访问应用。 -- ### 常见问题 1. **权限问题** - 若直接使用 80 端口,需以 root 启动 Tomcat(不推荐),或使用 `authbind` 工具。 2. **SELinux/防火墙** - 确保防火墙放行 80/443 端口: ```bash sudo ufw allow 80/tcp sudo ufw allow 443/tcp ``` 3. **多应用隔离** 若需在同一 Tomcat 部署多个一级域名,可为每个域名配置独立的 `<Host>` 块。 --- 通过以上步骤,即可将一级域名正确绑定Tomcat 服务。如有 HTTPS 需求,还需配置 SSL 证书(如 Let's Encrypt)。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值