Knowing his days were numbered, trailblazing rock icon David Bowie made one final public appearance last month at the New York City premiere of his off-Broadway musical, flashing smiles as he mingled with fans.

But behind the scenes, he was suffering.

“Newspapers wrote that he looked so well, so healthy. But when we left, he collapsed straightaway,” said Ivo van Hove, director of “Lazarus,’’ referring to its Dec. 7 opening at the New York Theatre Workshop on East Fourth Street.

“It was then that I realized that it may be the last time I would see him,” van Hove told Danish media.

A little over a month after the premiere, Bowie lost his secret, year-and-a-half battle with liver cancer — stunning his many admirers, including even some close to him.

His death came after a difficult few years in which he suffered half a dozen heart attacks related to his cancer, his biographer, Wendy Leigh, told BBC News.

But somehow, Bowie continued to create, releasing his final work, a bleak, haunting album, “Blackstar,” on his 69th birthday Friday, two days before his death.

While the Thin White Duke kept his deteriorating health quiet, his devoted supermodel wife knew the end was near — posting a series of heartfelt social media messages just before he took his last breaths Sunday.

“Sometimes you will never know the true value of a moment until it becomes a memory,” Iman Abdulmajid, 60, tweeted Saturday.

On Sunday, she posted, “The struggle is real, but so is God.”

Brian Eno, who produced several of the rocker’s greatest albums, said he only now realizes that his friend was saying farewell to him in an email he received just last week.

“It was as funny as always, and as surreal, looping through word games and allusions and all the usual stuff we did,” Eno said in a statement.

“It ended with this sentence: ‘Thank you for our good times, Brian. They will never rot.’ And it was signed ‘Dawn.’ I realize now he was saying goodbye.”

“David’s death came as a complete surprise, as did nearly everything else about him. I feel a huge gap now,” said Eno, a keyboardist for Roxy Music.

Although Bowie was a Brit, he became a true New Yorker.

He spent his last months in Nolita, where he had lived in a 5,300-square-foot penthouse apartment with Iman since 1999.

One of Bowie’s Lafayette Street neighbors, Mike Connor, 59, described the star as a “pure gentleman” and a “very polite man.”

He recalled one time when Bowie got stuck in the elevator, and Connor’s son, Colm, started singing lyrics from “Space Oddity” — “Here am I, sitting in a tin can, miles above the world.”

When they got out of the elevator, Bowie told them, “I was getting nervous, but as soon as I heard you singing the lyrics, I started laughing.”

The singer would grab sandwiches at Olive’s eatery, do his grocery shopping at a nearby Dean & Deluca, peruse the books at The Strand and stroll through Washington Square Park.

“He was a good customer, always left a tip,’’ said Angel Rodriguez, 25, a manager at Olive’s on Prince Street, into which Bowie would pop twice a week for a chicken sandwich with watercress and tomatoes.

Bowie also had a sweet tooth — and couldn’t resist a warm chocolate-chip cookie.

“He always said it was a tease because he could smell them at the counter and would have to get them even though he hadn’t planned on getting them,” Rodriguez said.

Bowie used to shop at Dean & Deluca once a week, but store workers noticed he’d recently stopped coming in as often.

“Everybody always appreciated how nice he was, how he just seems like a regular guy,” manager Marla Tremsky said.

Fans paid tribute to Bowie at a makeshift memorial outside his building Monday, leaving flowers, candles and photos.

Additional reporting by David K. Li and Bob Fredericks