BLOG16: Nginx Based Webhosting

BLOG 40: Nginx Based Webhosting NGINX configuration example showing:

  1. Port-based virtual hosts

  2. Name-based virtual hosts

  3. Load balancing using upstream

  4. Routing rules (basic path-based routing)

We’ll assume a Linux server environment with NGINX installed.


1. Port-Based Virtual Hosts

Here, NGINX serves different content on different ports.

# /etc/nginx/conf.d/port_based.conf
server {
    listen 8080;
    server_name localhost;

    location / {
        root /var/www/html_port1;
        index index.html;
    }
}

server {
    listen 9090;
    server_name localhost;

    location / {
        root /var/www/html_port2;
        index index.html;
    }
}
  • Access with http://server-ip:8080 → content from /var/www/html_port1

  • Access with http://server-ip:9090 → content from /var/www/html_port2


2. Name-Based Virtual Hosts

Here, NGINX serves different websites based on the Host header.

  • site1.example.com/var/www/site1

  • site2.example.com/var/www/site2

(Make sure DNS or /etc/hosts resolves the names.)


3. Load Balancer with Upstream

Here, NGINX distributes requests to multiple backend servers.

  • Requests to myapp.example.com are load-balanced across 192.168.1.101, .102, .103.


4. Rules / Path-based Routing

We can define different behavior based on URL paths.

  • /images/ → serves static files directly

  • /api/ → load-balanced to API servers

  • / → default app backend servers


Last updated