Previous metabolomics studies have highlighted the predictive value of metabolites on upper gastrointestinal (UGI) cancer, while most of them ignored the potential effects of lifestyle and genetic risk on plasma metabolites. This study aimed to evaluate the role of lifestyle and genetic risk in the metabolic mechanism of UGI cancer. Differential metabolites of UGI cancer were identified using partial least-squares discriminant analysis and the Wilcoxon test. Then, we calculated the healthy lifestyle index (HLI) score and polygenic risk score (PRS) and divided them into three groups, respectively. A total of 15 metabolites were identified as UGI-cancer-related differential metabolites. The metabolite model (AUC = 0.699) exhibited superior discrimination ability compared to those of the HLI model (AUC = 0.615) and the PRS model (AUC = 0.593). Moreover, subgroup analysis revealed that the metabolite model showed higher discrimination ability for individuals with unhealthy lifestyles compared to that with healthy individuals (AUC = 0.783 vs 0.684). Furthermore, in the genetic risk subgroup analysis, individuals with a genetic predisposition to UGI cancer exhibited the best discriminative performance in the metabolite model (AUC = 0.770). These findings demonstrated the clinical significance of metabolic biomarkers in UGI cancer discrimination, especially in individuals with unhealthy lifestyles and a high genetic risk.