MADRID — When the migrant surge began in 2015, Western Europe was welcoming.

Cheering crowds greeted refugees arriving by train in European cities. “We can do it!” German Chancellor Angela Merkel told her countrymen, urging on their generosity. Swedes scrambled to help new arrivals obtain housing and medical care. Italians began a massive search-and-rescue patrol effort in the Mediterranean.

But since then, much of Western Europe has closed its borders, turned away rescue ships and thrown support behind anti-migrant politicians. Even as the numbers of new arrivals have decreased to pre-2015 levels, migration has propelled the election of populist factions in Austria and Italy and threatened the stability of establishment parties in France and Germany.

The most visible exception is Spain.

Just over seven nautical miles from Africa at the nearest point, Spain is Europe’s new front line for migrants crossing the Mediterranean. Since the beginning of the year, nearly 49,000 have landed here, according to statistics from the U.N. refugee agency. The figure represents about twice the number of 2018 arrivals to Greece or Italy — previously the primary gateways to Europe.

But for Spain’s center-left government, this is hardly cause for concern.

“We shouldn’t be frightened by the arrival of 50,000 or so in one year,” said José Alarcón Hernández, a top migration official in the Labor Ministry. Spain’s population is 40 million.

Prime Minister Pedro Sánchez and his Socialist Workers’ Party have sought to distinguish their country for its compassion. “It is our duty to help avoid a humanitarian catastrophe and offer a safe port to these people, to comply with our human rights obligations,” Sánchez said when welcoming 630 migrants aboard the Aquarius rescue ship, which Italy had rejected, in June.

A Pew Research Center survey published last month found Spain to be the European country most supportive of refugees, with 86 percent of Spanish adults in favor of taking in people fleeing violence and war.

Despite Spain’s recent spike in migrant arrivals and a staggering level of unemployment — 15.2 percent, the second-highest rate in Europe — anti-migrant rhetoric is rare, and the far right is relatively weak.

The far-right party Vox attracted 9,000 people to a rally in Madrid this month, and polls suggest it could win a seat in the lower house of parliament in the 2020 election. But the party is still very much on the fringe.

Pablo Casado, the leader of the opposition People’s Party, has voiced anti-migrant positions, although not with the same vitriol as in Italy and elsewhere. “There can’t be papers for everyone, nor is it sustainable for a welfare state to absorb the millions of Africans who want to come to Europe,” he said in July. “And we have to say it, even if it is politically incorrect.”

Casado’s remarks were roundly criticized. Barcelona Mayor Ada Colau warned that such statements were a step toward “the destruction of Europe and democracy itself.” Days later, Casado was photographed shaking hands with migrants.

So why is the sentiment in Spain so different? And will it last?

According to a September poll from the Sociological Research Center in Madrid, unemployment is the most acute concern for Spaniards. Immigration ranks fifth — behind corruption, a sluggish economy and a perceived failure of the political class.

But unlike other Europeans, Spaniards have not generally blamed unemployment on immigration.

“Unemployment is seen as having many other roots,” said Carmen González Enríquez of the Elcano Royal Institute, a Madrid think tank. “Only some people, especially among those less educated, are in fact competing with migrants for jobs.”

Nationalist tendencies in Spain, she added, are constrained by memories of Francisco Franco’s dictatorship. Elsewhere in Europe, where mainstream right-wing parties have been willing to flirt with or embrace nationalist rhetoric, the memories of a right-wing past in the 1930s or 1940s are more distant. But Franco died in 1975, well within the lifetimes of many voters and policymakers today.

“During those years of dictatorship, the use of national symbols somehow inoculated the Spaniards against the use of ­nationalist rhetoric in any political form,” González Enríquez said. “It’s now unimaginable that a political party could use the expressions needed to create [a] xenophobic, anti-migration party.”

This is not to say that Spain is an unqualified haven for migrants. Their advocates note a contrast between Spain’s welcoming rhetoric toward those rescued in the Mediterranean and its attitude toward migrants seeking refuge in Spanish enclaves in northern Morocco.

According to the Spanish Interior Ministry, about 3,300 migrants have entered Spain through Ceuta and Melilla this year. Interior Minister Fernando Grande-Marlaska said on Spanish radio that the aim should be to deter migrants before they reach the dangerous fence.

"Our vision is: The more we help Morocco control its borders, the better it is for everyone," said Alarcón, the migration official.

Some advocates contend Spain has pressured Moroccan officials to arrest and expel migrants from sub-Saharan Africa before they try to get to Europe.

Morocco’s willingness to co­operate may be what allows Spain to welcome migrants to the degree that it has, and a disruption of that cooperation could result in a shift in Spanish public opinion and policy.

Moroccan officials have rejected a proposal — first floated by French President Emmanuel Macron — to build “hot spots” on their soil to prescreen refugees hoping to get to Europe. “Are we real partners, or just a neighbor you’re afraid of?” Moroccan Foreign Minister Nasser Bourita said to German newspaper Die Welt this month.

“What kind of social assistance is there for a mother who has her papers but has never worked?” Fatima Coulibrely, a 34-year-old from Ivory Coast, asked at the Karibu Association aid organization in Madrid. “Can my husband apply for Spanish citizenship if he doesn’t have a permanent contract?”

Sandra do Hoz, 37, said life remains difficult nine years after she arrived from Cape Verde and started making money by cleaning houses.

“It’s hard to find work when you have a baby,” she said as she pushed her 9-month-old in a carriage. “And my husband’s unemployment already ran out. For now, we make do.”