Background Nut consumption has been associated with reduced cancer-related mortality. However, it is unclear whether nut consumption also reduces the risk of esophageal and gastric cancer subtypes. We prospectively investigated the relationship of tree nut, peanut, and peanut butter intake with risk of esophageal squamous cell carcinoma (ESCC), esophageal adenocarcinoma (EAC), gastric cardia adenocarcinoma (GCA), and gastric non-cardia adenocarcinoma (GNCA) in the Netherlands Cohort Study. Methods In 1986, 120,852 males and females, aged 55-69 years, completed a baseline questionnaire on diet and cancer risk factors. After 20.3 years of follow-up, 133 ESCC, 200 EAC, 191 GCA, and 586 GNCA cases, and 3,720 subcohort members were available for multivariable Cox regression analyses, using a case-cohort approach. Results Increased total nut consumption was significantly associated with a decreased risk of ESCC and GNCA [HRs (95% CIs) for 10 + g/day vs. nonconsumers = 0.54 (0.30-0.96) and 0.73 (0.55-0.97), respectively], but not with EAC and GCA risk. Similar trends were observed for tree nut and peanut intake, which were mostly nonsignificant. For peanut butter intake, no significant associations were found. When excluding the first four years of follow-up to reduce the possible influence of reversed causation, the relation between nut consumption and ESCC risk attenuated, but remained inverse. Conclusions Our findings suggest that increased tree nut and peanut consumption is inversely associated with GNCA risk and possibly with ESCC risk, but not with the risk of the other esophageal and gastric cancer subtypes.