Theory of few-photon quantum scattering in nanophotonic structures