Meaning of "harsh mistress"
Solution 1:
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.
Solution 2:
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!
Solution 3:
Early literal use of 'a harsh mistress'
The earliest instances of "a harsh mistress" in English refer to the female counterpart of a master— that is, either an employer (of a servant or employee) or an owner (of a slave). From a 1713 (fourth edition) translation of Terence's The Eunuch:
Thais. Were you afraid that I shou'd prove a harsh Mistress, that you ran for't?
Cherea. No indeed, Madam.
From Sarah Scott, The History of Sir George Ellison (1766):
... for a woman may be a very disagreeable wife, a tiresome friend, a harsh mistress, and very deficient in the duties of a mother, and yet, according to this narrow way of thinking, be honest, chaste, prudent, and in the common acceptation of the phrase, good natured.
From Emma Roberts, "The White Wolf," in The Mirror of Literature, Amusement and Instruction (December 2–9, 1826):
Despite of the charms of the fair and jewelled multitude, his thoughts involuntarily recurred to the pale girl, pining beneath the iron sway of a harsh mistress ; but, at length, the image of the humble Mela was effaced by a lovely vision which which swam towards him in the graceful evolutions of the dance.
From Fifty Celebrated Women: Their Virtues and Their Failings, and the Lessons of Their Lives (1830):
And yet the Queen [Charlotte, wife of George III] was not wilfully unkind : she was an affectionate mother, and a devoted wife; and would have been shocked to consider herself a harsh mistress; but she did not conceive how any one could possibly care for pain, or discomfort, or illness, endured in the royal service.
From George Sargent, The English Peasant Girl (1830s[?]):
The English peasant girl, in those days, was insulted by the rich and powerful without means of redress: she might be torn from her parents and home, and sent away to other countries, to be offered for sale to the highest bidder, or taken to the house of her oppressors, and compelled to serve a harsh mistress, who might do almost anything she pleased with her, so that, even if the poor serf died under cruel treatment, there was scarcely any notice taken of it, or any punishment inflicted.
And from Charlotte Brontë, Villette, volume 1 (1853):
Even when she scolded me—which she did, now and then, very tartly—it was in such a way as did not humiliate, and left no sting; it was rather like an irascible , mother rating her daughter, than a harsh mistress lecturing a dependent : lecture indeed she could not, though she could occasionally storm.
Early figurative use of 'a harsh mistress'
Early figurative use of the phrase follows in the same vein. From a letter from Michael Faraday to the biographer, dated December 23, 1829, in John Paris, The Life of Sir Humphrey Davy (1831):
At the same time that he [Davy] thus gratified my desires as to scientific employment, he still advised me not to give up the prospects I had before me, telling me that Science was a harsh mistress ; and, in a pecuniary point of view, but poorly rewarding those who devoted themselves to her service.
From Robert Willmott, Conversations at Cambridge (1836):
The world is a harsh mistress, but consider how soon death fetches us home from school ! Every new affliction is, to the sincere Christian, only another friendly blow upon the fetters which bind him to his earthly servitude.
From "Duty," in The Young Woman's Journal (June 1913):
Duty may seem a harsh mistress when people are not well acquainted with her. Later, however, there comes to those who consistently follow the path marked out by her, a higher sense of the value of things and what should be done becomes not a hard task, a distasteful duty, but a blessed privilege.
From Cyrill Hall, Triumphs of Invention (1920) [combined snippets]:
In the Middle Ages the Church was a harsh mistress to those who did not obey her slightest behest, but her very exactions led to results most beneficial to the world at large. Throughout this period Humphrey Davy's warning to Faraday that Science was a harsh mistress to those devoted to her service was by far the most widely cited figurative expression involving "a harsh mistress."
Burgeoning figurative usage of 'a harsh mistress'
More recently, writers have extended the figurative use of "a harsh mistress" considerably. From James Cooper, Sheila of Big Wreck Cove: A Story of Cape Cod (1922):
The schooner, with her affairs, was a harsh mistress.
From Paule Henry-Bordeaux, The Circe of the Deserts (1925):
Only, there arrived a thing which was not expected ; the plague made its appearance and reigned as a harsh mistress over the Syrian coast.
From Young Men, volume 52 (1926):
Truth is a harsh mistress.
From Otto Lütken, Congo Gods (1929):
Africa was a harsh mistress. She put her victims through a veritable purgatory, visited upon them physical and spiritual misery, fever, malaria, sleeping sickness, and a boredom often approaching insanity.
From Papers Relating to the Question of the Closer Union of Kenya, Uganda, and the Tanganyika Territory (1931):
Moreover, in Tanganyika, nature has proved herself a harsh mistress, and some of the " settlement " has been precarious and unprofitable.
From Robert Frothingham, Trails Through the Golden West (1932):
She [the desert] is a harsh mistress, however, to those who take too much for granted. She also has a way of keeping her secrets from those who are unwilling to give the necessary hostages.
From Betty Jacobsen, A Girl Before the Mast (1934):
The sailing-ship is a harsh mistress and the sea a tyrant; I see that.
From Geoge Slocombe, The Heart of France: Parisian, Provincial & Peasant (1934):
The land is an unwilling servant and a harsh mistress.
From The Spectator: Property Insurance Review, volume 2 (1936):
The law, as a harsh mistress, often gives the lie to the old maxim ["what is sauce for the goose is sauce for the gander"], and what "is" sometimes "ain't."
And that takes us through 1936.
Conclusion
The double meaning of mistress that Jon Hanna discusses in his answer to this question is undoubtedly a factor in the sense of the wording as it is used today. Nevertheless, during the first century of its use, the phrase "X is a harsh mistress" seems to have used mistress primarily in its "female counterpart to master" sense, not in its "illicit girlfriend" sense.
Solution 4:
The term X "is a harsh mistress" is an aphorism. "Mistress" is the traditional title for a head "Teacher" in a boarding school... As in "Head Mistress."
The meaning should be obvious to all but the least intelligent.
It means X "is a hard teacher."
For example; Reality is a harsh mistress, means "Reality is a hard teacher, lesson, etc. et al words synonymous with learning and/or experience.