A few weeks ago, I visited the fence that made Hungary a symbol of European anxiety. Chain-link and razor wire, it slices through pastures as it traces Hungary’s southern border. More than 100 miles long, it was constructed quickly this summer as refugees streamed into the country, heading toward Austria and Germany. Police officers and soldiers were stationed every few hundred yards, and they examined my passport at almost every checkpoint. They seemed bored, perhaps because the flood of refugees and migrants had mostly abated. The fence had sealed Hungary off, and that made Laszlo Toroczkai — the 37-year-old mayor of Asotthalom, a Hungarian farming town on the Serbian border, and a rising ultranationalist star of far-right European politics — very happy.

Before Hungary’s government started building the fence, Toroczkai argued for months that something had to be done to stop the refugees. Once the fence was up, he posted his version of a ‘‘Dirty Harry’’ video on YouTube. Stone-­faced and wearing a black jacket, Toroczkai warns illegal immigrants to steer clear of his town. The video cuts to images of unsmiling Hungarian guards, patrolling the border on horseback and motorcycles. It even includes a map that shows migrants en route to Germany how they can skirt Hungary and go through Croatia and Slovenia instead.

‘‘Hungary is a bad choice,’’ Toroczkai says, staring into the camera. ‘‘Asotthalom is the worst.’’

It might seem odd that a chain-link fence could threaten the order of the European Union, but one of modern Europe’s singular accomplishments is its open internal borders. The treaty that made this possible, known as the Schengen Agreement, began going into effect in 1995 and expanded to include 26 countries in and around the European Union. It immediately provided a potent symbol of both the ideals and the real benefits of European integration. Elites believed unity would guarantee peace and prosperity and dispel the demons of nationalism. Over time, the European Union built a rich and diverse economy, exerting global influence through the clout of its scale and the soft power of its liberal, democratic values — a Western superpower without the bellicosity or the laissez-faire hardheartedness of the United States. Workers’ rights were protected. Generous social-welfare programs flourished. The open borders bound the member nations together in subtler ways, too: Italians could soon go skiing in Austria with ease and without a passport, or head to the Côte d’Azur for a swim as if Europe were all one country. Workers could commute between Brussels and Paris without hassle.

Hungary’s southern fence was erected along its external borders with Serbia and Croatia, which are not Schengen countries. But it caused a chain reaction that shook European politics to the core, as the distinctions between internal and external borders blurred. First, it led to more fences. Neither Slovenia (which is a Schengen country) nor Croatia was fully prepared for the surge of frantic migrants and refugees suddenly diverted toward them. Slovenia quickly constructed its own fence on the Croatian border. Austria later began building a fence on its boundary with Slovenia and established other border controls, as did other countries. Prime Minister Viktor Orban of Hungary said he was defending the Schengen area, while other leaders said these new barriers were intended to provide an orderly route north for refugees, not to block them. Nonetheless, the rapid proliferation of fences across a region defined by free movement was an unmistakable sign that the external Schengen borders had broken down and the founding values of the system were under terrible strain.