Some of America’s closest friends in Africa have turned with a vengeance on gay people. In Nigeria, President Goodluck Jonathan recently approved a law making homosexual acts punishable by up to a 14-year jail sentence and outlawing gay organizations. In Uganda, President Yoweri Museveni signed legislation that requires a life sentence for serial offenders and mandates that witnesses report homosexual acts or face penalties themselves.

Early accounts of the implementation of these laws are disturbing, as expected — and as intended. Arrests in Nigeria have given social permission for police extortion and for vigilante violence against gay people and health workers who serve them. In Uganda, government attitudes are amplified in the tabloids. A recent headline is typical: “EXPOSED! Uganda’s 200 Top Homos Named.” Both countries are producing gay refugees.

The proximate cause of these crackdowns is political. Both Jonathan and Museveni, facing a variety of electoral and economic challenges, are picking populist fights with Western colonialism. After signing Uganda’s most recent anti-gay law, Museveni said, “There’s now an attempt at social imperialism, to impose social values. We’re sorry to see that you [the West] live the way you live, but we keep quiet about it.” This defiance plays well in African electorates (as dictators such as Zimbabwe’s Robert Mugabe have exploited for decades). And the West’s outraged reaction plays into the strategy, at least in the short term.

Anti-gay laws in Africa (and elsewhere) are often remnants of old colonial statutes, but they also reflect a broad cultural consensus. Homosexuality is illegal in 38 of 54 African countries. Religious leaders, both Muslim and Christian, have a history of encouraging intolerance. Africa’s growing evangelical churches — rather than being distinguished by principled beliefs in human dignity and moral persuasion — have supported criminalization. And a few U.S. religious figures have incited malicious prejudices rather than confronting them.

This problem has many political, cultural and religious layers. One matter of science, however, is clear: Anti-gay laws are bad for public health.

The context here is HIV/AIDS. Over the past decade, serious progress has been made in places such as Uganda on promoting testing, reducing transmission and providing treatment among most groups. But these advances have slowed or stalled as the fight against AIDS has involved more marginalized, difficult-to-reach populations, including men who have sex with men (MSM).

Because MSM are stigmatized, they often go unstudied. But the problem in Africa, as elsewhere, is unprotected anal intercourse with a high number of partners. African MSM are nearly four times more likely to be HIV-positive than men who don’t have sex with men. And typical categories of “gay” or “bisexual” don’t fully capture the diversity of these sexual practices. Many MSM in Africa identify themselves as heterosexual. In one Ugandan study, about 75 percent of MSM have sex with women as well — making their female partners more vulnerable to disease.

What influence does criminalization and persecution have on this group? Senegal provides a test case. After nine male HIV-prevention workers, arrested in 2008, were convicted of “acts against nature” in and sentenced to eight years in prison, researchers studied the result. Heath providers reported a sharp decline in medical visits by MSM. Some, fearing assault, left the country or discontinued their medical treatment. A number of service providers, concerned for their own safety, stopped doing targeted outreach to MSM.

It is the job of public health officials to account for the reality of human behavior in pursuit of the public good. Anti-gay laws complicate that task in practical ways. MSM who are afraid of prosecution and violence are less likely to attend meetings where they are given education, condoms and lubricants. Less likely to be honest with their physicians about their sexual histories. Less likely to be tested for AIDS and receive treatment and care. And more likely to inadvertently infect others.

When Western governments lecture African countries about their retrograde views, it can feed a populist, anti-colonial backlash. When donors threaten to cut off aid, it can cause lesbian, gay, bisexual and transgender activists to cringe — fearing they will be scapegoated for the punishment of their whole country.

What might be more effective is a forceful health-related message. This is an area in which civil rights — starting out with a simple zone of personal privacy — is a requirement of public health. Nations such as Nigeria and Uganda are committed to ambitious objectives in fighting AIDS. Those goals are unachievable while any group is targeted for discrimination and excluded from effective outreach.

Read more from Michael Gerson’s archive, follow him on Twitter or subscribe to his updates on Facebook .