upstream app_server {
  server 0.0.0.0:3000;
}

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

  root /path/to/main_site;
  # ...

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

    if ($http_user_agent ~* '(iPhone|iPod)') {
      set $iphone_request '1';
    }
    if ($http_cookie ~ 'iphone_mode=full') {
      set $iphone_request '';
    }
    if ($iphone_request = '1') {
      rewrite ^.+ http://m.mysite.com$uri;
    }

    # serve cached pages ...

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