Stuart Harness, 34, and Gavin Humphries, 37, were caught on CCTV making petrol bombs in the back yard of Harness's house in Grimsby.
They were driven to nearby Grimsby Islamic Cultural Centre and threw the devices at the main doors as if they were lobbing grenades, as they would have been trained to do, the prosecution said.
The men thought a CCTV system near the mosque was turned off, but the cameras recorded the petrol bombs flying through the air and exploding in orange flames.
Terrified worshippers were trapped inside but managed to extinguish the flames coming under the door.
The attack happened four days after the murder of Fusilier Rigby outside Woolwich Barracks in south-east London in May.
Hull crown court heard how Harness had been based at Woolwich, living there with his family and his children attending local schools.
Jeremy Evans told the court Harness had been in the army for 13 years and experienced "active warfare" during deployments in Iraq and Afghanistan.
Humphries also served in the army for a number of years and both men had unblemished military records with no previous convictions.
The mosque had been targeted earlier in the week and two police community support officers were patrolling nearby when the ex-soldiers launched the arson attack, Mr Evans said.
One officer followed the man 300 metres back to Harness's house and the pair were arrested.
Harness and Humphries admitted arson, being reckless as to whether life was endangered. They were both jailed for six years each.
'Act of retribution'
Judge Mark Bury told the pair: "This was a crime of violence where a particular religious group was deliberately targeted in an act of retribution.
"This kind of attack cannot be tolerated. A severe sentence is required to punish but, more importantly, to deter."
He added: "Whatever your feelings of outrage were, you should have allowed justice to take its course. Instead you carried out a retaliatory act of throwing petrol bombs at the Grimsby Islamic Cultural Centre.
"As is usual in these cases, the victims had nothing to do with the events that so enraged you. They were entirely innocent law-abiding Muslims who were practising their religion in a peaceable way."
He also jailed a third defendant, Daniel Cressey, who drove Harness and Humphries from Harness's home to the mosque but did not join in the attack, for six years.
Cressey had denied arson, being reckless as to whether life was endangered, but was found guilty by a jury.
Inspector Mel Christie from Humberside Police said: "This was a difficult time for the Muslim community, both on a local and national level, so I would personally like to thank the majority of people who provided support to them during this time.
"The incident on 26 May will not hinder the good work that Humberside Police and representatives from the Mosque do to help bring communities together and promote cohesion across North East Lincolnshire."