upstream m_app_server {
  server 0.0.0.0:3001;
}

server {
  listen 80;
  server_name m.mysite.com;

  root /path/to/mobile_site;
  # ...

  location / {
    proxy_set_header X-Real-IP $remote_addr;
    # ...

    if ($http_user_agent ~* '(iPhone|iPod)') {
      set $iphone_request '1';
      set $iphone_path_prefix '/iphone';
    }
    if ($uri ~ ^/iphone.*$) {
      set $iphone_path_prefix '';
    }
    if ($uri ~ '(images|stylesheets|javascripts|\.css|\.js|\.ico|\.gif|\.jpg|\.png)') {
      set $iphone_path_prefix '';
    }
    if ($iphone_request = '1') {
      rewrite (.*) $iphone_path_prefix$1;
    }

    # serve cached pages ...

    if (!-f $request_filename) {
      proxy_pass http://m_app_server;
      break;
    }
  }
}
