Please tell me what does "harsh mistress" mean in this sentence:
Nostalgia can be a harsh mistress.
It deliberately has two meanings, using the ambiguity for poetic effect.
One meaning of mistress is as the feminine form of master, and hence it is saying nostalgia must be obeyed, (and is harsh).
Another meaning of mistress is as a woman one (traditionally based on heterosexist assumptions, a man) has a romantic and/or sexual affair with. Hence it is saying that nostalgia is "courted" as something beloved, but responds harshly (in many ways a more fraught position than having a harsh mistress in the other sense).
The second is probably the predominant meaning, but the first is deliberately still in there, which affects its poetic impact.
Just where the balance lies depends on the subject:
In the case of nostalgia, it's hard to see the first meaning as applying much as there's always a voluntary aspect to engaging in nostalgia.
In the case of the sea (a common subject of the idiom), the balance is greater - those who work on the sea typically chose that career out of a love for it, but must also continue to work it to make their living even if they are no longer as fond.
In the case of The Moon in the famous Heinlein novel, both meanings are definitely present, but given that the story is about a revolt against an undemocratic leadership, the author more deliberately wants the dichotomy of mistress/slave brought to mind.
According to Google Ngram, the 1830s is where we see the turn of phrase "harsh mistress" take root to describe the world, Russia, and actual females. Its usage there invokes the female equivalent of a "harsh master".
The phrase is used to describe someone (or something) with exacting standards that are hard, if not impossible, to satisfy.
Heavenly bodies like the earth and moon, as well as countries, are generally considered feminine in English (and explicitly so in languages like Spanish: la luna, la tierra), but this doesn't necessarily confer a woman's status (or historical lack thereof) to the object itself. For instance, "France deployed her troops" does not imply France's characteristics are "feminine" (as defined by patriarchal society?).
Without context, it's hard to discern the meaning(s) in the sentence. I lean toward "woman master" rather than "kept woman." Insofar as women are stereotyped as "love objects", the sentence could mean that nostalgia exerts some kind of lure that is ultimately unrewarding. However, it's not necessary to read in a woman's status, as the sentence means that nostalgia has some form of dominance despite nostalgia's lack of merit, regardless of "her" role.
Edit: removed non-existent Biblical reference, lazy citing on my part!
Mistress - something you fall dearly in love with.