Headaches that occur during or after a workout can put a damper on your exercise routine–here's how to get rid of them.

Let's set the scene: You’re at the gym in the middle of an intense workout—or you just finished one—and all of a sudden you get a major headache out of nowhere. The fighter in you says you should ignore it and keep powering through. But the pain is stubborn and doesn't relent. What's going on?

This kind of head pain is known as a primary exertion headache, or a primary exercise headache, and is a type of headache triggered by—you guessed it—exercise. The "primary" term in these exertion or exercise headaches stems from the headaches not being caused by another condition or disorder, according to the American Migraine Foundation (AMF). Though exertion headaches aren't as well known as migraines or stress headaches, they can be just as painful, lasting from five minutes to 48 hours, and are often described as a bilateral (on both sides of the head), pulsating pain.

Exertion headaches tend to happen when you're sweating your hardest; they're caused by increased pressure on the blood vessels in the brain. Exercises like biking, running, or weightlifting, are typically the more strenuous types of workouts than can trigger exertion headaches, according to the AMF.

If headaches after exercise (or during exercise) are new to you, it's best to get checked out by a doctor who can rule out any underlying health issues, like heart disease or a condition that can block the flow of the spinal fluid. But if you regularly experience headaches triggered from exercise and are otherwise healthy, here's what doctors say you should do if you develop one of these skull-throbbers, and how to keep them from coming back.

Stop and cool down.

"Exercising in hot, humid conditions or at high altitudes when your body isn’t acclimated yet,” can bring on exertion headaches, Clifford Stark, DO, sports medicine specialist in New York City, tells Health. So, an exertion headache is your body's way of telling you it's being overexerted, and if one strikes, it's time to take a break.

If the headache goes away, you can try getting back into your workout—but warm up first. Warming up before any type of exercise, strenuous or not, gradually increases your heart rate and gets your blood flowing to prepare your body for activity, which can also ward off exertion headaches.

Find your triggers, and try to avoid them.

Working too hard is the main cause of exertion headaches. But like migraines, this type of headache also has triggers, . Dehydration, sleep deprivation, blood pressure issues and your food and drink choices (such as chocolate, alcohol, caffeine) could all trigger an exertion headache after or during a workout.

Your best defense in this situation? Keeping well-hydrated, eating regularly, and getting enough sleep, to see if those changes keep the after-exercise head pain at bay, Erin Manning, MD, assistant attending neurologist at the Hospital for Special Surgery in New York, tells Health.

Correct your workout form. 

Exertion headaches can also be brought on by small mistakes you’re making during your workout, Steven Coppolecchia, physical therapist at Spear Physical Therapy in New York, tells Health. “A lot of times—in particular with lifting but you'll see it with running as well—people sit with the head sitting far too forward or an arched back," says Coppolecchia. "I work with patients to correct their posture and that's going to improve blood flow up to the brain and also reduce some of the muscle tension."

Try over-the-counter pain medication.

“Sometimes over the counter medications like Advil, naproxen, or Tylenol can be helpful for people,” says Dr. Manning. The AMF suggests taking naproxen 30 to 60 minutes before you plan to hit the gym. But it might be a good idea to fill your doctor in on this, since even over-the-counter pain meds can have side effects. It's also wise to reevaluate your situation and treatment approach after six months of taking OTC medication, to see if it's still necessary.

Dr. Manning adds that if OTC pills don't help or aren't an option, "sometimes people actually need [prescription] medication that they take either right before strenuous exercise or regular medication that they take on a daily basis to help prevent these from happening.”

Check in with your doctor.

“Anytime you have a severe headache that feels like the worst headache of your life, or something that came on very suddenly, or it causes other symptoms beyond the headache that you’re not used to like visual symptoms, you need medical attention right away because it could be something serious,” says Dr. Stark. If the head pain isn't quite that bad but it hits you frequently and eliminating triggers doesn't help, see a physician.

“I would say if this has happened more than a couple of times, then it's probably a good time to see a doctor," advises Dr. Manning. You can start with your primary care doctor, or see if you can find a neurologist, headache specialist, or someone who specializes in sports neurology."

To get our top stories delivered to your inbox, sign up for the Healthy Living newsletter