Men who ate the most vegetables had a 50% lower risk of having prostate cancer than those who ate the least, it was found.
The benefits could be due to allium, a sulphur-based compound, which is responsible for the characteristic smell.
Diet benefits
Men's diets were examined by researchers from the US National Cancer Institute.
Two hundred and thirty-eight men with prostate cancer and 471 men without were asked about what they ate.
It was found men who ate more than a third of an ounce (10 grammes) a day of onions, garlic, chives or scallions were much less likely to be in the group with cancer.
The researchers, led by Ann Hsing, said their findings provided additional evidence that a good diet could reduce the risk of cancer.
They said in a statement: "Several case-control studies, in which the diets of cancer patients are compared to the diets of healthy individuals, have linked allium vegetables to lower risk for cancer of the stomach, colon, oesophagus, breast and lining of the uterus."
Jamie Bearse of the US Prostate Cancer Coalition, said: "It's great to see that more favourable foods are proving to be preventatives for prostate cancer.
"Maybe it will encourage men to put down that Big Mac and pick up a salad with chives and onions."
Questions
Dr Chris Hiley of the UK's Prostate Cancer Charity said men should not be eating a lot of onions on the basis of this study.
"The researchers weren't trying to discover if all men should be advised to eat lots of onions, and they didn't.
"They found out that some men in Shanghai who ate more onions had less prostate cancer.
"We can't draw cause and effect conclusions from this, because of the way the study was conducted.
"It's an interesting study, which generates more questions, rather than giving any answers of use to the man in the street today."
The research is published in the Journal of the National Cancer Institute.