Match ends, Gillingham 0, West Ham United 2.
Gillingham 0-2 West Ham United, FA Cup third round
Premier League West Ham overcame a spirited performance from League One side Gillingham to advance to the fourth round of the FA Cup.
Gillingham's Brandon Hanlan's fierce effort was well saved at close range by Hammers goalkeeper Lukasz Fabianski.
But West Ham improved, with Sebastien Haller hitting the inside of the post after wasting an earlier one-on-one.
Pablo Zabaleta scored with a low strike 16 minutes from the end before Pablo Fornals added a second in injury-time.
West Ham will now be in Monday's draw for the fourth round. You can watch the draw on BBC One and the BBC Sport website from 19:35 GMT before the Arsenal versus Leeds United match.
- Live: Follow Sunday's FA Cup third-round action
- Watch all of the latest FA Cup highlights
- Where will the FA Cup third-round shocks be? Ladhood star Liam Williams takes on Lawro
- How to follow FA Cup third round on the BBC
West Ham get through tricky task
Several Premier League teams this weekend have made a large number of changes in order to rotate their squads and rest key players.
However, Hammers boss David Moyes, in only his second game in charge since returning to the club, only made three changes as he looked to overcome a Gillingham side sitting 12th in League One.
That did not stop the hosts, playing at a packed Priestfield Stadium, from performing well against a team ranked 40 places above them in the English football pyramid; the Gills were a constant threat from corners, long throws and free-kicks.
West Ham did not have a single shot on target in the opening 45 minutes and a bad half got even worse for them when defender Ryan Fredericks had to go off because of a hamstring injury, replaced by Zabaleta, an FA Cup winner with Manchester City in 2011.
Within 21 seconds of the restart, Haller should have put his side ahead when he only had goalkeeper Jack Bonham to beat, but the Frenchman made a complete mess of the chance, shooting high and wide.
The striker then hit the inside of the post after a poor backpass from Jack Tucker minutes later.
Bonham did well to hold on to a deflected Manuel Lanzini effort and then claw out a goalbound cross from Felipe Anderson as the visitors pushed for a goal.
They got it in the 74th minute as Arthur Masuaku burst forward down the left and his pull-back found Zabaleta, whose shot went through Bonham's attempt to stop it and into the net.
Gillingham's Stuart O'Keefe had a chance to force a replay but his volley from the edge of the penalty area was well held by Fabianski.
With the hosts pushing men forward, they were then caught out by a counter attack in the 94th minute, as Fornals blasted past Bonham to seal West Ham's victory.
Michael Brown, former Manchester City midfielder on BBC Radio 5 Live
Gillingham deserve a lot of credit against a multi-millionaire Premier League team, but as they didn't score they were always going to be vulnerable.
West Ham could do better but they will pleased to be through. They kept going through the game, showed their energy and eventually that class shone through.
'Great resilience' - what they said
Gillingham manager Steve Evans, speaking to BBC Sport: "I'm pleased with the first half. We were by some distance the dominant side but when you dominate against a quality opponent, you have to have a goal to show for it.
"We said 'more of the same' but you knew there was going to be a reaction. Moyesy [David Moyes] got a reaction and they were better than us in the second half, so no complaints.
"They had a bit more of the ball and he woke them up a bit, if I'm honest. But we were in the game in the last five minutes and nothing fell for us. The boys have given us everything, as they have done for most of the season.
"He [Moyes] has been a friend for 35-40 years and will be a friend until my last day. I didn't want him to get the West Ham job before this game. He is a good man and will do a great job at West Ham and you could see their strength."
West Ham boss David Moyes, speaking to BBC Sport: "We're really thrilled. These games have caused us difficulty over the years and Gillingham nearly did that again - they were really good, really competitive, hard to play against but we had a bit of quality.
"At half-time, I was more disappointed that we had not tried to play enough and not got the ball down enough. The defenders were slashing at clearances and did not show enough composure but it was always going to be this type of game.
"Great credit to the players - they stuck at it and showed great resilience.
"I thought Steve's team did very well and they will think they were a bit unlucky but I thought we could score again on the break."
Zabaleta's rare goal - the stats
- West Ham have won each of their past four meetings with Gillingham in all competitions, keeping a clean sheet in the past two.
- David Moyes has progressed in seven of his past nine FA Cup third-round ties as manager, falling short to Swansea in 2013-14 (with Manchester United) and Burnley in 2016-17 (with Sunderland).
- Gillingham boss Steve Evans has lost all eight of his matches as a manager against Premier League teams.
- No West Ham player has been directly involved in more goals in all competitions this season than Pablo Fornals (three goals, four assists).
- West Ham defender Pablo Zabaleta scored his third FA Cup goal - and first since March 2017, which was for Manchester City against Huddersfield. It was also his first goal as a substitute in English football.
- 12FullerSubstituted forNdjoliat 86'minutes
- 14JonesBooked at 65mins
- 24O'ConnorSubstituted forMarshallat 79'minutes
- 23JakubiakSubstituted forCharles-Cookat 79'minutes
- 24FredericksSubstituted forZabaletaat 43'minutesBooked at 79mins
- 11SnodgrassSubstituted forFornalsat 70'minutes
- 8Felipe AndersonSubstituted forC Sánchezat 90+5'minutes
- 15C Sánchez
- Andy Madley
- Shots on Target
Second Half ends, Gillingham 0, West Ham United 2.
Corner, Gillingham. Conceded by Angelo Ogbonna.
Substitution, West Ham United. Carlos Sánchez replaces Felipe Anderson.
Goal! Gillingham 0, West Ham United 2. Pablo Fornals (West Ham United) left footed shot from the centre of the box to the bottom right corner. Assisted by Felipe Anderson following a fast break.
Attempt missed. Mark Marshall (Gillingham) right footed shot from outside the box misses to the left following a set piece situation.
Foul by Issa Diop (West Ham United).
Brandon Hanlan (Gillingham) wins a free kick on the left wing.
Foul by Sébastien Haller (West Ham United).
Connor Ogilvie (Gillingham) wins a free kick in the defensive half.
Attempt missed. Stuart O'Keefe (Gillingham) right footed shot from outside the box misses to the right.
Corner, Gillingham. Conceded by Arthur Masuaku.
Pablo Zabaleta (West Ham United) wins a free kick in the defensive half.
Foul by Regan Charles-Cook (Gillingham).
Substitution, Gillingham. Mikael Ndjoli replaces Barry Fuller.
Foul by Sébastien Haller (West Ham United).
Max Ehmer (Gillingham) wins a free kick in the defensive half.
Hand ball by Alfie Jones (Gillingham).
Corner, West Ham United. Conceded by Barry Fuller.
Attempt saved. Stuart O'Keefe (Gillingham) left footed shot from outside the box is saved in the bottom right corner.
Corner, Gillingham. Conceded by Fabián Balbuena.
Offside, West Ham United. Arthur Masuaku tries a through ball, but Sébastien Haller is caught offside.
Attempt blocked. Arthur Masuaku (West Ham United) left footed shot from outside the box is blocked. Assisted by Pablo Fornals.
Substitution, Gillingham. Regan Charles-Cook replaces Alex Jakubiak.
Substitution, Gillingham. Mark Marshall replaces Thomas O'Connor.
Pablo Zabaleta (West Ham United) is shown the yellow card.
Pablo Zabaleta (West Ham United) has gone down, but the referee deems it simulation.
Attempt missed. Pablo Fornals (West Ham United) right footed shot from more than 35 yards misses to the left. Assisted by Issa Diop with a headed pass.
Offside, Gillingham. Barry Fuller tries a through ball, but Brandon Hanlan is caught offside.
Goal! Gillingham 0, West Ham United 1. Pablo Zabaleta (West Ham United) right footed shot from the centre of the box to the centre of the goal. Assisted by Pablo Fornals.
Substitution, West Ham United. Pablo Fornals replaces Robert Snodgrass.
Attempt missed. Arthur Masuaku (West Ham United) left footed shot from the left side of the box is high and wide to the left. Assisted by Pablo Zabaleta with a cross.
Sébastien Haller (West Ham United) wins a free kick in the attacking half.
Foul by Jack Tucker (Gillingham).
Alfie Jones (Gillingham) is shown the yellow card for a bad foul.
Manuel Lanzini (West Ham United) wins a free kick on the right wing.
Foul by Alfie Jones (Gillingham).
Foul by Felipe Anderson (West Ham United).
Jack Tucker (Gillingham) wins a free kick in the defensive half.
Manuel Lanzini (West Ham United) wins a free kick in the attacking half.