I have heard that Russia has been fighting with the Chechens on and off for about 300 years, occasionally losing control and reconquering. What exactly does Russia have to gain by retaining control of Chechnya, because I am not sure how could Russia be benefited by such a small nation, that is probably a huge economic burden to keep fighting in.
It's a bit of a long write-up, but the best reason is fairly easy to trace on a map.
The southern border of Russia between Caspian and Black seas is pretty defenseless as far as natural features (same is true for other borders). So historically, Russia worked/fought to extend its borders to defensible ranges, in case of this specific area, the Greater Caucusus Mountains range that extends from around Sochi on Black sea to NorthEast corner of Azerbaijan on Caspian sea.
This is not really an academic exercise, since historically, that region was strongly threatened by fairly expansionist Ottoman Empire and modern day Turkey (see larger Wiki map for good visual). Russia fought a whole bunch of wars with Ottomans/Turks, and given modern geopolitics, is still strongly competing with Turkey, which is on the accendant path in the region/world.
The of course ties into access to Caspian resources, especially oil (notice that if you lose Chechnya, you will possibly lose Dagestan, meaning the whole western Caspian seaboard).
First note that besides Chechnya there are some related and similar peoples in the North Caucasus: Ingushs, Dagestan peoples, Adygh people, Circassians etc. That is Chechnya is only a part of greater North Caucasus community.
The ancient lifestyle of most of these people, and especially, of Chechens was making raids on neighboring settlements, capturing horses, and hostages for a ransom. They had no agriculture and very little husbandry. This was quite intolerable.
But the main reason for capturing this region was, I think, making a tunnel towards Cristian Georgia to help it against Turkey after Georgians appealed to the Russian Tsar for incorporation of their lands into Russian Empire.
Regarding modern times I think the reasons for the first Chechen war were as follows.
Legality. Chechen republic never had constitutional right to secede unlike the Soviet republics (this is similar to Kosovo).
The danger that the disintegration process could spread to other regions. For example, dangerous separatist processes were also observed in Tatarstan in 1990s when they adopted a constitution that claimed priority of Tatarstan laws before federal laws.
The skyrocketing crime rates in "independent" Ichkeria. The most known scandals were fake aviso and bank orders through which they pumped billions from Russian banks. Another was taking people hostages and demanding ransom. Ichkeria became the main hub for criminal economy, including drug trafficking, slave trade (many Chechen families openly held Russian slaves), car theft etc. This all was covered and encouraged by the government of Ichkeria, so factually they did not want and could not be independent. Their criminal economy was heavily dependent on that of Russia.
The non-Chechen population was expelled from Chechnya and their homes and possessions were seized.
The only railroad towards South Dagestan crosses Chechnya. Before the first war the passenger trains were often assaulted when passing Chechnya.
The reasons for the second Chechen war were the same but the following reasons added:
Terrorism. It seems that some fighters adopted the tactic to use terrorism for earning money. They collected money abroad in Jihadist circles and made videos and reports for the sponsors to confirm their work (as you know, one of their leaders, Yandarbiyev was killed by the Russian secret service in Qatar).
Also terrorism was widely used in Russian politics. Surprisingly many and most bloody terrorist acts happened before the Russian elections which hinted at some connection between the terrorists and the opposition politicians such as Berezovsky (who already participated in hostage trade with the Chechens earlier).
But the casus belli for the second war was that the militants from Chechnya assaulted Dagestan in hope to capture it and trigger the creation of Caucasian Islamic emirate that would span the entire North Caucasus.
It should be noted however that it is quite uncertain to which extent the Chechen population supported separatism. On all elections they voted for a candidate that was supported by the Kremlin. The originator of the secession of Chechnya, president Dudayev was initially supported by anti-Communists who seized power in Moscow in the early 1990s. He was a honored Soviet pilot who participated in the Afghan war and was the only Chechen to become a Soviet general. He then quickly proceeded to install personal dictatorship.
After Dudayev was killed, a new president Maskhadov was elected. He was known for signing a peace treaty with Moscow and was supported by the Russian media in hope he would be a moderate leader. This suggests that the Chechens in general did not want a war. It turned out later that Maskhadov either supported the Jihadists and terrorists or was unable to do anything to control the situation, so the accords he signed were broken.
Chechnya contains the oil center known formerly as Grozny. It sits on the road to Derbens, and the rest of Dagestan, as well as Baku in Azerbaijan, which are also major oil producers. The region could become critical in shipping other oil and gas resources from around the Caspian sea, across the Caucasus, to the Black Sea.
Basically, Chechnya is close to Russia's main sources of oil, both for its own use and for export, meaning that a hostile presence there could be economically and strategically threatening.