An nginx configuration file for Dropwizard with static content

February 13, 2013 | Comments
Tags: Tips


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:

Tweet

Related Posts

Useful? Consider a small donation: 1KzTSfqjF2iKCduwz59nv2uqh1W2JsTxZH

Got an opinion? Comments are welcome!

Want more? Take a look at the following:

Simple Scaffolding
How to deploy dynamic sites with git
How to deploy static sites with git
A .gitignore file for Intellij and Eclipse with Maven
How to recover your bitcoins from a failed hard drive
How to build a Builder
How to recover lost bitcoins from an Android wallet
7 refactorings I couldn't live without
9 top tips to understand new code
Avoiding duplicate test results with Hudson

comments powered by Disqus