Accurate positioning inside confined urban underground pipelines is challenging due to the lack of geo-reference and the difficulty of access for geodetic apparatus. Micro-electromechanical system (MEMS) inertial navigation systems (INS) have become the mainstream positioning approach but face challenges in maintaining accuracy over a long time period because aiding for INS is limited. We propose a factor graph optimization (FGO) architecture for aided INS inside pipes, which enhances positioning accuracy by involving information fusion from three levels: measurements from physical sensors, motion constraints, and scene constraints. We achieved autonomous positioning using a typical industrial-grade MEMS inertial measurement unit within a 1700 m pipeline, with positioning accuracy of up to 1.0 m horizontally and 0.5 m vertically. Results demonstrate that the accuracy in horizontal and vertical directions improved by at least 45% and 26%, respectively, using FGO compared with traditional Kalman filtering methods.