Apache mod_jk not loading CSS

1.4k views Asked by At

I am using linux server with CentOS 7,Apache 2.4.23 with mod jk installed. mod jk is installed and configured properly which opens the site but its not loading the css of that site.

I have tried various rewrite rules too but the problem remains same.

below are the configurations which I have used in my httpd.conf for mod_jk

RewriteRule ^/(.*)$ /test/$1 [PT]

JkMount /* test

worker.properties:

worker.list=test 

worker.test.type=ajp13 
worker.test.host=localhost 
worker.test.port=8009 

all mod_jk configurations are in httpd.conf and CSS is in Tomcat

no any changes has been made in context file of Tomcat.

server.xml

shutdown port = 8005
http port = 8080
ajp port = 8009

Thanks

3

There are 3 answers

4
Ghayel On BEST ANSWER

Add all your html, images and css folders in apache server and your java classes in tomcat/webapps/project/WEB-INF/classes and all will start working fine

0
Dushyant Gohil On

This is Project Structure

enter image description here

link rel="stylesheet" href="${pageContext.request.contextPath}/resources/css/style.css" type="text/css"

8
jlumietu On

You cannot access your css because the link is incorrect.

According to your project structure, your css is in: project_root/webapp/resources/style.css, and the link to access it should be http://[host:port]/resources/style.css.

Instead of this, you are typing your css url as:

"${pageContext.request.contextPath}/resources/css/style.css"

where it should be:

"${pageContext.request.contextPath}/resources/style.css"

If you change this param in your css link I bet it would be returned as expected.

Other thing is the convenience or not of serving the css, js and other static stuff straight from Apache Httpd (or any other web server) instead of from Tomcat. There are different oppinions on it, specially if you provide Apache APR libraries to Tomcat. I personally prefer it. In this case, once after you copied/moved/redirect via alias your static elements to Apache, your JKMount strategy should be more complex. This could be one approach:

JkMount /test/* test
JkUnMount /test/resources/* test

EDIT:

I've been looking again to your config and I finally realized that the problem is in fact the RewriteRule you are using.

What RewriteRule ^/(.*)$ /test/$1 [PT] actually does is rewrite every request to that host (or virtual host) so adding an additional /test/ context path to the beginning of the request path.

It works OK for the first request, so that it takes the http://server.com/ request and rewrites it to http://server.com/test/. After it the JKMount redirects the request to the tomcat, as it redirects every mach to test worker and as you are applying a universal expression (/*) every request gets redirected to tomcat.

So, the first request does like this:

http://server.com/ > http://server.com/test/

But any subsequent resource or link (including css resources) within you Tomcat app will actually have the /test/ context correctly setted (at least the css you are trying to load). So, the css link /test/resources/css/style.css get rewrited too, and it ends this way: /test/test/resources/css/style.css which is an incorrect url.

Now, to avoid it, my suggestion is to change your RewriteRule to only manage the call to root element, this way:

RewriteRule ^/$ /test/ [PT]

If you do it like this, just the initial request to http://server.com/ will be rewritten to http://server.com/test, and any subsequent resource, link or form action, as your whole application is managed by spring mvc, will already have the /test context path mapping in the uri