Whether it is for a speaker, or a subwoofer, the port facing a boundary requires no more than the diameter of the port (actually, 1/2 will work) in distance from the boundary.
In the case of most rear firing, bookshelf speakers, let's assume the port operates from 55 to 70 Hz - You have a wavelength of appx. 8-10 feet, making the port location irrelevant. If the port is 2 inches in diameter, allow 1-2 inches of clearance, and you will be fine.