Chronic pain (CP) has been found to cause significant alternations of the brain’s structure and function due to changes in pain processing and disrupted cognitive functions, including with respect to the prefrontal cortex (PFC). However, until now, no studies have used a wearable, low-cost neuroimaging tool capable of performing functional near-infrared spectroscopy (fNIRS) to explore the functional alternations of the PFC and thus automatically achieve a clinical diagnosis of CP. In this case-control study, the pain characteristics of 19 chronic pain patients and 32 healthy controls were measured using fNIRS. Functional connectivity (FC), FC in the PFC, and spontaneous brain activity of the PFC were examined in the CP patients and compared to those of healthy controls (HCs). Then, leave-one-out cross-validation and machine learning algorithms were used to automatically achieve a diagnosis corresponding to a CP patient or an HC. The current study found significantly weaker FC, notably higher small-worldness properties of FC, and increased spontaneous brain activity during resting state within the PFC. Additionally, the resting-state fNIRS measurements exhibited excellent performance in identifying the chronic pain patients via supervised machine learning, achieving F1 score of 0.8229 using only seven features. It is expected that potential FC features can be identified, which can thus serve as a neural marker for the detection of CP using machine learning algorithms. Therefore, the present study will open a new avenue for the diagnosis of chronic musculoskeletal pain by using fNIRS and machine learning techniques.