Tomcat9中多Angular项目rewrite.config规则配置求助
Alright, let's work through this problem together. I’ve dealt with exactly this scenario—multiple Angular SPAs on Tomcat with refresh 404s and inconsistent loading—so here’s what’s going wrong and how to fix it:
First, Fix the Rewrite Rules
Your current rewrite.config has two key issues: incorrect variable usage and redundant (and potentially conflicting) condition bindings. Here’s the corrected version, with explanations:
# Skip requests for actual files or directories (like JS, CSS, images) RewriteCond %{REQUEST_URI} !-f RewriteCond %{REQUEST_URI} !-d # Rewrite app1's frontend routes to its index.html RewriteRule ^/app1/(.*)$ /app1/index.html [L] # Rewrite app2's frontend routes to its index.html RewriteRule ^/app2/(.*)$ /app2/index.html [L] # Rewrite app3's frontend routes to its index.html RewriteRule ^/app3/(.*)$ /app3/index.html [L]
What changed:
- Used
%{REQUEST_URI}instead of%{REQUEST_PATH}: Tomcat’s RewriteValve usesREQUEST_URIas the variable for the full request path (notREQUEST_PATH, which isn’t a valid variable here). Your original conditions were likely evaluating incorrectly, causing rules to misfire. - Shared conditions for all rules: Instead of repeating
!-ffor every rule, we set the "skip real files/dirs" conditions once at the top. These apply to all subsequent rules, which is cleaner and avoids redundancy. - Added
[L]flag: This tells Tomcat to stop processing further rules once a match is found. This prevents, for example, an/app2/...request from accidentally matching a later rule (though unlikely here, it’s a best practice for rewrite rules). - Explicit
$in regex: The$at the end of^/app1/(.*)$ensures we match the entire path, avoiding partial matches that could cause unexpected behavior.
Verify Your Tomcat Configuration
Make sure you’ve properly enabled the RewriteValve in server.xml (inside your <Host> block):
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true"> <!-- ... other existing config ... --> <Valve className="org.apache.catalina.valves.rewrite.RewriteValve" /> </Host>
This ensures Tomcat actually processes your rewrite rules.
Fix the 304 Not Modified Issue
The 304 error means your browser is caching a previous (failed) response for app2/app3’s index.html. To resolve this:
- Clear your browser cache: Hard refresh (Ctrl+Shift+R or Cmd+Shift+R) on app2/app3’s pages to bypass cached content.
- Ensure Angular builds use proper base hrefs: When building each Angular app, specify the base href to match its Tomcat context path:
- For app1:
ng build --base-href /app1/ - For app2:
ng build --base-href /app2/ - For app3:
ng build --base-href /app3/
This ensures Angular loads static assets (JS, CSS) from the correct path, preventing failed requests that get cached.
- For app1:
- Optional: Disable caching for index.html: If you keep running into cache issues, you can add a Valve to Tomcat to set no-cache headers for
index.htmlfiles. Add this inside your<Host>block inserver.xml:<Valve className="org.apache.catalina.valves.CacheControlValve" path="*.html" noCache="true" />
Test the Fix
- Restart Tomcat to apply the new rewrite rules and config changes.
- Load each app, log in, navigate to a route, and refresh—you should no longer get 404s.
- Verify app2 and app3 load their
index.htmlcorrectly (check the network tab in browser dev tools to confirm the index.html is loaded with a 200 status, not 304).
内容的提问来源于stack exchange,提问作者user7460319

