Experimental and simulation data [Moreau et al., Plasma Phys. Control. Fusion 62, 014013 (2019); Kaymak et al., Phys. Rev. Lett. 117, 035004 (2016)] indicate that self-generated magnetic fields play an important role in enhancing the flux and energy of relativistic electrons accelerated by ultra-intense laser pulse irradiation with nanostructured arrays. A fully relativistic analytical model for the generation of the magnetic field based on electron magneto-hydrodynamic description is presented here. The analytical model shows that this self-generated magnetic field originates in the nonparallel density gradient and fast electron current at the interfaces of a nanolayered target. A general formula for the self-generated magnetic field is found, which closely agrees with the simulation scaling over the relevant intensity range. The result is beneficial to the experimental designs for the interaction of the laser pulse with the nanostructured arrays to improve laser-to-electron energy coupling and the quality of forward hot electrons.