Background The invertebrate immune system comprises physiological mechanisms, physical barriers and also behavioral responses. It is generally related to the vertebrate innate immune system and widely believed to provide nonspecific defense against pathogens, whereby the response to different pathogen types is usually mediated by distinct signalling cascades. Recent work suggests that invertebrate immune defense can be more specific at least at the phenotypic level. The underlying genetic mechanisms are as yet poorly understood. Results We demonstrate in the model invertebrate Caenorhabditis elegans that a single gene, a homolog of the mammalian neuropeptide Y receptor gene, npr-1, mediates contrasting defense phenotypes towards two distinct pathogens, the Gram-positive Bacillus thuringiensis and the Gram-negative Pseudomonas aeruginosa. Our findings are based on combining quantitative trait loci (QTLs) analysis with functional genetic analysis and RNAseq-based transcriptomics. The QTL analysis focused on behavioral immune defense against B. thuringiensis, using recombinant inbred lines (RILs) and introgression lines (ILs). It revealed several defense QTLs, including one on chromosome X comprising the npr-1 gene. The wildtype N2 allele for the latter QTL was associated with reduced defense against B. thuringiensis and thus produced an opposite phenotype to that previously reported for the N2 npr-1 allele against P. aeruginosa. Analysis of npr-1 mutants confirmed these contrasting immune phenotypes for both avoidance behavior and nematode survival. Subsequent transcriptional profiling of C. elegans wildtype and npr-1 mutant suggested that npr-1 mediates defense against both pathogens through p38 MAPK signaling, insulin-like signaling, and C-type lectins. Importantly, increased defense towards P. aeruginosa seems to be additionally influenced through the induction of oxidative stress genes and activation of GATA transcription factors, while the repression of oxidative stress genes combined with activation of Ebox transcription factors appears to enhance susceptibility to B. thuringiensis. Conclusions Our findings highlight the role of a single gene, npr-1, in fine-tuning nematode immune defense, showing the ability of the invertebrate immune system to produce highly specialized and potentially opposing immune responses via single regulatory genes.