Though it was already ancient, Baku first came to prominence after an 1191 earthquake destroyed the region’s previous capital, Şamaxı. Wrecked by Mongol attacks, then a vassal to the Timurids, Baku returned to brilliance under Shirvanshah Khalilullah I (1417–65), who completed his father’s construction of a major palace complex. The Şirvan dynasty was ousted in 1501 by Shah Ismail I, remembered as poet ‘Xatai’ in Azerbaijan. He sacked Baku and then forcibly converted the previously Sunni city to Shia Islam. When Peter the Great captured the place in 1723, its population was less than 10,000, its growth hamstrung by a lack of trade and drinking water. For the next century Baku changed hands several times between Persia and Russia, before being definitively ceded to the Russians with agreements in 1806, 1813 and 1828.

Oil had been scooped from surface diggings around Baku since at least the 10th century. However, when commercial extraction was deregulated in 1872 the city rapidly became a boom town. Workers and entrepreneurs arrived from all over the Russian Empire, swelling the population by 1200% in under 30 years.

Baku’s thirst was slaked by an ambitious new water canal bringing potable mountain water all the way from the Russian border, and the city’s desert image was softened by parks nurtured using specially imported soil. By 1905 Baku was producing around 50% of the world’s petroleum and immensely rich ‘oil barons’ built luxurious mansions outside the walls of the increasingly irrelevant Old City. Meanwhile, most oil workers lived in appalling conditions, making Baku a hotbed of labour unrest and revolutionary talk. Following a general strike in 1904, the Baku oil workers negotiated Russia’s first-ever worker-management contract. But tensions continued to grow.

In the wake of the two Russian revolutions Baku’s history became complex and very bloody with a series of brutal massacres between formerly neighbourly Armenian and Azeri communities. When the three South Caucasus nations declared their independence in 1918, Baku initially refused to join Azerbaijan’s Democratic Republic, a position bolstered by a small British force that secretly sailed in from Iran hoping to defend the oilfields against the Turks (Britain’s WWI enemies). Turkish and Azeri troops eventually stormed the city as the British ignominiously withdrew by sea under cover of darkness. In the end game of WWI, the Turks were forced to evacuate too and Baku became capital of independent Azerbaijan for almost two years until, on 28 April 1920, the Red Army marched into Baku.

In 1935 the search for oil moved into the shallow coastal waters of the Caspian. A forest of offshore platforms and derricks joined the tangle of wells and pipelines on land. Investment dwindled after WWII and only really resumed in earnest after independence, with foreign oil consortia spending billions exploring these resources from 1994. By 2005 they'd built the world’s second-longest oil pipeline, BTC, to get Azeri oil to Ceyhan in Turkey, bypassing Russia and Iran. Almost instantly, as money flooded in, Baku boomed once more. Fountains, flagpoles and countless new multi-storey towers mushroomed, including a few jaw-droppingly impressive works of architectural inspiration. Some of the city's most atmospheric areas (notably Sovetski) have been bulldozed for redevelopment, but the many grand older buildings that avoided demolition have been cleaned and up-lit while new yet antique-looking stone facades have been appended to many surviving Soviet-era blocks.