Rail surface defects typically serve as early indicators of railway malfunctions, which may compromise the quality and corrosion resistance of rails, thereby endangering the safe operation of trains. The timely detection of defects is essential to ensure the safe operation of railways. To improve the classification accuracy of rail surface defect detection, this paper proposes a rail surface defects detection algorithm based on MobileNet-YOLOv7. By integrating lightweight deep learning algorithms into the engineering application of rail surface defect detection, a MobileNetV3 lightweight network is used as the backbone network for YOLOv7 to enhance both speed and accuracy in complex defect extraction. Subsequently, the efficient intersection over union (EIOU) loss function is utilized as the positional loss function to bolster system resilience. Finally, the k-means++ clustering algorithm is applied to obtain new anchor boxes. The experimental results demonstrate the effectiveness of the proposed method, achieving superior detection accuracy compared with traditional algorithms.