2

I've got a working spring mvc app. I want to make it available for beta users to see. I've bought domain name/setup nameservers and all that dns stuff.

I access my app trough local host like this :

localhost:8080/myApp

But in the realworld I want to access it like mydomain.com. So I googled a lot, and found people recommend nginx for these things as "the fastest". So installed nginx with following configuration :

server {

  listen 80;

  server_name www.mydomain.com mydomain.com;
  location / {
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Server $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://localhost:8080;
    }

} #end server

The problem :

When I visit mydomain.com I get that well known tomcat page If you're seeing this page via a web browser, it means you've setup Tomcat successfully. Congratulations!.

If I manually go to mydomain.com/myApp then everything works as expected.

Question :

Is there a way for me to configure this to do the following :

When I type in the address bar mydomain.com that I get transfered to mydomain.com/myApp

Or I'm completely off in this case. There is easier way to do this?

update:

Per fvu suggestion when I change ROOT to some other directory then deploy my up in the ROOT directory I get 404 from tomcat :

type Status report

message

description The requested resource is not available.

I can retrieve this error from the server log :

SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.util.Log4jConfigListener
java.lang.IllegalStateException: Web app root system property already set to different value: 'webapp.root' = [/tomcat_9090/webapps/myApp/] instead of [/tomcat_9090/webapps/ROOT
/] - Choose unique values for the 'webAppRootKey' context-param in your web.xml files!

Update II :

When I change configuration to :

proxy_pass http://localhost:8080/myApp/;

The website looks like it's working, but it really doesn't. Links don't work, css/js doesn't load.

London
  • 14,986
  • 35
  • 106
  • 147
  • Easiest will be to install your app as the root app in Tomcat, [as explained here](http://stackoverflow.com/questions/715506/tomcat-6-how-to-change-the-root-application) – fvu Nov 23 '12 at 00:02
  • Re update: see http://stackoverflow.com/questions/5014651/webapproot-in-spring for info, solutions and useful pointers wrt this Spring related issue – fvu Nov 23 '12 at 00:26
  • also, could it be that you did not undeploy the app you originally loaded (as /myApp)? I would only leave 1 copy loaded at all times. – fvu Nov 23 '12 at 00:31
  • thanks fvu I did help me run it, I will keep this as a backup solution (it's not very clean you'd have to agree but it works) – London Nov 23 '12 at 00:33
  • Great :-) Agreed, it's not the cleanest solution imaginable, but putting apps in the root of a servlet container is always a bit messy... – fvu Nov 23 '12 at 00:39
  • If CSS, JS or images are not loading, might be related to a wrong base url: should be {base}/css/* (so nginx does the correct proxying) instead of {base}/myApp/css/* (like http://stackoverflow.com/questions/2521606/spring-mvc-absolute-url-problem). – edrabc Dec 13 '12 at 15:17

1 Answers1

2

If you configure nginx with proxy_pass http://localhost:8080/myApp/;, the only issue is how to create links and resources so they use the expected base-url path, instead of the default Tomcat Servlet path.

If you are able to use Spring Framework 3.1+, the new Profiles feature could help defining different base-url depending on the active profile, as other web frameworks do: the path will be decided in runtime, depending on the server or environment.

For instance, assuming at least 2 properties files (production.properties and dev.properties) declare the baseurl field on each of them:

  • production.properties

    baseurl = //yourdomain.com
    
  • dev.properties

    baseurl = //localhost:8080/myApp
    

Then using your favourite Spring way, load the properties for each different environment, like:

<util:properties id="properties" location="classpath:META-INF/default.properties" />
....
<beans profile="development">
    <util:properties id="properties" location="classpath:META-INF/dev.properties" />
</beans>

With the previous requirements, you could define the base tag in any JSP template page with:

<spring:eval expression="@properties['baseurl']" var="baseurl" />

<base href="${baseurl}/" />

Finally you could declare all links and resources as relative paths:

<link rel="stylesheet" href="css/app.css">
<script src="js/libs/jquery-1.7.1.min.js"></script>
<a href="">Home</a>
<a href="about">About</a>
...

If for any reason you are having trouble with the base tag, you could also declare every link with ${baseurl}: <link rel="stylesheet" href="${baseurl}/css/app.css">

Using this solution, every server will have each own Active Profile, with all the links and resources updated in runtime, as expected.

edrabc
  • 1,259
  • 1
  • 14
  • 23