1770, "severe homesickness considered as a disease," Modern Latin, coined 1668 in a dissertation on the topic at the University of Basel by scholar Johannes Hofer (1669-1752) as a rendering of German heimweh "homesickness" (for which see home + woe). From Greek algos "pain, grief, distress" (see -algia) + nostos "homecoming," from neomai "to reach some place, escape, return, get home," from PIE *nes- "to return safely home" (cognate with Old Norse nest "food for a journey," Sanskrit nasate "approaches, joins," German genesen "to recover," Gothic ganisan "to heal," Old English genesen "to recover"). French nostalgie is in French army medical manuals by 1754.

Originally in reference to the Swiss and said to be peculiar to them and often fatal, whether by its own action or in combination with wounds or disease. By 1830s the word was used of any intense homesickness: that of sailors, convicts, African slaves. "The bagpipes produced the same effects sometimes in the Scotch regiments while serving abroad" [Penny Magazine," Nov. 14, 1840]. It is listed among the "endemic diseases" in the "Cyclopaedia of Practical Medicine" [London, 1833, edited by three M.D.s], which defines it as "The concourse of depressing symptoms which sometimes arise in persons who are absent from their native country, when they are seized with a longing desire of returning to their home and friends and the scenes their youth...." It was a military medical diagnosis principally, and was considered a serious medical problem by the North in the American Civil War:

In the first two years of the war, there were reported 2588 cases of nostalgia, and 13 deaths from this cause. These numbers scarcely express the real extent to which nostalgia influenced the sickness and mortality of the army. To the depressing influence of home-sickness must be attributed the fatal result in many cases which might otherwise have terminated favorably. ["Sanitary Memoirs of the War," U.S. Sanitary Commission, N.Y.: 1867]

Transferred sense (the main modern one) of "wistful yearning for the past" first recorded 1920, perhaps from such use of nostalgie in French literature. The longing for a distant place also necessarily involves a separation in time.

I knew there had been European epidemics of nostalgia in the seventeenth, eighteenth, and nineteenth centuries, but didn’t know of American ones. But indeed, the disease of nostalgia was widely known in the United States–during the Civil War, there were 74 deaths from it on the Union side, and more than 5,200 cases of it in the Surgeon General’s records.

It became such a problem that army bands were sometimes prohibited from playing “Home, Sweet Home,” which at that time, was the most popular song in the country....

...Medical sources of the eighteenth and nineteenth centuries abound with fatal bouts of emotion. Is it possible to pinpoint a date after which this changed – when homesickness and nostalgia became mere feelings rather than powerful and potentially fatal mental conditions?...

...One soldier in the American Expeditionary Force reportedly died of nostalgia during World War I. Increasingly during the War, however, many of the symptoms associated with nostalgia came to be defined as signs of the newly established syndrome of shell shock. While the diagnosis of nostalgia stayed on the books up through World War II, and while there were many reported cases of it among soldiers in that war, there were no deaths. In short, from the early twentieth century on, the number of cases of deadly nostalgia declined, although less lethal cases of homesickness continued (and continue) to abound....

It was actually coined in 1688. Johannes Hofer was born in 1669 and graduated from the University of Basel in 1689. In the year before he graduated, he supported a secondary thesis in front of Professor Johan Jacob Harder, in which he described a condition he called "nostalgia".