Much attention is rightly focused on how microbes cause disease, but they can also affect other aspects of host physiology, including behaviour. Indeed, pathogen avoidance behaviours are seen across animal taxa and are probably of major importance in nature. Here, we review what is known about the molecular genetics underlying pathogen avoidance in the nematode Caenorhabditis elegans. In its natural environment, the soil, this animal feeds on microbes and is continuously exposed to a diverse mix of microorganisms. Nematodes that develop efficient behavioural responses that enhance their attraction to sources of food and avoidance of pathogens will have an evolutionary advantage. C. elegans can specifically detect natural products of bacteria, including surfactants (such as serrawettin) and acylated homoserine lactone autoinducers, and it can learn to avoid pathogenic species. To date, several distinct mechanisms have been shown to be involved in pathogen avoidance. They are based on G protein-like, insulin-like and neuronal serotonin signalling. We discuss recent findings on the mechanisms of pathogen recognition in C. elegans, the relationship between alternative behavioural defences and also between these and other life-history traits. We propose that the selective pressure associated with avoidance behaviours influence both pathogen and host evolution.