After years of setbacks, a nearly $100 billion-a-year compromise farm bill cleared the House on Wednesday despite strong opposition from conservatives who sought a bigger cut in food stamps.

The five-year bill, which preserves generous crop subsidies, heads to the Senate, where approval seems certain. The White House said President Barack Obama would sign it.

The measure, which the House approved 251-166, had backing from the Republican leadership team, even though it makes smaller cuts to food stamps than they would have liked.

House Speaker John Boehner did not cast a vote on the bill, a commonplace practice for a speaker, but he had issued a statement Monday saying it was ‘‘worthy of the House’s support.’’ Minority Leader Nancy Pelosi, D-Calif., voted for the bill despite concerns from some in her caucus that the bill cut too much from the food stamp program.

The legislation would eliminate a $4.5 billion-a-year farm subsidy called direct payments, which are paid to farmers whether they farm or not. But the bill nonetheless would continue to heavily subsidize major crops — corn, soybeans, wheat, rice and cotton — while shifting many of those subsidies toward more politically defensible insurance programs. That means farmers would have to incur losses before they could get a payout.

The almost $100 billion-a-year bill would save around $1.65 billion annually overall, according to the Congressional Budget Office. The amount was less than the $2.3 billion annual savings the agriculture committees originally projected for the bill.

The bill ultimately would cut about $800 million a year from the $80 billion-a-year food stamp program, or around 1 percent. The House had sought a 5 percent cut.

The legislation also would continue to heavily subsidize major crops for the nation’s farmers while eliminating some subsidies and shifting them toward more politically defensible insurance programs.

House Agriculture Chairman Frank Lucas, R-Okla., called the compromise a ‘‘miracle’’ after trying to get the bill passed for almost three years. An early version of the legislation was defeated on the House floor last June after conservatives said the food stamp cuts were too modest and liberal Democrats said they were too deep.

The House later passed a bill with a higher, $4 billion cut, arguing at the time that the program had spiraled out of control after costs doubled in the last five years. But cuts that high were ultimately not possible after the Senate balked and the White House threatened a veto. The Senate had sought a cut of $400 million annually.

Many House conservatives still voted against the bill — 63 Republicans opposed it, one more than in June.

One of those conservative opponents was Rep. Marlin Stutzman, R-Ind. ‘‘It spends money we simply don’t have,’’ he said.