We study the effect of nonsolvent on the formation of polymer nanomaterials in the nanopores of porous templates. Water (nonsolvent) is added into a poly (methyl methacrylate) (PMMA) solution in dimethylformamide (DMF) confined in the nanopores of an anodic aluminum oxide (AAO) template. Water forms a wetting layer on the pore wall and causes the PMMA solution to be isolated in the center of the nanopore, resulting in the formation of PMMA nanospheres or nanorods after the solvent is evaporated. The formation of the polymer nanomaterials induced by nonsolvent is found to be driven by the Rayleigh-instability-type transformation. Without adding the nonsolvent, PMMA chains precipitate on the walls of the nanopores after the solvent is evaporated, and PMMA nanotubes are obtained. Poly(methyl methacrylate) (PMMA) solution in dimethylformamide (DMF) is first introduced to the nanopores of AAO templates by capillary force. When water (nonsolvent) is added, the polymer solution is isolated in the central part of the nanopores, resulting in the formation of polymer nanospheres or nanorods.