Nociceptor sensory neurons are specialized to detect potentially damaging stimuli, protecting the organism by initiating the sensation of pain and eliciting defensive behaviours. Bacterial infections produce pain by unknown molecular mechanisms, although they are presumed to be secondary to immune activation. Here we demonstrate that bacteria directly activate nociceptors, and that the immune response mediated through TLR2, MyD88, T cells, B cells, and neutrophils and monocytes is not necessary for Staphylococcus aureus-induced pain in mice. Mechanical and thermal hyperalgesia in mice is correlated with live bacterial load rather than tissue swelling or immune activation. Bacteria induce calcium flux and action potentials in nociceptor neurons, in part via bacterial N-formylated peptides and the pore-forming toxin α-haemolysin, through distinct mechanisms. Specific ablation of Nav1.8-lineage neurons, which include nociceptors, abrogated pain during bacterial infection, but concurrently increased local immune infiltration and lymphadenopathy of the draining lymph node. Thus, bacterial pathogens produce pain by directly activating sensory neurons that modulate inflammation, an unsuspected role for the nervous system in host-pathogen interactions.