Someone who must
daven outside of a shul should not
daven with his back towards the shul or facing a direction opposite to the one that the congregants face while
davening. If someone realized in the middle of
davening that he is facing the wrong direction, he should turn around -- even during
Shemoneh Esrei. Someone
davening alongside a shul should face towards Yerushalayim. If that is not possible, he should turn towards the shul. The
poskim discuss whether the prohibition against
davening while facing the wrong direction is in effect at all times or only while there is a
minyan
davening in the shul.
. והמתפלל מחוץ לבית הכנסת בסמוך לו, ומסיבה כלשהי אינו יכול להתפלל לכיוון שאליו מתפללים - יתפלל לכיוון בית הכנסת
(
סעיף ז וס"ק טו-יט; ביאורים ומוספים דרשו, 13-14)