Those nightmares might be out of the mischief of Satan, or out of sickness. It is well known to doctors that a patient can experience such nightmares in their sleep. In all cases,
we advise you to say Dhikr, especially those prescribed for sleeping, as they guard Muslims against Satan and bring benefit, with Allah's Permission, to those who utter them. The following are mere examples: reciting Ayat-ul-Kursy when going to bed, and reciting,
Say (O Muhammad صلى الله عليه وسلم): "He is Allâh, (the) One.
Say: "I seek refuge with (Allâh), the Lord of the daybreak,
Say: "I seek refuge with (Allâh) the Lord of mankind,
three times on sleeping, following the example of the Prophet (peace be upon him). (Part No. 24; Page No. 205) May Allah grant us success. May peace and blessings be upon our Prophet Muhammad, his family, and Companions.