I know this is not a completely accurate answer, but it is some sort of chemical that is released to the brain after you have eaten cheese that causes it. But personally I have never had nightmares after eating cheese before bed time!!
My husband often makes cheese on toast for us late at night & it doesn't seem to give us nightmares. Having said that, I do have nightmares, but not necessarily when I've eaten cheese. Squeak, sqeak!
Traditionally it was hard to digest, so indigestion would disturb your sleep. In actual fact, no food (other than drugs) will actually cause nightmares.
I read an article in a UK paper which looked into this myth - the best that their "scientists" could suggest is that the eating of any food with a high fat content before sleeping can lead to disturbed sleep patterns and more dreaming than normal. The consclusion was that there is nothing specific in cheese which would cause nightmares and any effect was either due to the fat content of the power os suggestion.