Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

I have to run a WordPress site served via Apache2 & Java-based webapp using Tomcat on the same server.

When users come to example.com or example.com/public-pages they need to served from WordPress but when they come to example.com/private-pages they need to be served from the Tomcat.

I have asked this question on serverfault where they suggested using different port, different IP & sub-domain.

I want to go for different port solution since it will mean I need to buy only one SSL certificate.

I tried doing the reverse proxy method by having the following in my default-ssl.conf

    <VirtualHost _default_:443>
            ServerAdmin webmaster@localhost
            ServerName localhost:443

            DocumentRoot /var/www

            <Directory /var/www>
            #For Wordpress
                    Options FollowSymLinks
                    AllowOverride All
            </Directory>

            <Proxy *>
                    Order deny,allow
                    Allow from all
            </Proxy>

            ProxyRequests           Off
            ProxyPass               /private-pages       ajp://localhost:8009/
            ProxyPassReverse        /private-pages       ajp://localhost:8009/


            SSLEngine on
            SSLProxyEngine On


            SSLCertificateFile      /etc/apache2/ssl/apache.crt
            SSLCertificateKeyFile /etc/apache2/ssl/apache.key

    </VirtualHost> 

As you have noticed I am using mod_proxy_ajp in Apache2 for this. And that my Tomcat is listening to port 8009 and then serving content. So now when I go to example.com/private-pages I am seeing the content from my Tomcat. But 2 issues are happening.

  1. All my static resources are getting 404-ed, so none of my images, CSS, js are getting loaded. I see that the browser is requesting for the resources using URL example.com/css/* This will clearly not work because it translates to example.com:80/css/* instead of example.com:8009/css/* & there are no such resources in the WordPress directory.
  2. If I go to example.com/private-pages/abcd I am somehow kicked to the WordPress site (which obviously displays a 404 page).

I can understand why #1 is happening but have no clue why the #2 is happening. Regardless, if there is another clean solution for resolving this, I would appreciate y'alls help.

share|improve this question

2 Answers 2

I suggest you use mod_rewrite to specify which directory gets the reverse proxy. Part of your problem is that the rewrite rules for WordPress are stealing requests from the proxy. If you use mod_rewrite for the proxy, then you can specify [L] to indicate that the proxy rule is the last rewrite rule and that the WordPress rewrite rules shouldn't be used.

ProxyRequests off
RewriteEngine on
RewriteRule ^/private-pages  ajp://localhost:8009/  [P,L]
ProxyPassReverse /private-pages ajp://localhost:8009/

To solve your CSS problem, you could use mod_proxy_html which could be configured to rewrite all the HTML that comes back through the proxy and replace references to /css/ with /private-pages/css.

Alternately, you could modify the web application that is running inside Tomcat7 to be proxy aware. mod_proxy can be configured to send via: headers. It also sends x-forwarded headers that your web app could examine and know that it is running in a reverse proxy situation.

share|improve this answer
up vote 1 down vote accepted

I was able to finally resolve the issue by doing couple of things.

  1. Renamed the WAR file in my tomcat, in this case private-pages
  2. In my virtual hosts definition I changed ajp:// to reflect the above change.

Here is virtual hosts file,

<VirtualHost _default_:443>
        ServerAdmin webmaster@localhost
        ServerName localhost:443

        DocumentRoot /var/www

        <Directory /var/www>
        #For Wordpress
                Options FollowSymLinks
                AllowOverride All
        </Directory>

        <Proxy *>
                Order deny,allow
                Allow from all
        </Proxy>

        ProxyPreserveHost On
        ProxyRequests           Off
        ProxyPass               /private-pages       ajp://localhost:8009/private-pages
        ProxyPassReverse        /private-pages       ajp://localhost:8009/private-pages

        <Location /private-pages>
            Order allow,deny
            Allow from all
        </Location>



        SSLEngine on
        SSLProxyEngine On


        SSLCertificateFile      /etc/apache2/ssl/apache.crt
        SSLCertificateKeyFile /etc/apache2/ssl/apache.key

</VirtualHost> 
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.