Nginx基于多域名、多端口、多IP配置虚拟主机

配置虚拟主机的常见方式

一、基于多域名配置虚拟主机

1、编辑nginx配置文件,添加vhost虚拟主机配置路径
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;
2、创建vhost目录,与nginx配置文件同级
mkdir -p /etc/nginx/vhost                           //创建与nginx.conf同级的目录
3、创建虚拟主机配置文件
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;
               }
       }
4、创建虚拟主机测试目录及测试页面
# 创建目录       
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
5、重启nginx服务,curl测试
systemctl restart nginx
# curl www.aaa
this is aaa test
# curl www.bbb
this is bbb test

二、基于多端口配置虚拟主机

这里为方便起见,直接添加配置文件

1、创建虚拟主机配置文件ddk.conf
# 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;
      }
}
2、创建测试目录及页面
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
3、重启nginx服务,curl测试
# 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配置虚拟主机

这里还是接着添加配置文件演示,前提是多网卡或者添加虚拟机添加网络适配器。这里是用虚拟机演示。

1、虚拟机添加网络适配器,就是加几块网卡
点击虚拟机设置------》添加  -----》网络适配器  ------》完成 -------》在重复添加一次
# 添加完后,重启网卡,查看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
2、创建虚拟主机配置文件dip.conf
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;
      }
}
3、创建测试目录及页面
# 创建目录       
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
4、重启nginx服务,curl测试
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

你可能感兴趣的:(nginx,linux,服务器,运维,centos)