In vertebrates, variability at genes of the Major Histocompatibility Complex (MHC) represents an important adaptation for pathogen resistance, whereby high allelic diversity confers resistance to a greater number of pathogens. Pathogens can maintain diversifying selection pressure on their host's immune system that can vary in intensity based on pathogen richness, pathogen virulence, and length of the cohabitation period, which tend to increase with temperature. In this study, we tested the hypothesis that genetic diversity of MHC increases with temperature along a latitudinal gradient in response to pathogen selective pressure in the wild. A total of 1549 Atlantic salmon from 34 rivers were sampled between 46°N and 58°N in Eastern Canada. The results supported our working hypothesis. In contrast to the overall pattern observed at microsatellites, MHC class II allelic diversity increased with temperature, thus creating a latitudinal gradient. The observed temperature gradient was more pronounced for MHC amino acids of the peptide-binding region (PBR), a region that specifically binds to pathogens, than for the non-PBR. For the subset of rivers analyzed for bacterial diversity, MHC amino acid diversity of the PBR also increased significantly with bacterial diversity in each river. A comparison of the relative influence of temperature and bacterial diversity revealed that the latter could have a predominant role on MHC PBR variability. However, temperature was also identified as an important selective agent maintaining MHC diversity in the wild. Based on the bacteria results and given the putative role of temperature in shaping large-scale patterns of pathogen diversity and virulence, bacterial diversity is a plausible selection mechanism explaining the observed association between temperature and MHC variability. Therefore, we propose that genetic diversity at MHC class II represents local adaptation to cope with pathogen diversity in rivers associated with different thermal regimes. This study illuminates the link between selection pressure from the environment, host immune adaptation, and the large-scale genetic population structure for a nonmodel vertebrate in the wild.