It was August 2018, and Oswaldo Alanís sat stunned in the middle of Madrid, his career suddenly in shambles.

The Mexican defender had just been cut by Spanish club Getafe scarcely one month after signing a two-year contract, seemingly aborting his lifelong ambition of playing in Europe. Two months earlier, Alanís had been one of the last players left off Mexico’s national-team roster before the 2018 World Cup, the apparent end of another dream.

Uncertainty consumed Alanís. He didn’t know whether to go back to Mexico or stay in Spain. The stress was so bad that he developed a minor skin condition on his face and neck.

“It was a hard time,” he said.

Alanís headed north to play one season at Real Oviedo in Spain’s second division, then returned to his former club, Chivas Guadalajara, for a six-month stint. Once there, he started 10 games and scored two goals, but he did not make an appearance in the club’s three games to begin its 2020 season.

Then came a one-year loan transfer to San Jose, where Alanís needed only one game to attain something he desperately needed: a rebirth.

On the final play of last Saturday’s MLS season opener against Toronto FC, with the Earthquakes trailing 2-1, Alanís stood over a free kick 5 yards outside the penalty area. He had played admirably for 95 minutes at center back, but few expected the defender to produce any last-gasp success on the offensive end.

His free kick curled over Toronto defenders’ heads and dipped into the net. According to research geophysicist Brad Aagaard, the resulting bedlam from the sold-out crowd — measured by a seismograph installed adjacent to Earthquakes Stadium — registered the second-highest “seismic moment” since the team began playing there in 2015.

“It was emotional because it was in the last minute and also we were playing good, and the result was not what we were wanting,” said Alanís, who turns 31 on March 18. “So scoring in the last minute was a great feeling of the stadium, of all the teammates, of all the staff and all of San Jose.”

Of the Mexican players making their MLS debuts last Saturday, many figured Javier “Chicharito” Hernández would be the one setting off goal-scoring shock waves. The former Manchester United striker marked MLS’ most celebrated offseason signing when he joined the LA Galaxy in January, but his statistics in the Galaxy’s 1-1 draw against the Houston Dynamo were meager — nine passes completed, one shot and 32 touches, fewer than any of the game’s other 21 starters.

All Alanís did was post an MLS goal-of-the week nominee, rescue his new team from defeat and endear himself to fans hungry for a playoff push.

“I think he played 95 minutes with a certain personality and enthusiasm that showed why we brought him here,” second-year head coach Matías Almeyda said after the game. “I think the beginning for any player at any club, like the one that he had today, was one that players dream about.”

It wasn’t the first time Almeyda witnessed a free-kick golazo from the left-footed defender. Alanís played for Almeyda at Chivas from 2015 to 2018, and his 30-yard rocket against the Seattle Sounders helped propel Chivas toward the 2018 CONCACAF Champions League title.

Alanís learned the art of the set piece from his father, Gerardo Alanís, who was a left-footed striker for Atlético Monarcas Morelia in southern Mexico. Oswaldo Alanís developed as a central midfielder with uncanny passing ability, but at age 17, his coach, Darío Franco — like Almeyda, an Argentinian with a decorated playing career — offered him some career-altering advice.

“He said, ‘If you want to earn money, you should become a defender,’” Oswaldo Alanís said.

That might seem counterintuitive, given pro soccer’s premium on flashy attacking talents, but Franco recognized the relative rarity of tall, left-footed center backs with a keen eye for passing lanes. Alanís struggled mightily during the first year-and-a-half of the transition, but eventually he found a groove and began to thrive in Mexico’s first division.

It all culminated in the summer of 2018, when Alanís signed with Getafe and set sail for Spain. After settling in, he trained for two weeks with the team and played well. Only one problem: His coach, José Bordalás, did not want him there.

Apparently Getafe ownership had signed Alanís without conferring with the coaching staff, and Bordalás had no use for another defender.

“One of my goals of being in Europe was gone, and two months before that I didn’t get to the final list of the World Cup. Both those things were gone in two months,” Alanís said. “I wondered if I’m not good enough.”

The quaint town of Oviedo, combined with reduced pressure in Spain’s second division, calmed Alanís’ nerves. He sharpened his game with regular playing time, and in doing so, he experienced a revelation of sorts: Soccer pairs best with enjoyment.

“If you play on Chelsea and you are unhappy, it’s better to play on Chacarita and be happy,” he said. “I wanted to play on a big team, but I started thinking that soccer is enjoying.”

That’s a sentiment regularly espoused by Almeyda, who didn’t hold back in expressing his happiness at the goal that anchored a wayward player to his new home.

“I believe in divine justice,” Almeyda said. “It makes me happy for him, it makes me happy for his family and it makes me happy because he’s a humble guy and a great player. Hopefully, he continues on this path.”

Nick Eilerson is a San Francisco Chronicle staff writer. Email: nick.eilerson@sfchronicle.com