Well we dream what is in our mind and then the dream last well on our mind too. As you rightly said, these nightmares have something to do with your past. And here comes my suggestion, I think you need to change the state of your mind, come out of depression and be happy. For that I can suggest do something adventurous, read inspiring novels or meet positive people and make yourself so occupied that you get hardly any time to think about past. And when you go to bed, you are so tired that you have a sound sleep. Yes it works, if you have kept yourself engaged and occupied all day, you sleep good and there is positivity everywhere. If you ever feel loneliness and need to talk just reach the inbox of person whom you are comfortable to talk with. Take care and stay positive, these dreams and depression is just a temporary feeling.