The photocatalytic production of hydrogen peroxide (H 2 O 2 ) by graphitic carbon nitride (g-C 3 N 4 ) has been a promising technique. However, its photocatalytic property is limited by the easy combination of photogenerated charges, low reaction selectivity, and reaction rate in the O 2 reduction reaction (ORR) process. Hence, a photocatalytic nanoreactor system is designed and constructed by embedding the P, N co-doped carbon nanomaterials in a g-C 3 N 4 hollow sphere. The P, N co-doped nanocarbon as an acceptor and transporter for photo-generated electrons can inhibit the combination of photogenerated carriers with electron transfer from CNHS to P, N−C. Particularly, the P, N co-doped carbon active sites are beneficial to improve two-electron (2 e − ) ORR selectivity with favorable formation of hydroperoxo species (−OOH). With the promoted electron transfer and 2 e − ORR selectivity for H 2 O 2 production in the confinement domain of the hollow sphere interior, the resultant photocatalytic system exhibits a high production rate towards H 2 O 2 of 239.5 μmol h −1 g −1 in pure water and 4568 μmol h −1 g −1 with isopropanol hole scavenger, which is 14.1 and 35.7 times those of bulk g-C 3 N 4 . This study offers an efficient avenue for the construction and improvement of photocatalysts and an insight into the understanding of photocatalytic H 2 O 2 production.