The long-term coevolution of hosts and pathogens in their environment forms a complex web of multi-scale interactions. Understanding how environmental heterogeneity affects the structure of host–pathogen networks is a prerequisite for predicting disease dynamics and emergence. Although nestedness is common in ecological networks, and theory suggests that nested ecosystems are less prone to dynamic instability, why nestedness varies in time and space is not fully understood. Many studies have been limited by a focus on single habitats and the absence of a link between spatial variation and structural heterogeneity such as nestedness and modularity. Here we propose a neutral model for the evolution of host–pathogen networks in multiple habitats. In contrast to previous studies, our study proposes that local modularity can coexist with global nestedness, and shows that real ecosystems are found in a continuum between nested-modular and nested networks driven by intraspecific competition. Nestedness depends on neutral mechanisms of community assembly, whereas modularity is contingent on local adaptation and competition. The structural pattern may change spatially and temporally but remains stable over evolutionary timescales. We validate our theoretical predictions with a longitudinal study of plant–virus interactions in a heterogeneous agricultural landscape.