Hi,
How are you? Pain or headache that develop during sleep that may last for several hours may be due to cluster headaches This is also associated with pain in the vicinity of the eye, tearing of eye, nose congestion, and even flushing of the face. Attacks may occur every day for weeks then disappear for up to a year. Have this checked by your doctor for proper evaluation. Your medical history and direct clinical and neurological examination may help. Take care and do keep us posted.
Another possibility that occurred to me while reading your post is this: have you been checked for sleep apnea? For a while, my docs were considering that my migraines were being exacerbated by sleep apnea, where my brain wasn't getting enough oxygen at night. I would think if you did have sleep apnea, it could account for the headaches, just like holding your breath during the day can bring on a headache.
As for them not happening every night, do you always sleep in one position, or are you more of a restless sleeper? If the latter, it might be that it's one sleeping position that aggravates your nocturnal headaches. Maybe you could keep a notebook of what position you're in when you go to sleep, and what position you're in when you wake up and see if there's a pattern there.
Good luck and keep us posted,
~Dame