The Sages enacted a provision forbidding taking medication on Shabbat. Although there are numerous exceptions to the rule, in general, one may not take medication on Shabbat. As the Rishonim explain, this prohibition was enacted due to the concern that people might grind herbs to produce medication, which would constitute an act of Shabbat desecration. In ancient times, people produced their own medicine by grinding various different herbs, and thus the Sages were concerned that in the frantic attempt to cure one’s ailment, he might mistakenly desecrate Shabbat by grinding herbs in order to produce medicine.
Rav Haim Naeh (Israel, 1890-1954) compellingly argued that this enactment should not be applicable nowadays, when nobody grinds herbs to produce medication. We purchase our medication all prepared, without any need to grind herbs, and we would not even know how to make our own medication. Clearly, there is no concern at all that anybody might desecrate Shabbat by grinding herbs in order to produce medicine, and thus, according to Rav Haim Naeh, this entire prohibition is not practically applicable in today’s day and age.
More recent Halachic authorities, including Hacham Ovadia Yosef and the contemporary Posek Rabbi Shlomo Miller, accepted this argument, but asserted that it may be applied only in conjunction with other factors. Meaning, in a case where there are already considerations to allow taking medicine, the argument advanced by Rav Haim Naeh may be introduced as an additional factor that would lead us to a lenient ruling.
One example is the issue of taking pain relievers on Shabbat, such as aspirin or Tylenol. Hacham Ovadia Yosef (in Yalkut Yosef) rules that one may take pain relievers on Shabbat, due to the combination of several factors. First, as mentioned, it could be argued that the entire prohibition against medication on Shabbat does not apply nowadays. Secondly, many people take aspirin or similar pills every day, even when they do not experience pain, and thus aspirin can be considered "Ma’achal Beri’im" – food ingested even by healthy people, which is permissible on Shabbat. What’s more, the Mishna Berura (Rav Yisrael Meir Kagan of Radin, 1839-1933), citing the Radbaz (Rav David Ben Zimra, Egypt, 1479-1573), writes that Halacha treats the prohibition against taking medication on Shabbat more leniently than it treats the other Shabbat prohibitions, as evidenced by the numerous exceptions that apply (such as the provision allowing bedridden patients to take medication). Therefore, there is certainly greater room for leniency. Additionally, it could be argued that pain relievers are not truly "medication," because they do not actually cure the ailment, and merely provide temporary comfort. For this reason, Hacham Ovadia permits using on Shabbat even anesthetic sprays that numb aching parts of the body to provide temporary relief. Such remedies do not actually qualify as "medications" with respect to this Halacha, and are therefore permitted for use on Shabbat.
Although other Poskim – including Rav Miller, and Rav Moshe Ha’levi (Israel, 1961-2000) – ruled stringently in this regard, and forbade the use of painkillers on Shabbat, one can certainly rely on the ruling of Hacham Ovadia and take painkillers, particularly if he experiences considerable discomfort.
It should be noted that Rav Shlomo Zalman Auerbach (Israel, 1910-1995) distinguished in this regard between different situations. Halacha allows bedridden patients to take medicine on Shabbat to alleviate their symptoms, and thus those who are bedridden due to pain would be allowed to take painkillers. Normally, a person experiencing pain does not lie down in bed to deal with the pain, because lying down will not alleviate the discomfort. In some cases, however, people suffering from an intense headache would indeed lie down to alleviate the pain. In the case of a severe headache, therefore, it would be permissible to take a pain reliever. This would certainly apply to migraine headaches, but also to any severe headache which a person would want to alleviate by lying down.
In any event, as mentioned above, Hacham Ovadia allows taking painkillers on Shabbat in all circumstances.
Summary: Different views exist among the Poskim as to the permissibility of taking painkillers on Shabbat. It would certainly be permissible for somebody suffering from a strong headache, which he would want to relieve by lying down, to take a pain reliever, and according to Hacham Ovadia Yosef, painkillers are always permitted on Shabbat.