I think I've conclusively proven that it's nutrient deficiency rather than dehydration that does it. Lately, before I go to sleep, I take out a bag of spinach I have in my fridge and drunkenly eat several large handfuls of the stuff before I go to sleep. It's been doing a pretty decent job at staving off head pain.