How did the British come to control Gibraltar?
How important was Gibraltar to the British empire?
Did Gibraltar ever begin to decline in strategic importance?
Did the British ever come close to losing Gibraltar in a war?
Gibraltar was taken by the British during the war of Spanish Succession. It was taken to help counteract the threat posed by the Franco-Spanish fleet, the Balearic island of Manorica was also taken for this reason, but it was given back. At first Britain captured the town in the name of the Spanish pretender they were backing, but in the treaty of Urtech, they retained control of the town. Gibraltar's importance has went up and down and is very open to debate. In both World Wars Gibraltar allowed supplies to go into the Mediterranean and straight to Malta or wherever else, rather than go around Africa and through the Suez canal.
Gibraltar's strategic importance does decline a bit when Britain isn't at war in the Mediterranean, but it is still important. It is one of the colonies, along with Aden, Malta, Cyprus, and Belize that the British colonial office felt it needed to keep under British control for strategic and security reasons. Gibraltar has been besieged three times since being under British control. The first time was an attempt by the French and Spanish to recapture it in the War of Spanish succession, the second was a minor siege in the Anglo- Spanish war of 1727, and the final third time was during the American revolutionary war by Spain and France again. The third siege is arguably when the British came closest to losing Gibraltar.
Sources:
Rise and Fall of the British Empire by: Lawrence James
The Great Siege of Gibraltar, during the American Revolution, was the longest siege the British Armed forces have every endured throughout a long history of battle and sieges.
The siege lasted for 3 years 7 months. At times the situation for the defenders seemed desperate, and they were starving, but the British Navy always managed to sneak or force in re-supply at critical moments. This siege was the closest the British ever came to losing Gibraltar in a war.
A possible indirect consequence of the Great Siege of Gibraltar in the American Revolution might have been to get the US better terms at the peace settlement.
According to treaties, America could not make peace with Britain independently from France. France, however, by a secret treaty, could not make peace independently from Spain. Furthermore, France had ( in another secret treaty) guaranteed Spain that France would not make peace until Spain had regained Gibraltar from Britain.
In 1781, Britain decided they wanted to end the war (or at least the war with the Americans). They sent private messages to Benjamin Franklin (through Henry Laurens, an American envoy the British had captured) saying they were willing to talk.
Franklin, Laurens, and Adams wanted to talk peace, but the French were not ready. They were engaged with Spain in the Great Siege of Gibraltar and wanted to capture Gibraltar before making peace.
France's reluctance to talk about peace led the wily Franklin to open secret talks with the British. Because the French were stalling (in hopes of winning Gibraltar for Spain) the British were keen to split the allies and make concessions to the Americans.
Franklin got the British to concede the Mississippi as a western boundary, and the Northwest Territories (which Britain had transferred to Canada in "The Quebec Act of 1774". Adams (looking after Massachusetts interests) got the British to concede Grand Banks Cod fishing rights.
Spain, stalling the peace in order to try to capture Gibraltar, allowed the US to get much more out of the Peace than they had thought possible.
At the end of the day, Spain never did get Gibraltar. The battered and starving garrison fought off every attack. In 1782, the Royal Navy sent a re supply convoy, protected by 33 battleships to break through to Gibraltar or die trying. The navy lured the French and Spanish ships away from the harbor, and the undetected convoy made a dash for it. Gibraltar was re supplied without a battle being fought, and France and Spain decided to throw in the towel and negotiate for peace.
By this time, the US had already agreed all of their terms with the British, and although Spain didn't like the US border on the Mississippi, and France didn't like the US fishing rights, it was too late for them to do anything about it.
So, in an indirect way the heroic resistance by the British defending Gibraltar proved very beneficial to the favorable US peace terms at the end of the Revolutionary War.
In response to the second question, Sir John Fisher - First Sea Lord from 1904 to 1910 - once described Gibraltar, Alexandria, the Cape of Good Hope, Dover, and Singapore as "five keys [that] lock up the world". This referred to the usefulness of these ports for interdicting the five most important waterways in the world: the Mediterranean, the Suez Canal, the Clipper Route, the English Channel and the Strait of Malacca (this predated the opening of the Panama Canal).
By controlling these five ports, the UK could exert control over the five waterways. Due to the huge volume of ocean-borne trade moving through at least one of the ports, this was a major strategic advantage.
Source: "The Geopolitics of Super Power" by Colin Gray.