vim /etc/nginx/nginx.conf
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include vhost/*.conf; //添加这一行
include /etc/nginx/mime.types;
default_type application/octet-stream;
mkdir -p /etc/nginx/vhost //创建与nginx.conf同级的目录
vim /etc/nginx/vhost/www.aaa.conf //创建虚拟主机配置文件
server {
server_name www.aaa;
root /usr/local/nginx/html/aaa;
location / {
index index.html;
}
}
vim /etc/nginx/vhost/www.bbb.conf
server {
server_name www.bbb;
root /usr/local/nginx/html/bbb;
location / {
index index.html;
}
}
# 创建目录
mkdir /usr/local/nginx/html/{aaa,bbb}
# 创建测试页面
echo "this is aaa test" > /usr/local/nginx/html/aaa/index.html
echo "this is bbb test" > /usr/local/nginx/html/bbb/index.html
# 配置域名快速解析
echo 192.168.140.143 www.aaa www.bbb >> /etc/hosts
systemctl restart nginx
# curl www.aaa
this is aaa test
# curl www.bbb
this is bbb test
这里为方便起见,直接添加配置文件
# vim /etc/nginx/vhost/ddk.conf
server {
listen 81;
server_name localhost;
location / {
root /usr/local/nginx/html/bb1;
index index.html;
}
}
server {
listen 82;
server_name localhost;
location / {
root /usr/local/nginx/html/bb2;
index index.html;
}
}
server {
listen 83;
server_name localhost;
location / {
root /usr/local/nginx/html/bb3;
index index.html;
}
}
mkdir -p /usr/local/nginx/html/{bb1,bb2,bb3}
echo "this is ddk.test1" > /usr/local/nginx/html/bb1/index.html
echo "this is ddk.test2" > /usr/local/nginx/html/bb2/index.html
echo "this is ddk.test3" > /usr/local/nginx/html/bb3/index.html
# curl 192.168.140.143:81
this is ddk.test1
# curl 192.168.140.143:82
this is ddk.test2
# curl 192.168.140.143:83
this is ddk.test3
这里还是接着添加配置文件演示,前提是多网卡或者添加虚拟机添加网络适配器。这里是用虚拟机演示。
点击虚拟机设置------》添加 -----》网络适配器 ------》完成 -------》在重复添加一次
# 添加完后,重启网卡,查看ip
systemctl restart network
# ip a | grep '/24' | awk '{print $2}' | awk -F '/' '{print $1}'
192.168.140.143
192.168.140.145
192.168.140.146
server {
listen 192.168.140.143:80;
server_name localhost;
location / {
root /usr/local/nginx/html/bb1;
index index.html;
}
}
server {
listen 192.168.140.145:80;
server_name localhost;
location / {
root /usr/local/nginx/html/bb2;
index index.html;
}
}
server {
listen 192.168.140.146:80;
server_name localhost;
location / {
root /usr/local/nginx/html/bb3;
index index.html;
}
}
# 创建目录
mkdir /usr/local/nginx/html/{bb1,bb2,bb3}
# 创建测试页面
echo "this is a 192.168.140.143" > bb1/index.html
echo "this is a 192.168.140.145" > bb2/index.html
echo "this is a 192.168.140.146" > bb3/index.html
systemctl restart nginx
# curl 192.168.140.143
this is a 192.168.140.143
# curl 192.168.140.145
this is a 192.168.140.145
# curl 192.168.140.146
this is a 192.168.140.146