The detection of train track fasteners is an important task to ensure the safe, reliable, and long-term stable operation of the railway system. Under the high stress and dynamic loads during train operation, track fasteners play a crucial role in maintaining the alignment and structural integrity of the tracks. However, traditional manual inspection methods are time-consuming, labor-intensive, and error-prone, while existing defect-detection algorithms often have the problem of low accuracy. To address these challenges, an improved YOLOv8n algorithm named MGSF-YOLO for high-precision detection of train track fastener defects is proposed. Firstly, an attention mechanism MSAM that combines MSCA and CBAM is proposed. MSAM adaptively adjusts the feature dimensions of different channels and the attention intensity at spatial positions through convolution kernels of various scales. This allows the model to focus on key features and regions and simultaneously enhances information extraction by exploiting the correlations between channels, thereby improving the model's detection performance. Secondly, GSConv is used to replace Conv. This not only reduces the number of model parameters and computational cost but also improves the model's detection accuracy. Then, an SPPFPool module that integrates average pooling and max pooling is proposed. This module aggregates the edge, background, and contextual information of the entire image, alleviates the impact of image scale changes, enhances the model's adaptability to inputs of different sizes, effectively reduces false positives and false negatives, and improves the detection accuracy. Finally, the FocalerIoU loss function is introduced to replace the original CIoU loss function, which improves the regression accuracy and accelerates the model's convergence speed. Experimental results show that compared with YOLOv8n, MGSF-YOLO has achieved performance improvements. The number of model parameters has decreased by 12.2%, and the computational amount has been reduced by 6.1%. Without sacrificing the detection speed, the mean average precision (mAP) has also increased by 2.6%. On the coco128 dataset, the publicly available NEU-DET dataset, and the real-world fastener dataset collected from the Roboflow website, the mAP of MGSF-YOLO has increased by 1.6%, 2.4%, and 3.6%, respectively, compared to YOLOv8n. Compared with other models, MGSF-YOLO reaches the highest mAP value, fully demonstrating its superior generalization ability and detection accuracy. It provides a reliable solution for the detection of railway fastener defects.