There are many factors why nightmare occurring during our sleep. The reasons are you have negative thoughts in your mind, you encountered bad experience before you sleep, you are worrying too much about something, and you have problems that you want to get rid of.
Now to avoid nightmares, You should free yourself on your anxiety, worries, and fears. It is better to change how you think and respond in every situation you are encountering. Also, it is a big factor in how you see things and problems. You should look at things very positively, having this kind of thinking will help you to live more peacefully and happier. Nonetheless, It is always the best to pray and ask help to our God before we sleep. Give your every burden to Him and Trust Him that He will help you with your life. Do These things you will be nightmare free.