简体   繁体   中英

link apache web server on port 80 and tomcat webapp on port 8080

On port 80 I have normal apache web server.

On port 8080 I have tomcat with client and server side stuff.

My goal is:

www.mydomain.com renders a static and SEO friendly index.html while javascript stuff is loading.

In the header of this index.html I load www.mydomain.com:8080/myapp/stuff.js

stuff.js is compiled with gwt and calls a RootLayoutPanel.get().add(nice_panel) which will remove static content and show dynamic widgets. It also calls servlets (server side code).

Problem: for security reasons, browsers wont let me load www.mydomain.com:8080/myapp/stuff.js because it is on a different port.

Wrong attempt: I tried to create a symlink from "normal" apache web server directory to the tomcat webapp containing stuff.js. I am now able to load stuff.js because its url is: www.mydomain.com/mysymlink_to_tomcat/stuff.js. But stuff.js is not able anymore to call servlets on server side again because of browsers security rules ("XMLHttpRequest cannot load ... origin ...is not allowed by Access-Control-Allow-Origin").

I would like to avoid the "crazy" solution of redirect from index.html to tomcat with header('location: http://mydomain.com:8080/another_index_on_tomcat.html '). This solution works but it has many drawbacks (SEO...)

What would be the best approach ?

Thanks.

You have basically two solutions:

  • make it work with the 2 origins: use the xsiframe linker in GWT to allow the page on :80 to load the script from :8080 (for readers: it's not about loading, it's about what the script does). Add the following to your `gwt.xml:

     <add-linker name='xsiframe' /> 

    That unfortunately won't solve your issue with GWT-RPC (o whatever you use to talk to the server). For that, there's CORS .

  • use a single origin: use Apache's mod_proxy (or mod_jk ) to proxy your Tomcat through your Apache. Nobody will ever use :8080 , everything will go through :80 . See Using Tomcat with Apache HTTPD and a proxy at https://developers.google.com/web-toolkit/doc/latest/DevGuideServerCommunication#DevGuideRPCDeployment

And of course there's also the solution of ditching the HTTPD and serving everything with Tomcat (recent Java and Tomcat versions have fixed their slowness issues).

I'm not sure if this would avoid the security error, but you could try an iframe. On apache, you have the index and an iframe to the tomcat, where the JS loads inside the iframe. Dunno if that will help with the SEO problem.

The best solution would be to redirect the port 80 calls to 8080 on apache when the client call is asking for a tomcat application.

Install mod_jk on apache and configure it to mount a context on the path you want example: (edit /mods_enabled/jk.conf)

# Configure access to jk-status and jk-manager
# If you want to make this available in a virtual host,
# either move this block into the virtual host
# or copy it logically there by including "JkMountCopy On"
# in the virtual host.
# Add an appropriate authentication method here!
<Location /jk-status>
    # Inside Location we can omit the URL in JkMount
    JkMount jk-status
    Order deny,allow
    Deny from all
    Allow from 127.0.0.1
</Location>
<Location /jk-manager>
    # Inside Location we can omit the URL in JkMount
    JkMount jk-manager
    Order deny,allow
    Deny from all
    Allow from 127.0.0.1
</Location>

JkMount /*/myAppDir/* ajp13

Then add a virtual host in your site settings (edit /apache2/sites-enabled/)

    <VirtualHost *:80>
    . Here is the rest of the
    . of the config of
    . the host
    # Tomcat jk connector settings
    JkMount /*.jsp ajp13_worker
    JkMount /myAppDir/* ajp13_worker
    JkMount /myAppDir* ajp13_worker
    JKMount /manager* ajp13_worker
    JkMount /manager/* ajp13_worker
    </VirtualHost>

And you should also edit the server.xml file and inside the tag write and comment the previous Host name="localhost"

<!-- Define an AJP 1.3 Connector on port 8009 -->

<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />

<Host name="localhost" appBase="webapps" unpackWARs="true"
   autoDeploy="true" >

          <Context path="/" docBase="/var/lib/tomcat7/webapps/myAppDir/"
                       debug="0" reloadable="true" />
          <!-- please notes on logs down below -->
     <Valve className="org.apache.catalina.valves.AccessLogValve"
             directory="/var/lib/tomcat7/logs" prefix="tomcat_access_" 
            suffix=".log" pattern="common" resolveHosts="false" />
  </Host>

The only thing left to do is edit the workers.properties file and add

worker.myapp2.port=8009
worker.myapp2.host=localhost
worker.myapp2.type=ajp13
worker.loadbalancer.type=lb
worker.loadbalancer.balance_workers=ajp13_worker

Then you should be set to work, and when a url containing the myAppDir appears, the apache server will redirect the calls to tomcat the answer will come back from apache.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM