nxweb + Apache Tomcat Configuration

19.09.2014

Prerequisites

  • Apache Tomcat 7 is configured with HTTP port 8080 (this is the default config of Tomcat)
  • your Java application myapp.war is deployed under /myapp prefix

Directory layout

current working directory/
  |- nxweb_config.json
  |- static/
  |- cache/
  |- logs/

static directory shall contain static files like *.js, *.css, images, etc.

cache directory will be used by nxweb to store cached files (you might need to periodically clean this directory by removing files of certain age).

logs directory will contain nxweb log files.

Make sure file permissions allow nxweb to write into cache and logs directories.

nxweb_config.json

{
  "backends":{
    "tomcat":{"connect":"localhost:8080"}
  },

  "routing":[

    { // static files
      "prefix":null, // "vhost":".mydomain.com",
      "handler":"sendfile", "memcache":false,
      "dir":"static",
      "charset":"utf-8", // charset for text files
      "index_file":"index.htm", // directory index
      "filters":[
        {"type":"gzip", "compression":4, "cache_dir":"cache/gzip"}
      ]
    },

    { // java app
      "prefix":null, // "vhost":".mydomain.com",
      "handler":"http_proxy", "backend":"tomcat",
      "uri":"/myapp", // prepend this uri prefix to path info
      "proxy_copy_host":true, // copy host header from original request
      "filters":[
        {"type":"file_cache", "cache_dir":"cache/tomcat"},
        {"type":"templates"}
      ]
    }
  ]
}

server.xml

In order to make real user IP available to your servlets via request.getRemoteAddr() add the following <Valve> to your Tomcat's server.xml file:

<Engine name="Catalina" defaultHost="localhost">

  <Valve className="org.apache.catalina.valves.RemoteIpValve"
    remoteIpHeader="X-NXWEB-Forwarded-IP"
    protocolHeader="X-NXWEB-Forwarded-SSL"
    protocolHeaderHttpsValue="ON"
    httpServerPort="80" httpsServerPort="443" />

   ...
</Engine>

Launch nxweb

Make sure your current working directory is where nxweb_config.json file is located.

nxweb -H :80 -S :443 -l logs/nxweb_error_log -a logs/nxweb_access_log

This must be run by a privileged user (root) in order to use port numbers below 1024. If you specify -u <user> -g <group> options then nxweb will drop privileges after binding the ports.

Now you should be able to access your application servlets under site's root directory. Static files will be checked first, then request will be routed to backend.

nxweb will maintain a pool of keep-alive connections to Tomcat for maximum performance.

There are two filters applied to Tomcat's output in this sample config:

  • file_cache will cache Tomcat's responses (only when they contain Last-Modified header)
  • templates will pass backend's response to templating engine so you can keep your page design in a separate static files (more about it in future articles)

 

Comments