In the case of batteries of towed artillery the temporary magazine would be placed, if possible, in a pit, or natural declivity, or surrounded by sandbags or earthworks.
The path through which the naval artillery's ammunition passed typically has blast-resistant airlocks and other safety devices, including provisions to flood the compartment with seawater in an emergency.
See especially the Oliver Hazard Perry-class frigates, owned by several different navies around the world, in which one 40-missile magazine carries a mixture of all three types of missiles: surface-to-air, surface-to-surface, and surface-to-underwater.
[4] Battleships were highly armored to protect from external attack, but the strength of the construction aids to constrict and worsen the impact of internal explosions, as the rigid steel does not allow blast waves to dissipate.
During the 1941 Attack on Pearl Harbor, the USS Arizona was destroyed when a Japanese armor-piercing bomb punched through her deck and detonated in proximity to the ship's ammunition magazine, which was caught on film.
The magazines of the Japanese battleship Yamato exploded in 1945 after hours of continuous assault by Allied aircraft, utterly destroying the ship and leaving few survivors.