A lot of trial and error! Caveat: I don't always follow my own advice
Do you have trouble falling asleep in the first place, or do you wake up and cannot get back to sleep???
Typical recommendations would be:
-no caffeine within 8 hrs of going to bed, avoiding screens/blue light within 1-2 h of going to bed. NO screens in the bedroom (too tempting to look at it in the middle of the night) or leave the cellphone-turned-alarm-clock across the room.
-if you are lucky enough to not do shiftwork, try to go to bed at near the same time every night, get up at approx the same time everyday, even on the weekend. If you are a shiftworker, keep that schedule you are on, on your days off, if possible eg: stay up late/sleep in a bit like your work rotation.
-some sort of a wind down routine after you turn screens off like dim lights, read a bit, a bath, some stretches or yoga, set up the coffee pot for the morning, make your work lunch. Something to signal your body that it's"time for bed". Just like we did with our babies/toddlers/young children and their baths and stories!
-find your own ideal bedroom ambiance .... temperature, covers, etc. Dark as you can with curtains, or....I use this every night and find it amazing, especially if I have to sleep a fair bit into the day (shiftworker in healthcare here)....it does not come off or shift and is a nice soothing gentle pressure on the face, I've never ripped it off in the night:
The Nite Hood™ offers a new kind of sleep mask. Its essential properties lay in our soft, locally knit bamboo fabric that uses opacity and stretch to make a cozy dark space for sleeping. The Nite Hood™ is worn to completely cover the head, covering the eyes and ears, softly applying a gentle...
www.nitehood.com
-calming the mind down...meditation, counting breaths, doing a brain dump on paper before bed.
-if you wake up in the night, don't look at the clock and notice what time it is, or it may stress you out. Just lay there and "rest", if you can't fall back asleep, get up and do something quiet like read in dim lighting (out of the bedroom...bed for sleeping and sex/intimacy only, no association of the bed with being wide awake in it)....when tired, go back to bed. This article is interesting....how way back when waking in the night was "normal" and people did not always freak out about it. The way society is set up now, it makes sleeping like this a bit more challenging:
Around a third of the population have trouble sleeping, including difficulties maintaining sleep throughout the night.
www.sciencealert.com
-have never tried it, but there are some CBT (Cognitive Behavior Therapy) books out there people can work through to help, or even therapists.
like I said, I always don't follow my own advice and kick myself the next day when I don't. Like my own personal "I told you so!"
It would be up to you and a doctor, but one thing I would say is whatever you do, try to use prescription sleep aids (those really strong ones like lorazepam, Zoplicone, etc) as a LAST LAST LAST resort. I have doled out way too many of them to people who have been on them for YEARS, and have extreme difficulty getting off of them (if ever), it is said that some of them give you an "amnesia" of sorts, versus a true cycling of REM/ non-REM sleep patterns, leading to long term effects.
good luck! I know all to well sometimes how it sucks to not sleep.