简体   繁体   中英

jRuby deployment into a subfolder in Tomcat

While developing a jRuby application using webbrick as the webserver, all my code is written with http://localhost:3000 as the root.

When deploying to Tomcat, I create a WAR file and it creates a subfolder under the webapps/ folder: localhost:8080/project_name/

This causes a load of problems with my code. Is there anything I can do in my ruby routes.rb file to deal with this?

Should I resort to using some sort of virtual host in Tomcat?

Although this question is quite old, we ran into a similar problem with a current project of ours. The project is done with JRuby on Rails, using Tomcat 6.x (7.0.26 in development for testing purposes), needing multiple deployments of the same application on the same servlet container, having individual server names. It took me quite some time to find a good enough solution and I like to share it with you.

Continue reading, if root context is no option for you (as cosmikduster described it above). If it is, use it.

TL;DR

Use virtual hosts for each app provided by Tomcat to give yourself root context for individual apps deployed on the Tomcat servlet container. The explanation provided below is based on this and is just an illustrative example with a step by step solution attached.

The problem

The problem arises when deploying a *.war file to Tomcat, as per default, the deployed app, let's call it awesome_app_uno for now, will live under http://localhost:8080/awesome_app_uno . This is all fine and dandy but can cause problems with the internal path generation of rails, which by default will now use /awesome_app_uno for every path generated.

Explanation & initial setup

Now, JRuby Rack , which is the rack implementation for JRuby is kind enough to include a feature that automatically sets the relative_url_root for you, I quote from the README:

The Rails controller setting ActionController::Base.relative_url_root is set for you automatically according to the context root where your webapp is deployed.

Normally, I would cheer - but in the real world, we'd probably do not want something like this, as we'll have something like a proxy to get ourselves some nice domainname, eg

http://awesomeappuno.com/

And now we're screwed. If we take a naive solution for this, we'd probably end up using an Apache (or an nginx, whatever you like) to function as a proxy. We'll want something like

http://awesomeappuno.com/ -> http://localhost:8080/awesome_app_uno

Just for the fun of it, lets look at the configuration for an apache config file:

<VirtualHost *:80>
    ServerName awesomeappuno.com

    ErrorLog /var/log/apache2/awesomeappuno_error.log

    LogLevel warn

    CustomLog /var/log/apache2/awesomeappuno_access.log combined

    <Proxy *>
        Order allow,deny
        Allow from all
    </Proxy>
    ProxyPreserveHost On
    ProxyRequests off

    ProxyPass          / http://localhost:8080/awesome_app_uno
    ProxyPassReverse   / http://localhost:8080/awesome_app_uno

</VirtualHost>

Kindly stolen from here .

The madness

With that being your configuration, you can now visit http://awesomeappuno.com .

This is where you'll notice, that despite now having a clean root without prefixes, Rails will still generate paths with the /aweseome_app_uno prefix, ie

http://awesomeappuno.com/awesome_app_uno/the/path/you/wanted

This is generally undesirable. While some of you could propably live with this, I couldn't. Moreover, there's awesome_app_dos coming up on the horizon which needs to be deployed on the same Tomcat instance, needing it's own domain name http://awesomeappdos.com , along with the two ugly cousins awesome_staging and awesome_integration behind it. The gist is, if you're forced to use the same Tomcat container for all these apps, giving on app root context is not really an option.

You can try some solutions like writing custom initializers, resetting the ENV hash, which gets initially filled by the jruby-rack (see comments and answers above). You might find yourself rewriting the paths manually or using absolute urls. Or you create hacks for a single environment. Or you could try to use rewrites. Or mod_subsitute . You could even try using multiple tomcat instances running everything under root context.

Don't do that.

Please.

Virtual Hosts to the rescue

While reading up on the issue (and trying everything described in the last paragraph), I stumpled on an entry in the ruby forum .

You can create virtual hosts with Tomcat, providing a root context for each app. To get more in depth knowledge, you can start here or here . In short, suppose your Tomcat (7.x) is located under /usr/local/tomcat , this would be the quick and dirty version:

Create a virtual host

The host will have it's appBase within the tomcat installation for this. If this isn't your stale, change it.

( cd /usr/local/tomcat/libexec )

I am using an installation via homebrew here, I am on a mac under OSX 10.7.4 - however with slight alterations this should work on a standard Debian as well.

Open up conf/server.xml and within the engine tag create another host:

<Host name="awesome_app_uno" alias="awesomeappuno" appBase="awesome_app/uno" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">
    <Context path="" docBase="ROOT"></Context>
</Host>

After that, create a file called conf/Catalina/awesome_app_uno/ROOT.xml . These are the contents:

<?xml version="1.0" encoding="UTF-8"?>
<Context docBase="${catalina.base}/awesome_app/uno/ROOT.war">
</Context>

Afterwards, place the awesomeappuno.war as /usr/local/tomcat/libexec/awesome_app/uno/ROOT.war and restart Tomcat. You can check the virtual host via http://localhost:8080/host-manager/html .

You should be able to visit http://awesome_app_uno:8080 and find your app there. Paths should now be correct, however, port 8080 isn't that user friendly. With slight changes to the previously shown apache config, we can also get a nice name for this:

<VirtualHost *:80>
    ServerName awesomeappuno.com

    ErrorLog /var/log/apache2/awesomeappuno_error.log

    LogLevel warn

    CustomLog /var/log/apache2/awesomeappuno_access.log combined

    <Proxy *>
        Order allow,deny
        Allow from all
    </Proxy>
    ProxyPreserveHost On
    ProxyRequests off

    ProxyPass          / http://awesome_app_uno:8080/
    ProxyPassReverse   / http://awesome_app_uno:8080/

</VirtualHost>

Reload your apache and you should now have no further problems with pathing, routing, because everything now happens under the root context of the tomcat virtual host.

Rinse and repeat for additional apps on the same tomcat instance.

The end

Hope i could help you with this.

lg,

flo

There is code in JRuby-Rack to deal with this. Depending on the version of Tomcat and/or Rails, it may not be detecting the extra context path correctly.

The environment variable that is supposed to take effect is called ENV['RAILS_RELATIVE_URL_ROOT'] . You might print out the value of that expression during boot time and see whether it's getting set when you run in Tomcat.

The code in question is here:

https://github.com/nicksieger/jruby-rack/blob/master/src/main/ruby/jruby/rack/rails.rb#L32-38

The versions of Tomcat, Rails and JRuby-Rack you're using would help diagnose the problem further.

Your app should almost never care about the host/domain/port it will run under. To deploy at the relative path / , instead of /myapp , simply rename the WAR to ROOT.war instead of myapp.war when you copy it to the tomcat/webapps folder.

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