You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

引入AWS Classic Load Balancer后EC2站点页面更新不生效问题求助

Classic Load Balancer Caching Explained (And How to Fix Your Stale Content Issue)

Yep, you’re spot-on—AWS Classic Load Balancers (CLB) absolutely do cache static content like HTML files, even when you only have one EC2 instance behind them. This is almost certainly why your updates aren’t showing up, and why the page still loads even after deleting it from your EC2 server. Let’s break down what’s happening and how to fix it:

Why This Happens

Classic Load Balancers use connection pooling and response caching to optimize performance. By default, if your Tomcat server doesn’t send explicit Cache-Control or Expires HTTP headers with static content, the CLB will automatically cache those responses for a short period (usually a few minutes, but it can vary). Even worse, the CLB might reuse existing connections to your EC2 instance instead of fetching fresh content, even if the file on the server has changed.

Step 1: Verify It’s Definitely the CLB Caching

First, confirm the issue is with the load balancer and not something else:

  • Directly access your EC2 instance’s public IP (not your domain that points to the CLB). If the content here is updated/deleted as expected, but your domain still shows the old page, the CLB is definitely the culprit.
  • Try a hard refresh (Ctrl+F5 or Cmd+Shift+R) in your browser—if the page still doesn’t update, it’s not browser caching (which would be cleared by a hard refresh).

Step 2: Fix the Stale Content Immediately

Since CLBs don’t have a manual "clear cache" button, use one of these quick fixes:

  • Deregister and re-register your EC2 instance: Go to the EC2 Console → Load Balancers → Select your CLB → Instances tab → Deregister your EC2 instance, wait 2-3 minutes, then re-register it. This forces the CLB to reset its connections and clear any cached content for that instance.
  • Rename your HTML file: If you need immediate access to the updated content, rename the file (e.g., index.html → index_v2.html) and access the new filename via your domain. The CLB won’t have a cached version of the new URL.

Step 3: Prevent This From Happening Long-Term

The best fix is to configure your Tomcat server to send explicit caching headers that tell the CLB (and browsers) how to handle content:

  1. Edit your Tomcat web.xml file (usually located at /opt/tomcat/conf/web.xml on AWS EC2).
  2. Add the following filter to disable caching for HTML files (while allowing caching for static assets like CSS/JS to keep performance high):
    <filter>
        <filter-name>ExpiresFilter</filter-name>
        <filter-class>org.apache.catalina.filters.ExpiresFilter</filter-class>
        <!-- Disable caching for HTML -->
        <init-param>
            <param-name>ExpiresByType text/html</param-name>
            <param-value>access plus 0 seconds</param-value>
        </init-param>
        <!-- Cache CSS/JS for 1 month -->
        <init-param>
            <param-name>ExpiresByType text/css</param-name>
            <param-value>access plus 1 month</param-value>
        </init-param>
        <init-param>
            <param-name>ExpiresByType application/javascript</param-name>
            <param-value>access plus 1 month</param-value>
        </init-param>
        <!-- Cache images for 3 months -->
        <init-param>
            <param-name>ExpiresByType image/jpeg</param-name>
            <param-value>access plus 3 months</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>ExpiresFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    
  3. Restart Tomcat to apply the changes:
    sudo systemctl restart tomcat
    

This setup tells the CLB to never cache HTML files, so any updates will show up immediately. Static assets like CSS and images will still be cached to improve performance, which is ideal for most websites.


内容的提问来源于stack exchange,提问作者Sagar D

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:52:40