Apache 2.4反向代理JBoss7项目时CSS/JS资源404问题求助
Ah, I’ve run into this exact problem before—when you proxy a JBoss app through Apache with a simplified path, the relative resource URLs break because the browser doesn’t know about the underlying JBoss context root. Let’s break down why this happens and how to fix it.
Why You’re Seeing 404s
Say you’ve set up Apache to rewrite http://yourdomain.com/myapp to http://localhost:8080/actual-jboss-app. When your JBoss app serves a page with relative paths like ./css/style.css, the browser will interpret that relative to the URL it’s viewing: http://yourdomain.com/css/style.css—not the correct proxied path http://yourdomain.com/myapp/css/style.css. Since Apache doesn’t have that resource at the root, you get a 404.
The Best Fix: Use ProxyPass + ProxyHTML (Instead of Just Rewrite)
Rewrite alone only handles incoming requests, but it doesn’t fix the paths embedded in the HTML response. Apache’s mod_proxy_html module can rewrite those paths for you, and combining it with ProxyPass makes the setup clean and reliable.
Step 1: Enable Required Modules
First, make sure these Apache modules are enabled (run these commands on your server):
a2enmod proxy proxy_http rewrite proxy_html deflate
Step 2: Update Your Virtual Host Config
Here’s a complete example configuration that fixes the 404 issue. Let’s assume:
- Your simplified access path:
http://yourdomain.com/myapp - JBoss app’s actual context root:
http://localhost:8080/actual-jboss-app
Add this to your virtual host file:
<VirtualHost *:80> ServerName yourdomain.com # Enable HTML rewriting to fix relative paths ProxyHTMLEnable On # Map JBoss's context root to your simplified path ProxyHTMLURLMap /actual-jboss-app /myapp # Handle compressed content (so ProxyHTML can modify it) SetOutputFilter INFLATE;proxy-html;DEFLATE # Proxy all requests to /myapp to the JBoss app ProxyPass /myapp http://localhost:8080/actual-jboss-app ProxyPassReverse /myapp http://localhost:8080/actual-jboss-app # Fix redirect headers from JBoss (so Location headers use your simplified path) ProxyPassReverse / http://localhost:8080/actual-jboss-app/ </VirtualHost>
How This Works
ProxyPassandProxyPassReversehandle forwarding requests to JBoss and adjusting response headers (like redirects) to use your simplified path.ProxyHTMLURLMapscans every HTML response from JBoss and replaces all instances of/actual-jboss-appwith/myapp. This ensures relative paths in your CSS/JS links are interpreted correctly by the browser.- The
SetOutputFilterline ensures Apache can modify compressed responses (if JBoss serves gzipped content), which is crucial for the HTML rewriting to work.
Alternative: If You Must Use Rewrite (Not ProxyPass)
If you prefer sticking with Rewrite rules, you’ll need to add a rule to catch resource requests and forward them to the correct proxied path, plus fix the HTML content paths. Here’s how:
<VirtualHost *:80> ServerName yourdomain.com RewriteEngine On # Proxy /myapp requests to JBoss RewriteRule ^/myapp/(.*)$ http://localhost:8080/actual-jboss-app/$1 [P,L] # Catch resource requests that the browser is sending to the root (e.g., /css/style.css) # Adjust this regex based on your resource paths RewriteRule ^/(css|js|images)/(.*)$ http://localhost:8080/actual-jboss-app/$1/$2 [P,L] # Still need ProxyHTML to fix paths in the response ProxyHTMLEnable On ProxyHTMLURLMap /actual-jboss-app /myapp SetOutputFilter INFLATE;proxy-html;DEFLATE </VirtualHost>
This works, but it’s less scalable—you have to explicitly list every resource directory. The ProxyPass approach is better for most cases.
Quick Checks to Verify
- After updating the config, restart Apache:
systemctl restart apache2 - Clear your browser cache (old cached paths might still cause issues)
- Check the page source in your browser—you should see resource paths pointing to
/myapp/css/style.cssinstead of/css/style.css
内容的提问来源于stack exchange,提问作者Black.Jack

