Nasri has been training with the Premier League side since early November and has now put pen to paper.
Samir Nasri has signed an initial six-month deal with West Ham.
The former Arsenal and Manchester City midfielder has been training with the Premier League club since early November.
Nasri has now served his 18-month doping ban and has proven his fitness to Hammers boss Manuel Pellegrini.
The 31-year-old worked with the Chilean at City and said: “You choose a club and a project because of the ambition, and I think that West Ham has everything to be one of the biggest clubs in London.
We play in the Olympic Stadium, the training ground is there and you have owners who wants to invest and have a good team. Everything is there to be doing great things.
“Coming back to the most competitive league in the world, with a team full of ambition with a manager that I know; it’s the best job in the world, and I’m really excited about it.”
Nasri was most recently with Turkish side Antalyaspor for whom he made eight appearances during a five-month stay.
He was then slapped with a doping ban after he received an intravenous drip in 2016.
Pellegrini added: “I am very pleased to welcome Samir to West Ham United. He is a player I obviously know well from our time together at Manchester City and I am happy to be working with him again.