How did Nagato have his eyes before Madara’s death during the 2nd Shinobi war?
Madara implanted the Rinnegan onto Nagato when he was very young. And this was done without Nagato's knowledge. Then the Second Shinobi War started, after a while.

After implanting the eyes, Madara waited for years to find a person who could be used as a tool. This "tool" was supposed to guide Nagato to properly use the Rinnegan at the right time, bringing Madara back alive, at his prime state.

So in order to find a suitable "tool", Madara connected himself with the Gedo Statue and siphoned it's life essence. He would die the moment he disconnected, as he was far past his natural death.

Madara then died after finding Obito, training him and handing over his name.