A group of migrants walk along the railway near Horgos, on the Serbian border with Hungary, on Aug. 25. (Darko Vojinovic/AP)

Thousands of refugees, most fleeing wars in Syria, Iraq and Afghanistan, have been snaking northward through the Balkans in recent days, confronting a Europe woefully unprepared to deal with them at every step.

Most endured a perilous crossing to Greece aboard rafts and boats, some barely fit to sail. They traversed Greece, a nation paralyzed by economic crisis and too poor to handle a flow of people that in July hit a record high. At the border with Macedonia late last week, they trudged through a wall of riot police, who fought them back with tear gas before relenting. Now, the asylum-seekers, thousands a day, are racing into Hungary, which is rushing to complete a barbed-wire border fence by the end of the month to force them to seek other routes.

It is a long parade of misery unparalleled in Europe in recent years. But the continent has so far failed to agree how to respond. Amid a refugee crisis that by some measures is the worst since World War II, individual nations are being left to improvise their own measures. In Hungary, that is taking the form of 108 miles of barbed wire and fencing.

The crisis is shaking fundamental tenets of European life, including the principle of free movement between most of the nations of the European Union. It is fueling a surge of anti-migrant sentiment in the countries that are housing the bulk of the asylum-seekers, Germany and Sweden. And it is straining the weakest countries, such as Greece, that are on migration’s front lines.

“Unless we do something, we will become a lifeboat sinking under the weight of people holding on to it and drowning everybody, both those seeking help and those offering help,” said Janos Lazar, Hungarian Prime Minister Viktor Orban’s chief of staff, at a ceremony last week celebrating the founding of Hungary.

The surging numbers of asylum-seekers are being propelled by violence in the Middle East, instability in Africa and poverty in South Asia. Many who fled the war in Syria and stayed for years in neighboring nations are giving up on returning home. Fresh waves are leaving because of the violence of the Islamic State.

Germany alone expects 800,000 asylum-seekers this year, a fourfold increase over 2014. The overall E.U. numbers have tripled compared with last year. According to E.U. law, asylum-seekers are supposed to apply for help in their first port of entry. In reality, many front-line countries give them transit papers or look the other way as they slip onward to richer nations within Europe’s borderless Schengen zone.

Their arrivals are reverberating around Europe. Earlier in August, thousands of refugees piled up in Calais, France, seeking to cross to Britain through the railway tunnel that exits there. On Saturday, the Italian coast guard rescued about 4,400 migrants from overcrowded ships off the coast of Libya. Even the theoretical idea of refugees is controversial: Slovakia, which has taken in just a handful, said last week that it wanted only Christian asylum-seekers before reversing itself. Tiny Baltic nations have revolted at a proposal to make them each shelter a couple of hundred.

“All of those dynamics point to a Europe that is not finding ways to cooperate,” said Alexander Betts, director of the Refugee Studies Center at the University of Oxford. “And while Europe squabbles, people die.”

An obstacle, not barrier

In Hungary, workers have raced to complete the fence by the end of August. In some stretches of the 108-mile-long frontier with Serbia, it is composed of tall coils of razor wire. In others, it is a 10-foot-tall fence.

The construction of the barrier is rich with irony, since one of the first holes in the Iron Curtain was created by Hungary’s 1989 decision to cut open its border fence with Austria to allow East German asylum-seekers to flee there. In 2004, when Hungary joined the European Union, it joyfully cut through the remainder of its border fence with Austria.

On Monday, a record 2,093 asylum-seekers crossed into Hungary, about double the flow of most days in recent weeks, according to police figures. Tuesday’s number looked likely to rival Monday’s, said Babar Baloch, a Budapest-based spokesman for the Office of the U.N. High Commissioner for Refugees (UNHCR).

“What these fences do is push these people into the hands of more-ruthless smugglers and put them in even more danger,” Baloch said. But it does not stop them. “They have already taken so many risks. So they will take more risks.”

Local leaders say that the fence is not enough. “It isn't worth anything,” said Istvan Fackelmann, the deputy mayor of Asotthalom, a small Hungarian border town. “They pass by or climb over.”

Hungary is just a waypoint for most of the migrants. But each step of their journey has been getting more difficult. Greece, the first E.U. entry point for most of them, nearly was kicked out of the euro zone in July, further challenging its ability to handle the record flows of asylum-seekers. Macedonia is their next step, but on Thursday it suddenly decided to close a major border crossing, sending out riot-gear-clad soldiers who formed a wall with their shields that held back a jostling crowd of thousands. The riot squad used tear gas to push people back toward Greece.

“People were coming from behind, they were pushing and shoving, and they were passing children to the front because they thought the children could go through,” said Alexandra Krause, a senior protection officer with the UNHCR who watched the scene at the Gevgelija crossing on Thursday and Friday.

When the Macedonian government relented and reopened the border late Friday, she said, a backlog of about 7,500 asylum-seekers continued the journey northward through the Balkans by rail and bus. On Tuesday the migrant flows were much calmer, she said by telephone from the border crossing.

German Chancellor Angela Merkel said this week she would demand that other European nations take in asylum-seekers. The dilemma is dominating her schedule. On Wednesday, she plans to visit Heidenau, a town in Germany’s ex-communist east where hundreds of anti-immigrant protesters rioted Saturday as refugees arrived by bus. On Thursday, she will attend a summit on the Western Balkans that will focus on the flows of migrants.

“Germany is a country that respects the dignity of every human being,” Merkel said Monday, speaking alongside French President François Hollande after meeting with him about the crisis. Other European countries are “not currently implementing” their laws on asylum, she said.

Gergo Saling in Budapest contributed to this report.

Read more:

A smugglers’ haven in the Sahara

On Greek island, migrants find that paradise quickly turns into purgatory

‘We want papers! We want to eat!’ Fights break out among migrants as tensions rise on Kos