The Axis offensive in Libya (Battle of Gazala) forced an Allied retreat deep inside Egypt until Axis forces were stopped at El Alamein (First Battle of El Alamein), which lasted from 1 to 27 July 1942. The British prevented a second advance by the Axis forces into Egypt. Axis positions near El Alamein, only 66 mi (106 km) from Alexandria, were dangerously close to the ports and cities of Egypt, the base facilities of the Commonwealth forces and the Suez Canal.
The Allies commenced an attack of their own in Egypt, dislodging the Axis forces and beginning a drive west across Libya. The Second Battle of El Alamein took place near the Egyptian railway halt of El Alamein, the battle lasted from 23 October to 11 November 1942.