As infectious diseases pose a threat to host integrity, eukaryotes have evolved mechanisms to eliminate pathogens. In addition to develop strategies reducing infection, animals can engage in behaviors that lower the impact of the infection. The molecular mechanisms by which microbes impact host behavior are not well understood. We demonstrate that bacterial infection of Drosophila females reduces oviposition and that peptidoglycan, the component that activates Drosophila antibacterial response, is also the elicitor of this behavioral change. We show that peptidoglycan regulates egg-laying rate by activating NF-κB signaling pathway in octopaminergic neurons and that, a dedicated peptidoglycan degrading enzyme acts in these neurons to buffer this behavioral response. This study shows that a unique ligand and signaling cascade are used in immune cells to mount an immune response and in neurons to control fly behavior following infection. This may represent a case of behavioral immunity.