Spring mvc tomcat application deploy Spring mvc tomcat application deploy nginx nginx

Spring mvc tomcat application deploy


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.