Heat stress (HS) commonly occurs as a severe pathological response when the body's sensible temperature exceeds its thermoregulatory capacity, leading to the development of chronic brain inflammation, known as neuroinflammation. Emerging evidence suggests that HS leads to the disruption of the gut microbiota, whereas abnormalities in the gut microbiota have been demonstrated to affect neuroinflammation. However, the mechanisms underlying the effects of HS on neuroinflammation are poorly studied. Meanwhile, effective interventions have been unclear. β-Hydroxybutyric acid (BHBA) has been found to have neuroprotective and anti-inflammatory properties in previous studies. This study aims to explore the modulatory effects of BHBA on neuroinflammation induced by HS and elucidate the underlying molecular mechanisms. An invivo and invitro model of HS was constructed under the precondition of BHBA pretreatment. The modulatory effects of BHBA on HS-induced neuroinflammation were explored and the underlying molecular mechanisms were elucidated by flow cytometry, WB, qPCR, immunofluorescence staining, DCFH-DA fluorescent probe assay, and 16S rRNA gene sequencing of colonic contents. Heat stress was found to cause gut microbiota disruption in HS mouse models, and TM7 and [Previotella] spp. may be the best potential biomarkers for assessing the occurrence of HS. Fecal microbiota transplantation associated with BHBA effectively reversed the disruption of gut microbiota in HS mice. Moreover, BHBA may inhibit microglia hyperactivation, suppress neuroinflammation (TNF-α, IL-1β, and IL-6), and reduce the expression of cortical endoplasmic reticulum stress (ERS) markers (GRP78 and CHOP) mainly through its modulatory effects on the gut microbiota (TM7, Lactobacillus spp., Ruminalococcus spp., and Prevotella spp.). Invitro experiments revealed that BHBA (1 mM) raised the expression of the ERS marker GRP78, enhanced cellular activity, and increased the generation of reactive oxygen species (ROS) and anti-inflammatory cytokines (IL-10), while also inhibiting HS-induced apoptosis, ROS production, and excessive release of inflammatory cytokines (TNF-α and IL-1β) in mouse BV2 cells. β-Hydroxybutyric acid may be an effective agent for preventing neuroinflammation in HS mice, possibly due to its ability to inhibit ERS and subsequent microglia neuroinflammation via the gut-brain axis. These findings lay the groundwork for future research and development of BHBA as a preventive drug for HS and provide fresh insights into techniques for treating neurological illnesses by modifying the gut microbiota.