In order to avoid both starvation and disease, animals must allocate resources between energy reserves and immune defence. We investigate the optimal allocation. We find that animals with low reserves choose to allocate less to defence than animals with higher reserves because when reserves are low it is more important to increase reserves to reduce the risk of starvation in the future. In general, investment in immune defence increases monotonically with energy reserves. An exception is when the animal can reduce its probability of death from disease by reducing its foraging rate. In this case, allocation to immune defence can peak at intermediate reserves. When food changes over time, the optimal response depends on the frequency of changes. If the environment is relatively stable, animals forage most intensively when the food is scarce and invest more in immune defence when the food is abundant than when it is scarce. If the environment changes quickly, animals forage at low intensity when the food is scarce, but at high intensity when the food is abundant. As the rate of environmental change increases, immune defence becomes less dependent on food availability. We show that the strength of selection on reserve-dependent immune defence depends on how foraging intensity and immune defence determine the probability of death from disease.