虚拟主机

虚拟主机就是把一台服务器主机分成多台虚拟的主机
每台虚拟主机都可以是一个独立的网站,可以具有独立的域名
具有internet服务器功能(www,ftp,email等)
同一台主机上的虚拟主机是完全独立的

利用虚拟主机,不用位每个网站提供一台单独的nginx服务器
或单独运行一组nginx进程

虚拟主机提供了在同一台服务器,同一组nginx进程上运行多个网站的功能

http
{
    server
    {
        listen 80 default;
        server_name _*;
        access_log  /var/www/test/access.log combined;
        error_log   /var/www/test/error.log combined;
        location / 
        {
            index index.html;
            root /var/www/test;
        }
    }
}

配置多种类型虚拟主机

1. 基于IP的虚拟主机

添加Ip别名,使用ifconfig,route

1. ifconfig lo  本地回环地址的作用
    1. 测试本机的网络配置,能ping同127.0.0.1说明本机网卡,ip协议没有问题
    2. SERVER/CLIENT,比如本地安装了nginx,可以测试127.0.0.1

2. 添加alias,需要添加到 /etc/rc.local中因为重启后会消失
    sudo ifconfig wlp5s0:1 192.168.0.43 broadcast 192.168.0.255 netmask 255.255.255.0 up
    sudo route add -host 192.168.0.43 dev wlp5s0:1

    sudo ifconfig wlp5s0:2 192.168.0.44 broadcast 192.168.0.255 netmask 255.255.255.0 up
    sudo route add -host 192.168.0.44 dev wlp5s0:2

配置

但是不好访问,你如果要给用户的话,还是需要域名的,所以我觉得这种配置方式好像没什么意义
访问如下
http://192.168.0.43
http://192.168.0.44

# 第一个虚拟主机
server 
{
    listen 192.168.0.43:80;
    server_name 192.168.0.43;
    access_log /var/www/vh01/access.log combined;
    location /
    {
        index index.html index.htm;
        root /var/www/vh01;
    }
}

# 第二个虚拟主机
server 
{
    listen 192.168.0.44:80;
    server_name 192.168.0.44;
    access_log /var/www/vh02/access.log combined;
    location /
    {
        index index.html index.htm;
        root /var/www/vh02;
    }
}

2. 基于域名

# 第一个虚拟主机
server 
{
    listen 192.168.0.43:80;
    server_name 192.168.0.43;
    access_log /var/www/vh01/access.log combined;
    location /
    {
        index index.html index.htm;
        root /var/www/vh01;
    }
}

# 第二个虚拟主机
server 
{
    listen 192.168.0.44:80;
    server_name 192.168.0.44;
    access_log /var/www/vh02/access.log combined;
    location /
    {
        index index.html index.htm;
        root /var/www/vh02;
    }
}

3. 基于端口

results matching ""

    No results matching ""