BILASUVAR, Azerbaijan — The perennially tense relationship between Azerbaijan and Iran, wary neighbors on the Caspian Sea, has deteriorated in recent weeks amid deep unease in Tehran over expanding military cooperation between Azerbaijan and Israel.

A vital border crossing here has been shut for days at a time, stranding long lines of trucks. Not far away, Iranian warships maneuver in the Caspian Sea. Last week, a senior aide to Iran’s supreme leader, Ayatollah Ali Khamenei, was refused entry at the airport in Baku, the Azerbaijani capital. Ambassadors on each side have returned home.

And a public relations war is raging as officials trade nasty barbs online and in the news media — including an exchange in which the two predominantly Muslim countries each accused the other of being overly friendly to gay people.

In March, in perhaps the gravest sign of the strains, authorities in Azerbaijan arrested 22 people they said were part of an Iranian-backed plot to kill American and Israeli diplomats and attack other targets in Baku, though the allegations are as yet unproved.