It is the first time a so-called royal decree has been rejected in parliament since the late 1970s.

Rajoy was re-instated for a second term last October but with the smallest parliamentary minority in Spain's modern history. As a result he is forced to seek support from rivals every time he wants to pass legislation.

His cabinet passed the draft ports law in February, after Spain raked up at least 21 million euros (18.36 million pounds) in European Union fines since 2014 for failing to crack down on closed-shop hiring among dockers.

"The loss is not just for us, but all Spaniards who will have to pay the fine," Public Works Minister Inigo de la Serna said in parliament following the vote.

With 350 votes cast in total, the bill was rejected after 175 voted against the decree, 142 were in favour while there was 33 abstentions.

The decree caused an uproar among unions who called several days of strikes and urged the government to sit down and negotiate the reform rather than rush it through.

They said they would be willing to meet with the government and business representatives to discuss changes to the proposal following the vote.

Several opposition parties, including the centre-left Socialists and anti-austerity Podemos ("We Can"), voted against the decree on Thursday.

The Centrists Ciudadanos ("Citizens") - the fourth largest force in parliament and a party that is working in tandem with Rajoy's conservative People's Party (PP) on some policies - abstained in the vote.

The European Commission, which had suspended fines when it appeared the Spanish government was nearing a reform, has suggested these might restart if the bill did not progress through parliament.

Dockers unions said that they had cancelled all planned strikes following the vote.

Around two-thirds of Spain's imports and exports - a major part of Spanish economic base - pass through its ports and prolonged industrial action would be especially painful for the automobile industry and chemical sector.

($1 = 0.9328 euros)

(Writing by Sarah White; Editing by Paul Day and Pritha Sarkar)

By Inmaculada Sanz