Poorer mental health is common in undergraduate students due to academic stress. An interplay between stress and diet exists, with stress influencing food choices. Nutritional interventions may be effective in preventing mental health decline due to complex bidirectional interactions between the brain, the gut and the gut microbiota. Previous studies have shown walnut consumption has a positive effect on mental health. Here, using a randomized clinical trial (Australian New Zealand Clinical Trials Registry, #ACTRN12619000972123), we aimed to investigate the effects of academic stress and daily walnut consumption in university students on mental health, biochemical markers of general health, and the gut microbiota. We found academic stress had a negative impact on self-reported mood and mental health status, while daily walnut consumption improved mental health indicators and protected against some of the negative effects of academic stress on metabolic and stress biomarkers. Academic stress was associated with lower gut microbial diversity in females, which was improved by walnut consumption. The effects of academic stress or walnut consumption in male participants could not be established due to small numbers of participants. Thus, walnut consumption may have a protective effect against some of the negative impacts of academic stress, however sex-dependent mechanisms require further study.