The battle over Sofía Vergara’s frozen embryos with ex-fiancé Nick Loeb has taken a shocking turn. The actress is demanding Loeb reveal the names of two former girlfriends who underwent abortions around 20 years ago.

A California judge upheld Vergara’s request that Loeb, 41, name the women who terminated their pregnancies many years ago. A horrified Loeb appealed the decision, but the Court of Appeals backed Vergara.

Vergara’s legal team wants to quiz both women under deposition to probe Loeb’s sexual past, but he fears the women’s names could become public due to the high-profile nature of the case, and the women could face intense scrutiny and shame.

Loeb exclusively told Page Six that despite losing at the appellate court, “I would rather go to jail than reveal the names. I believe we have to protect a woman’s right to privacy.”

A heavily redacted petition from Loeb reads, “Not only are Plaintiff’s [Loeb’s] privacy rights under attack but so are the rights of third-party individuals who have nothing to do with [Loeb’s] relationship with Ms. Vergara, the embryos or the litigation.” It also described Vergara’s move as “an attempt to harass third parties to obtain testimony.”

Loeb filed a lawsuit in 2015 to protect, and bring to term, the two female embryos he created with Vergara, who is now married to Joe Manganiello. Vergara and Loeb had planned to use a surrogate but did not agree on what would happen if they split.

Loeb, who intends to raise the children himself without any parental or financial help from Vergara, wrote in a New York Times op-ed, “When I was in my 20s, I had a girlfriend who had an abortion, and the decision was entirely out of my hands. Ever since, I have dreamed about a boy at the age he would be now.” Another ex-girlfriend terminated a pregnancy when Loeb was 19.

Vergara wants the embryos destroyed, and previously said, “A child needs a mother and a loving relationship with parents who don’t hate each other . . . I wouldn’t want to bring kids to the world where it’s already set against them. It would be so selfish.” Her rep didn’t get back to us Monday night.