Pathogen evolution is influenced strongly by the host immune response. Previous studies of the effects of herd immunity on the population structure of directly transmitted, short-lived pathogens have primarily focused on the impact of competition for hosts. In contrast, for long-lived infections like HIV, theoretical work has focused on the mechanisms promoting antigenic variation within the host. In reality, successful transmission requires that pathogens balance both within- and between-host immune selection. The Opa adhesins in the bacterial Neisseria genus provide a unique system to study the evolution of the same antigens across two major pathogens: while N. meningitidis is an airborne, respiratory pathogen colonising the nasopharynx relatively transiently, N. gonorrhoeae can cause sexually transmitted, long-lived infections. We use a simple mathematical model and genomic data to show that trade-offs between immune selection pressures within- and between-hosts can explain the contrasting Opa repertoires observed in meningococci and gonococci.