Microbes represent both an essential source of nutrition and a potential source of lethal infection to the nematode Caenorhabditis elegans. Immunity in C. elegans requires a signaling module comprised of orthologs of the mammalian Toll-interleukin-1 receptor (TIR) domain protein SARM, the mitogen-activated protein kinase kinase kinase (MAPKKK) ASK1, and MAPKK MKK3, which activates p38 MAPK. We determined that the SARM-ASK1-MKK3 module has dual tissue-specific roles in the C. elegans response to pathogens--in the cell-autonomous regulation of innate immunity and the neuroendocrine regulation of serotonin-dependent aversive behavior. SARM-ASK1-MKK3 signaling in the sensory nervous system also regulates egg-laying behavior that is dependent on bacteria provided as a nutrient source. Our data demonstrate that these physiological responses to bacteria share a common mechanism of signaling through the SARM-ASK1-MKK3 module and suggest the co-option of ancestral immune signaling pathways in the evolution of physiological responses to microbial pathogens and nutrients.