Ecosystem services (ESs) associated with the water-energy-food (WEF) nexus influence the ecological security of urban agglomerations. However, how to apply the assessment of ESs relevant to the WEF nexus into the identification and optimization of ecological security patterns still remains unclear. This study assessed the development level of the WEF nexus based on a constructed evaluation index system and the evaluation model combined with game theory and the Technique for Order Preference by Similarity to an Ideal Solution (TOPSIS) method. The relevant ESs were selected based on the analysis of stakeholders related to the WEF nexus, and further quantified by the models. On this basis, the relationship between the WEF nexus and ESs was evaluated by Spearman rank correlation analysis. Then this study identified ecological security patterns by following the basic paradigm of “Source identification-Resistance surface establishment-Corridor extraction”. Lastly, this study optimized ecological security patterns by considering the WEF nexus. Thus, a methodological framework of identifying and optimizing ecological security patterns from the perspective of ESs related to the WEF nexus was designed, which was applied to the case of the Yangtze River Delta Urban Agglomeration (YRDUA), China. The results revealed that the WEF nexus was significantly negatively correlated with ESs. The sources of water yield were in the southwest and central areas of the YRDUA, and those of carbon storage and food production were respectively in the forested and farming areas. The ecological resistance surface displayed a slow declining trend from east to west, and ecological corridors showed a zigzag distribution from north to south. The total length of the ecological corridors was 20,891.45 km. A scheme of “two belts, three zones and four groups” was proposed to optimize the ecological security pattern of the YRDUA. Policy recommendations were therefore put forward in this study to promote the ecological security of urban agglomerations from different perspectives.