I often find myself having to fiddle about with nginx configuration settings to get it to work with a Dropwizard application and static content. Of course, it would also work with any Java application container (such as Jetty, JBoss, Glassfish or Tomcat):

Here is what works for me:

# Site (port 80 -> 9090)
server {
  listen          80;       # Listen on port 80 for IPv4 requests

  server_name localhost;

  access_log      /var/log/nginx/site_access.log;
  error_log       /var/log/nginx/site_error.log;

  # Set the root of the static content
  root /usr/share/nginx/html;

  # Redirect server error pages to the static page /50x.html
  error_page   500 502 503 504  /50x.html;
  location = /50x.html {
    root /usr/share/nginx/html;
  }

  # Filter static content types and serve from the root
  location ~*\.(jpg|jpeg|gif|css|png|js|ico|html)$ {
    access_log off;
    expires max;
  }

  # Serve the dynamic content (Site)
  location / {
    # The application provides its own detailed logs
    access_log off;

    # Hand over to the application
    proxy_pass        http://localhost:9090/;

    proxy_set_header  Host             $http_host;
    proxy_set_header  X-Real-IP        $remote_addr;
    proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for;
  }

}

If you place this in /etc/nginx/conf.d/site.conf and link that to the main nginx config then you’ll get the following behaviour:

  • / will serve the root of your application
  • Any static files are filtered on common extensions (.jpg,.png etc)
  • Any other content is directed to the app serving on port 9090
  • Any 50x error generated by the app will cause a redirect to the /50x.html static file