Israel’s offensive in the Gaza Strip city of Rafah has heaped pressure on Egyptian President Abdel Fattah Al Sisi to respond more forcefully to a former adversary that Egypt has long held a cold peace with, but also viewed with deep suspicion.

Since Israel began deploying troops along Gaza’s southern border in recent weeks, the Egyptian military has issued instructions to soldiers at the border to return fire if fired upon, Egyptian officials say. Egypt has warned Israel that it won’t hesitate to respond militarily if its security is threatened, they added.

With Israeli forces saying this week that they now control the roughly 9-mile border , Israeli troops are just a few hundred yards away from Egyptian soldiers holding positions on the other side of the Gaza fence. The chances of a miscalculation are high.

On Monday, those fears were realized when a rare clash on the border left two Egyptian soldiers dead, according to officials and a family member, and sparked outrage in the Arab country.

This week’s events have put a spotlight on two of the Sisi government’s worst fears: that the fighting could spill into Egypt, and that the Gaza war could stir up a popular revolt in the Middle East’s most populous country. Already, a series of small pro-Palestinian demonstrations in Egypt have heightened fears among security officials that the public discontent could eventually turn against the government.

A banner depicting Egypt’s President Abdel Fattah al-Sisi, as people prepare for a rally to support him in upcoming presidential election in December, in Cairo, Egypt, October 2, 2023. REUTERS/Amr Abdallah Dalsh

Recent events have been “humiliating for Egypt,” said Michael Hanna, the director of the U.S. program at International Crisis Group and an expert on Egyptian politics and security. “The lack of any response is going to be frustrating for a lot of people,” he said.

The Egyptian Foreign Ministry didn’t respond to a request for comment. The Israeli Foreign Ministry declined to comment.

Israel has said that capturing the Gazan side of the border with Egypt is a central aim of the war, intending to cut off what it says is Hamas’s ability to smuggle weapons into the Gaza Strip. Egypt has disputed Israel’s charges that it isn’t doing enough to clamp down on arms smuggling. Israeli Prime Minister Benjamin Netanyahu also says the Rafah campaign aims to destroy remaining Hamas military forces in the area.

Despite some veiled threats, Egypt has ruled out military action against Israel for now and opted for what officials are calling a containment strategy designed to gradually increase pressure on Israel. The policy includes conditioning the reopening of the Rafah crossing—a vital route for humanitarian aid to Gaza from Egypt—on the Israeli military’s withdrawal from the area and return of control over the crossing to the Palestinians, and joining a case accusing Israel of genocide at the International Court of Justice, an accusation that Israel has denied. If Egypt exhausts those other options, the government would freeze relations with Israel entirely, according to Egyptian officials.

Earlier in the war, Israel praised Egypt’s role as a mediator with Hamas in talks that led to the release of more than 100 hostages taken captive when the U.S.-designated terrorist group stormed Israel and killed more than 1,200 people, mostly civilians, according to Israeli officials. After a decade of Egypt’s decline as a regional power, Sisi “spent the last eight months leveraging this crisis to prove Egypt’s diplomatic importance,” said Timothy Kaldas, deputy director of the Tahrir Institute for Middle East Policy, a think tank based in Washington.

Satellite image shows the Kerem Shalom border crossing between Israel and Egypt as supplies and material are being brought, in the aftermath of the Rafah airstrike, May 29, 2024. Maxar Technologies/Handout via REUTERS

From the beginning, the war has also squeezed Egypt’s already strained economy, with revenue from the Suez Canal plummeting because of reduced traffic amid Yemeni rebel attacks on Red Sea shipping routes . The government recently announced an increase in subsidized bread prices and devalued its currency, measures that have hit working-class and poor Egyptians hard.

And ordinary Egyptians are increasingly angry about the course of the war as the Palestinian death toll mounts. The Israeli offensive in Gaza has killed more than 36,000 Palestinians, mostly civilians, Palestinian officials have said, without specifying how many were combatants.

Egypt was the first Arab state to recognize Israel. The two countries, after fighting a series of wars, have developed an important security partnership since 1979, with Egypt becoming a cornerstone of Israel’s strategy to gain acceptance in the Middle East. The nations’ security agencies have worked together closely, particularly over the past decade under Sisi, during which they shared intelligence to help defeat Islamic State extremists in Egypt’s North Sinai region.

But relations have always been publicly chilly and never extended to average Israelis or Egyptians, who don’t generally visit each other’s countries or do much business.

The most recent flashpoint came in early May, when the Israeli military ignored Egyptian warnings against launching its operation in Rafah.

Israel gave Egypt just hours’ notice of the impending operation , in which it seized control of the Gazan side of the Rafah border crossing with Egypt, Egyptian officials said. The Egyptian government closed its side of the crossing in protest and threatened to downgrade Egypt’s diplomatic representation in Israel.

Egyptian officials say that the presence of Israeli forces in the area along the border with Egypt violates the terms of the 1979 peace treaty, which places limits on the number of troops that both countries can deploy near their shared border. The treaty bans Israel from deploying tanks, artillery and antiaircraft weapons in a narrow strip along the border with Egypt.

A senior Israeli official said that Israel “is acting in self-defense and isn’t in breach of any agreement.”

“Does Israel fully understand the meaning of Egypt’s warnings against entering Rafah? Will it choose this option to the detriment of the peace that has lasted for 45 years?,” said Diaa Rashwan, an Egyptian government spokesman, in an interview in April with pan-Arab news channel al-Arabiya.

Egyptian military leaders have been particularly concerned in recent weeks about the presence of Israeli forces in proximity to Egyptian conscript soldiers, who often have had limited training, along the border. The Egyptian military started rotating soldiers every few days to avoid a scenario in which Egyptian soldiers could take matters into their own hands and open fire at Israeli forces.

The precautions failed to prevent a deadly exchange of fire on Monday. The Egyptian military has acknowledged the death of only one soldier and said it was investigating the incident, part of an approach of publicly playing down the incident, a sign of the stakes involved for Sisi.

The death of a second soldier was confirmed by a relative and by Egyptian officials. At his funeral in Egypt’s rural Fayoum province just south of Cairo, an imam led a crowd in prayers against what he called “Zionist traitors” and in support of the “mujahedeen of Palestine,” according to a recording of a livestream of the funeral.

A series of small protests in Egypt has heightened fears among Egyptian security officials that any unrest could eventually turn against the government. Since Sisi came to power in a 2013 coup, Egyptian authorities have jailed thousands in a broad attempt to end an era of popular protests and unrest following the 2011 revolution that ousted the president at the time, Hosni Mubarak .

Egyptian authorities have arrested 120 people in connection with pro-Palestinian protests since the war began, according to a list of the detainees compiled by the Egyptian Commission for Rights and Freedoms, a nongovernmental group based in Cairo.

“As much as there is sympathy with the Palestinians in general, there is a fear of sliding into a situation of war, so that’s on everyone’s minds,” said Mohamed Lotfy, executive director of the commission.

Ramadan Mohamed, father of Abdallah Ramadan, an army soldier killed in Rafah, is embraced during a funeral reception attended by relatives and friends in his hometown in Fayoum, Egypt, May 28, 2024. REUTERS/Mohamed Abd El Ghany

People attend the funeral of Islam Ibrahim Abdel Razeq, Egyptian army soldier killed in Rafah, in his hometown, in Fayoum, Egypt, May 28, 2024. REUTERS/Mohamed Abd El Ghany

Write to Jared Malsin at jared.malsin@wsj.com and Summer Said at summer.said@wsj.com