Sobes.tech
Junior — Middle

Is it possible to run multiple websites on the same port using Nginx?

sobes.tech AI

Answer from AI

It is not possible to run multiple websites on the same port directly, as a port is a unique identifier for a network connection. However, with Nginx, you can configure virtual hosts (server blocks) that allow hosting multiple sites on a single IP and port (usually 80 for HTTP or 443 for HTTPS).

This is achieved by differentiating based on the hostname (Host header in the HTTP request). Nginx analyzes the request header and redirects to the appropriate site.

Example configuration for two sites on the same port 80:

server {
    listen 80;
    server_name site1.example.com;
    root /var/www/site1;
}

server {
    listen 80;
    server_name site2.example.com;
    root /var/www/site2;
}

Thus, multiple sites operate on the same port but differ by domain name.