All metazoan guts are subjected to immunologically unique conditions in which an efficient antimicrobial system operates to eliminate pathogens while tolerating symbiotic commensal microbiota. However, the molecular mechanisms controlling this process are only partially understood. Here, we show that bacterial-derived uracil acts as a ligand for dual oxidase (DUOX)-dependent reactive oxygen species generation in Drosophila gut and that the uracil production in bacteria causes inflammation in the gut. The acute and controlled uracil-induced immune response is required for efficient elimination of bacteria, intestinal cell repair, and host survival during infection of nonresident species. Among resident gut microbiota, uracil production is absent in symbionts, allowing harmonious colonization without DUOX activation, whereas uracil release from opportunistic pathobionts provokes chronic inflammation. These results reveal that bacteria with distinct abilities to activate uracil-induced gut inflammation, in terms of intensity and duration, act as critical factors that determine homeostasis or pathogenesis in gut-microbe interactions.