Prejudiced attitudes and political nationalism vary widely around the world, but there has been little research on what predicts this variation. Here we examine the ecological and cultural factors underlying the worldwide distribution of prejudice. We suggest that cultures grow more prejudiced when they tighten cultural norms in response to destabilizing ecological threats. A set of seven archival analyses, surveys, and experiments (∑N = 3,986,402) find that nations, American states, and pre-industrial societies with tighter cultural norms show the most prejudice based on skin color, religion, nationality, and sexuality, and that tightness predicts why prejudice is often highest in areas of the world with histories of ecological threat. People's support for cultural tightness also mediates the link between perceived ecological threat and intentions to vote for nationalist politicians. Results replicate when controlling for economic development, inequality, conservatism, residential mobility, and shared cultural heritage. These findings offer a cultural evolutionary perspective on prejudice, with implications for immigration, intercultural conflict, and radicalization.