Encounters with pathogens provoke changes in gene transcription that are an integral part of host innate immune responses. In recent years, studies with invertebrate model organisms have given insights into the origin, function, and evolution of innate immunity. Here, we use genome-wide transcriptome analysis to characterize the consequence of natural fungal infection in Caenorhabditis elegans. We identify several families of genes encoding putative antimicrobial peptides (AMPs) and proteins that are transcriptionally up-regulated upon infection. Many are located in small genomic clusters. We focus on the nlp-29 cluster of six AMP genes and show that it enhances pathogen resistance in vivo. The same cluster has a different structure in two other Caenorhabditis species. A phylogenetic analysis indicates that the evolutionary diversification of this cluster, especially in cases of intra-genomic gene duplications, is driven by natural selection. We further show that upon osmotic stress, two genes of the nlp-29 cluster are strongly induced. In contrast to fungus-induced nlp expression, this response is independent of the p38 MAP kinase cascade. At the same time, both involve the epidermal GATA factor ELT-3. Our results suggest that selective pressure from pathogens influences intra-genomic diversification of AMPs and reveal an unexpected complexity in AMP regulation as part of the invertebrate innate immune response.