Bacteria secrete a large variety of beneficial metabolites into the environment, which can be shared as public goods among producing bacteria, but also be exploited by nonproducing cheats. Here, we focus on cooperative production of iron-chelating molecules (siderophores) in the bacterium Pseudomonas aeruginosa to study how relevant ecological factors influence selection for cheating. We designed patch-structured metapopulations that allowed us introducing among-patch ecological variation. We found that cheating readily evolved in uniform iron-limited environments. This finding is explained by severe iron limitation demanding high siderophore-production efforts, which results in high metabolic costs accruing to cooperators, and thereby facilitates the spread of cheats. In contrast, we observed a significant reduction or even negation of selection for cheating in metapopulations where we introduced patches with increased iron availability and/or opportunities to recycle siderophores. These findings are compatible with the view that cheats are less likely to invade in environments that allow bacteria to reduce siderophore-production efforts, as this lowers the overall metabolic costs accruing to cooperators. Because we increased iron availability and siderophore recycling opportunities moderately, and only in some patches, our findings demonstrate that already-small local variations in ecological conditions as occurring in nature can significantly affect selection for public-goods secretion in microbes. In addition, we found that most (84.6%) of the evolved cheats were partially deficient for siderophore production and not loss-of-function mutants. Genetic considerations indicate that mutations leading to partial deficiency occur more frequent than mutations leading to loss of function, but also suggest that partially deficient mutants might often be the more competitive cheats.