Traditional ultraviolet–visible spectroscopic quantitative analytical methods face challenges in simultaneous and long-term accurate measurement of chemical oxygen demand (COD) and nitrate due to spectral overlap and the interference from stochastic background caused by turbidity and chromaticity in water. Addressing these limitations, a compact dual optical path spectrum detection sensor is introduced, and a novel ultraviolet–visible spectroscopic quantitative analysis model based on physics-informed multi-task learning (PI-MTL) is designed. Incorporating a physics-informed block, the PI-MTL model integrates pre-existing physical knowledge for enhanced feature extraction specific to each task. A multi-task loss wrapper strategy is also employed, facilitating comprehensive loss evaluation and adaptation to stochastic backgrounds. This novel approach significantly outperforms conventional models in COD and nitrate measurement under stochastic background interference, achieving impressive prediction R2 values of 0.941 for COD and 0.9575 for nitrate, while reducing root mean squared error (RMSE) by 60.89 % for COD and 77.3 % for nitrate in comparison to the conventional chemometric model partial least squares regression (PLSR), and by 30.59 % and 65.96 %, respectively, in comparison to a benchmark convolutional neural network (CNN) model. The promising results emphasize its potential as a spectroscopic instrument designed for online multi-parameter water quality monitoring against stochastic background interference, enabling long-term accurate measurement of COD and nitrate levels.