In the early 1860s, Spain was dealing with the loss of many of its overseas colonies. Spain also suffered a series of minor insults to their prestige at the hands of Peru, a former Spanish colony. In an effort to save face and collect on debts left over the Peruvian War of Independence, Spanish forces launched a fleet of ships in April 1864 to seize the valuable Chincha Islands off the coast of Peru.
The Chincha Islands supplied over half of Peru's annual budget at the time as laborers collected bird guano from 150-foot-high deposits. Yes, sh-t stacks that high. In the late 1800s, bird guano was known as both a great fertilizer and a great source of saltpeter. Saltpeter mixed in the proper ratio with sulfur and charcoal forms gunpowder.
So when the Spanish landed 400 marines on the islands and formed a blockade of Peruvian ports, the Peruvian government was understandably angry. A political crisis followed as negotiations repeatedly failed to end the standoff. This stalemate drug on from April 1864 to September 1865.
That was when a Spanish ship went to a Chilean port to refuel with coal and the Chileans refused it on the basis that they couldn't support Spain's occupation of the islands and blockade of Peru. The Spanish commander, Vice Adm. Juan Manuel Pareja, sailed his flagship into the Chilean harbor and demanded a military salute from the town. When it was refused, Spain demanded reparations payments. The Chileans declared war in September 1865 instead. The Chincha Islands war was on.
Chile won an early victory at the Battle of Papudo when it captured a Spanish ship, the Covadongo. Peru then entered open hostilities with Spain and signed a treaty with Chile. Spain, Peru, and Chile fought for a year before Ecuador and Bolivia joined the fight against Spain in late-1866.
In early 1867 Spain crippled Chile's merchant marine and shelled a Chilean city. While the attacks were militarily successful, the rest of the world was quick to condemn Spanish actions. Spain found itself with even fewer friends as Britain and America condemned the attacks.
Spain eventually gave up on the war without having captured any ground beyond the original guano-soaked gains in the Chincha Islands. As the Spanish withdrew, Peru took the islands again and have continued to collect the guano since.
The Chincha Islands War wasn't the only conflict the nations fought for guano. The War of the Pacific from 1879-1884 was partially over guano deposits in the Atacama Desert.