Uruguay opened the scoring when Cavani and Luis Suarez teamed up brilliantly after eight minutes. Cavani took the ball on the right touchline in Portugal's half then hit Suarez on the opposite side of the field with a long ball.
The Uruguayan striker chested the ball down then dropped his shoulder and cut back inside before delivering a cross to the far post.
After passing to Suarez, Cavani had continued his run to the back post and he met the cross, heading home past goalkeeper Rui Patricio to make it 1-0 after eight minutes.
Portugal pressed forward and had more of the ball inside the opening 20 minutes but apart from a header that went wide, it was unable to create any clear-cut chances on goal as Uruguay's defence swarmed on anyone who got within range.
Suarez forced Portugal goalkeeper Rui Patricio into a diving one-handed save down low to his left from a free kick after he was chopped down by Jose Fonte, and Ronaldo too missed a chance to convert one of his trademark free kicks when he slammed a set piece into the wall after half-an-hour.
Portugal finally found a way through Uruguay's defence 10 minutes into the second half when a cross was whipped into the box after a short corner, allowing Pepe to leap above his man and score his sixth international goal โ heading into the back of the net to make it 1-1.
But the joy was short-lived as Cavani showed exquisite touch to help Uruguay regain the lead with his second goal of the night. Pepe's loose header in defence found a Uruguayan player and he slipped it to Cavani, who opened himself up and struck the ball first time, curling it into the bottom corner from the edge of the area.
"That is pure gold," one commentator said. "That is simply stunning. Any angle, any situation, he can find a way to goal."
Portugal pushed forward as it searched desperately for an equaliser but although Uruguay's defence cracked once, it wouldn't crack again.
Patricio rushed upfield in the final minute and he and his teammates appealed for a handball from a corner, but those in charge of the VAR decided there was nothing to look at and Uruguay held on for the tense victory.
Uruguay will play France in the quarterfinals.