Rebel forces were held up Wednesday in their advance on Tripoli, the Libyan capital, facing fierce resistance from government troops in a key western city as thousands of refugees from areas still under Moammar Gaddafi’s control sought to escape anticipated violence.

Fighters carrying light weapons streamed into Zawiyah as families in packed sedans headed in the opposite direction, toward the Nafusa mountain range, which has been under rebel control since March.

Zawiyah is seen as critical to the rebel advance to the capital, 27 miles to the east. It is home to one of two oil refineries still under Gaddafi’s control, and the coastal highway connecting Tripoli with the Tunisian border passes through it.

“We will oust this dictator and turn Libya into a perfect nation,” said Abdel Bassed Ammar, a rebel from Zawiyah, as missiles fired by government forces exploded in the distance, not far from the street where he had lived all his life.

The rebels have made significant gains in the past two weeks, causing the United States and other members of an international coalition supporting them to voice hope that Tripoli would soon fall into their hands.

Their forces appeared to have stalled Wednesday in Zawiyah, which after four days of clashes was still the scene of chaotic back-and-forth fighting between the rebels and Gaddafi supporters.

Rebels managed to halt the output of oil from the city’s refinery, but apparently without immediate effect in the capital, according to refugees.

Families fleeing Tripoli in new Korean-made sedans drove through the violence in Zawiyah to reach rebel-controlled areas. Rebel forces counting the refugees heading to the mountains said more than 2,000 families had passed their checkpoint in the village of Bir Aiab, south of Zawiyah.

Farook Sherwan, Tripoli resident and father of three, stepped out of his car to register with the rebel administration, saying he expected “a big battle” in the capital soon.

“In the last days, we saw fewer and fewer government checkpoints and increasingly heard the sounds of gunfire,” he said.

Also Wednesday, Nasser al-Mabrouk Abdullah, the Libyan deputy security chief, who arrived in Egypt under a shroud of mystery this week, denied reports that he had defected.

Abdullah appeared on Libyan state television and said he had traveled to Cairo to deal with health problems.

Correspondent Leila Fadel in Cairo contributed to this report.