The Messenger of Allah ﷺ said, "When you are praying, do not spit in front of you or to your right. Spit behind you, or toward your left if that side is empty. Otherwise, do like this." Then he spat beneath his foot and rubbed it in.
أخبرنا عبيد الله بن سعيد قال حدثنا يحيى عن سفيان قال حدثني منصور عن ربعي
قال رسول الله ﷺ إذا كنت تصلي فلا تبزقن بين يديك ولا عن يمينك وابصق خلفك أو تلقاء شمالك إن كان فارغا وإلا فهكذا وبزق تحت رجله ودلكه
[Its chain of transmission is sahih] Tirmidhi narrated it, Prayer, chapter on the disapproval of spitting in the masjid, no. 571, from the hadith of Yahya bin Saeed Qattan, with this wording, and he said, "Hasan sahih." It is also in Sunan al-Kubra, no. 805, Sunan Abu Dawud, no. 478, and Ibn Majah, no. 1021.