Nginx基于端口的虚拟主机实现
nginx.conf
配置文件
下面我们配置了3 个虚拟主机,分别监听的是虚拟主机的 8080,8081,8082端口。由于我们前面额外配置了2个虚拟网卡,因此如下配置当
- 访问的是
192.168.125.100:8080
,192.168.125.101:8080
,192.168.125.101:8080
,都会输出"你好我是虚拟主机1"- 访问的是
192.168.125.100:8082
,192.168.125.101:8082
,192.168.125.101:8082
,都会输出"你好我是虚拟主机2"
#user nobody;
worker_processes auto;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
# 字符集设置成utf-8避免中文乱码
charset utf-8;
server {
listen 8080;
server_name localhost;
location / {
root html/virtual01;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 8081;
server_name localhost;
location / {
root html/virtual01;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 8082;
server_name localhost;
location / {
root html/virtual02;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
效果图如下