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. This study shows that most known mediators of immunity, such as TLR2, MyD88, T cells or B cells, and neutrophils and monocytes, are dispensable for pain produced by Staphylococcus aureus infection; instead, bacterial products, such as N-formylated peptides and α-haemolysin, induce pain by directly activating nociceptor neurons, which in turn modulate inflammation. Bacterial infections such as those caused by Staphylococcus produce pain thought to be secondary to the immune response and inflammation. Now Clifford Woolf and colleagues report a previously unsuspected mechanism of pain induction during bacterial infection: a direct pathogen-mediated activation of nociceptors. They find that pain produced by Staphylococcus aureus infection in mice is independent of most known mediators of immunity. Rather, the bacteria produce two classes of molecules — formylated peptides and pore-forming toxins — that induce pain by directly activating nociceptor neurons that in turn modulate inflammation.