Both malnutrition and undernutrition can lead to compromised immune defense in a diversity of animals, and "nutritional immunology" has been suggested as a means of understanding immunity and determining strategies for fighting infection. The genetic basis for the effects of diet on immunity, however, has been largely unknown. In the present study, we have conducted genome-wide association mapping in Drosophila melanogaster to identify the genetic basis for individual variation in resistance, and for variation in immunological sensitivity to diet (genotype-by-environment interaction, or GxE). D. melanogaster were reared for several generations on either high-glucose or low-glucose diets and then infected with Providencia rettgeri, a natural bacterial pathogen of D. melanogaster. Systemic pathogen load was measured at the peak of infection intensity, and several indicators of nutritional status were taken from uninfected flies reared on each diet. We find that dietary glucose level significantly alters the quality of immune defense, with elevated dietary glucose resulting in higher pathogen loads. The quality of immune defense is genetically variable within the sampled population, and we find genetic variation for immunological sensitivity to dietary glucose (genotype-by-diet interaction). Immune defense was genetically correlated with indicators of metabolic status in flies reared on the high-glucose diet, and we identified multiple genes that explain variation in immune defense, including several that have not been previously implicated in immune response but which are confirmed to alter pathogen load after RNAi knockdown. Our findings emphasize the importance of dietary composition to immune defense and reveal genes outside the conventional "immune system" that can be important in determining susceptibility to infection. Functional variation in these genes is segregating in a natural population, providing the substrate for evolutionary response to pathogen pressure in the context of nutritional environment.