Neonates face unique challenges in the period following birth. The postnatal immune system is in the early stages of development and has a range of functional capabilities that are distinct from the mature adult immune system. Bidirectional immune-microbial interactions regulate the development of mucosal immunity and alter the composition of the microbiota, which contributes to overall host well-being. In the past few years, nutrition has been highlighted as a third element in this interaction that governs host health by modulating microbial composition and the function of the immune system. Dietary changes and imbalances can disturb the immune-microbiota homeostasis, which might alter susceptibility to several autoimmune and metabolic diseases. Major changes in cultural traditions, socioeconomic status and agriculture are affecting the nutritional status of humans worldwide, which is altering core intestinal microbial communities. This phenomenon is especially relevant to the neonatal and paediatric populations, in which the microbiota and immune system are extremely sensitive to dietary influences. In this Review, we discuss the current state of knowledge regarding early-life nutrition, its effects on the microbiota and the consequences of diet-induced perturbation of the structure of the microbial community on mucosal immunity and disease susceptibility.