Nutrition is a central factor influencing immunity and resistance to infection, but the extent to which nutrition during development affects adult responses to infections is poorly understood. Our study investigated how the nutritional composition of the larval diet affects the survival, pathogen load, and food intake of adult fruit flies, Bactrocera tryoni, after bacterial septic infection. We found a sex-specific effect of larval diet composition on survival post-infection: survival rate was higher and bacterial load was lower for infected females fed sugar-rich larval diet compared with females fed protein-rich larval diet, an effect that was absent in males. Both males and females were heavier when fed a balanced larval diet compared to protein- or sugar-rich diet, while body lipid reserves were higher in the sugar-rich larval diet compared with other diets. Body protein reserve was lower for sugar-rich larval diets compared to other diets in males, but not females. Both females and males shifted their nutrient intake to ingest a sugar-rich diet when infected compared with sham-infected flies without any effect of the larval diet, suggesting that sugar-rich diets can be beneficial to fight off bacterial infection. Overall, our findings show that nutrition during early life can shape individual fitness in adulthood.