Sex pheromones are widely used by insects as a reproductive isolating mechanism to attract conspecifics and repel heterospecifics. Although researchers have obtained extensive knowledge about sex pheromones, little is known about the differentiation mechanism of sex pheromones in closely related species. Using Bactrocera dorsalis and Bactrocera cucurbitae as the study model, we investigated how the male-borne sex pheromones are different. The results demonstrated that both 2,3,5-trimethylpyrazine (TMP) and 2,3,5,6-tetramethylpyrazine (TTMP) were sex pheromones produced by rectal Bacillus in the two flies. However, the TMP/TTMP ratios were reversed, indicating sex pheromone specificity in the two flies. Bacterial fermentation results showed that different threonine and glycine levels were responsible for the preference of rectal Bacillus to produce TMP or TTMP. Accordingly, threonine (glycine) levels and the expression of the threonine and glycine coding genes were significantly different between B. dorsalis and B. cucurbitae. In vivo assays confirmed that increased rectal glycine and threonine levels by amino acid feeding could significantly decrease the TMP/TTMP ratios and result in significantly decreased mating abilities in the studied flies. Meanwhile, decreased rectal glycine and threonine levels due to RNAi of the glycine and threonine coding genes was found to significantly increase the TMP/TTMP ratios and result in significantly decreased mating abilities. The study contributes to the new insight that insects and their symbionts can jointly regulate sex pheromone specificity in insects, and in turn, this helps us to better understand how the evolution of chemical communication affects speciation.