The nematode Caenorhabditis elegans responds to pathogenic bacteria with conserved innate immune responses and pathogen avoidance behaviors. We investigated natural variation in C. elegans resistance to pathogen infection. With the use of quantitative genetic analysis, we determined that the pathogen susceptibility difference between the laboratory wild-type strain N2 and the wild isolate CB4856 is caused by a polymorphism in the npr-1 gene, which encodes a homolog of the mammalian neuropeptide Y receptor. We show that the mechanism of NPR-1-mediated pathogen resistance is through oxygen-dependent behavioral avoidance rather than direct regulation of innate immunity. For C. elegans, bacteria represent food but also a potential source of infection. Our data underscore the importance of behavioral responses to oxygen levels in finding an optimal balance between these potentially conflicting cues.