In this paper, a nonlinear PID (NLPID) controller is used to replace a traditional PID controller to overcome the influence of nonlinear factors in the computer numerical control (CNC) system. A particle swarm optimization based on a generalized opposition-based learning (G-PSO) algorithm is proposed to optimize the NLPID controller. The convergence speed and global optimization ability of the particle swarm optimization (PSO) algorithm are improved by introducing generalized opposition-based learning. The natural selection mutation is introduced into the G-PSO algorithm to further avoid the particles falling into local optimization. Different from the existing research, this paper designs a special fitness function according to the control objectives of improving system response speed and suppressing overshoot. By comparing the differential evolution (DE) algorithm, the ant lion optimizer (ALO) and the genetic algorithm (GA) through simulation, it is proven that the G-PSO algorithm has a faster convergence speed and better global optimization ability. Compared to Fuzzy PID and MRAC PID, G-PSO NLPID is shown to be more suitable for CNC systems. Additionally, it is proven through experiments that the rise time and settling time of the NLPID controller optimized by the G-PSO algorithm are 22.22% and 24.52% faster, respectively, than the traditional PID controller, and the system overshoot is successfully suppressed.