虛擬主機是一種特殊的軟硬件技術,它可以將網絡上的每一臺計算機分成多個虛擬主機,每個虛擬主機可以獨立對外提供www服務,這樣就可以實現一臺主機對外提供多個web服務,每個虛擬主機之間是獨立的,互不影響。
nginx可以實現虛擬主機的配置,nginx支持三種類型的虛擬主機配置。
1、基于域名的虛擬主機 (server_name來區分虛擬主機——應用:外部網站)
2、基于ip的虛擬主機, (一臺主機綁定多個ip地址)
3、基于端口的虛擬主機 (端口來區分虛擬主機——應用:公司內部網站,外部網站的管理后臺)
基于IP
worker_processes 4;
worker_rlimit_nofile 102400;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
server {
listen 192.168.17.134:80;
server_name localhost;
location / {
root /mcode;
index index.html index.htm;
}
}
server {
listen 192.168.17.135:80;
server_name localhost;
location / {
root /mcode1;
index index.html index.htm;
}
}
}
基于端口
worker_processes 4;
worker_rlimit_nofile 102400;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
server {
listen 80;
server_name localhost;
location / {
root /mcode;
index index.html index.htm;
}
}
server {
listen 81;
server_name localhost;
location / {
root /mcode1;
index index.html index.htm;
}
}
}
基于域名
worker_processes 4;
worker_rlimit_nofile 102400;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
server {
listen 80;
server_name www.mcode.com;
location / {
root /mcode;
index index.html index.htm;
}
}
server {
listen 80;
server_name www.mcode1.com;
location / {
root /mcode1;
index index.html index.htm;
}
}
}