The liver connects the intestinal portal vasculature with the general circulation, using a diverse array of immune cells to protect from pathogens that translocate from the gut1. In liver lobules, blood flows from portal triads that are situated in periportal lobular regions to the central vein via a polarized sinusoidal network. Despite this asymmetry, resident immune cells in the liver are considered to be broadly dispersed across the lobule. This differs from lymphoid organs, in which immune cells adopt spatially biased positions to promote effective host defence2,3. Here we used quantitative multiplex imaging, genetic perturbations, transcriptomics, infection-based assays and mathematical modelling to reassess the relationship between the localization of immune cells in the liver and host protection. We found that myeloid and lymphoid resident immune cells concentrate around periportal regions. This asymmetric localization was not developmentally controlled, but resulted from sustained MYD88-dependent signalling induced by commensal bacteria in liver sinusoidal endothelial cells, which in turn regulated the composition of the pericellular matrix involved in the formation of chemokine gradients. In vivo experiments and modelling showed that this immune spatial polarization was more efficient than a uniform distribution in protecting against systemic bacterial dissemination. Together, these data reveal that liver sinusoidal endothelial cells sense the microbiome, actively orchestrating the localization of immune cells, to optimize host defence.