Biostimulation by supplementing of nitrogen and phosphorus nutrients is a common strategy for remediation of petroleum-polluted soils. However, the dosage influence of exogenous nitrogen or phosphorus on petroleum hydrocarbon removal and soil ecotoxicity and microbial function remain unclear. In this study, we compared the efficiencies of hydrocarbon degradation and ecotoxicity control by experiment conducted over addition of inorganic nitrogen or phosphorus at C/N ratio of 100/10, C/N/P ratio of 100/10/1, and C/P ratio of 100/1 in a heavily petroleum-contaminated loessal soil with 12,320 mg/kg of total petroleum hydrocarbon (TPH) content. A 90-day incubation study revealed that low-dose of phosphorus addition with the C/P ratio of 100/1 promoted hydrocarbon degradation and reduced soil ecotoxicity. Microbial community composition analysis suggested that phosphorus addition enriched hydrocarbon degrader Gordonia and Mycolicibacterium genus. The key enzymes EC 5.3.3.8, EC 6.2.1.20 and EC 6.4.1.1 which referred to degradation of long-chain hydrocarbons, unsaturated fatty acids and pyruvate metabolism were abundance by phosphorus supplementation. While nitrogen addition at C/N ratio of 100/10 or C/N/P ratio of 100/10/1 inhibited hydrocarbon degradation and exacerbated soil ecotoxicity due to promoting denitrification and coupling reactions with hydrocarbons. Our results suggested that low-dose phosphorus addition served as a favorable strategy to promote crude oil remediation and ecotoxicity risk control in heavily petroleum-contaminated soil. Hence, the application of suitable doses of exogenous biostimulants is an efficient approach to restore the ecological functions of organically contaminated soils.