To extract from an image of a single nanoscale object maximum physical information about its position, we propose and demonstrate a framework for pupil-plane modulation for 3D imaging applications requiring precise localization, including single-particle tracking and superresolution microscopy. The method is based on maximizing the information content of the system, by formulating and solving the appropriate optimization problem--finding the pupil-plane phase pattern that would yield a point spread function (PSF) with optimal Fisher information properties. We use our method to generate and experimentally demonstrate two example PSFs: one optimized for 3D localization precision over a 3 μm depth of field, and another with an unprecedented 5 μm depth of field, both designed to perform under physically common conditions of high background signals.