nginx虛擬主機配置(3)

1. 虛擬主機概念

在Web服務裏面虛擬主機就是一個獨立的網站站點,這個站點對應獨立的域名(也多是IP或端口),具備獨立的程序及資源目錄,能夠獨立地對外提供服務供用戶訪問。html

2. 虛擬主機類型

2.1. 基於域名的虛擬主機

不一樣虛擬主機對應一個獨立的域名nginx

2.2. 基於端口的虛擬主機

不一樣虛擬主機端口號不一樣。也能夠是同一個域名或同一個IP,但端口號不一樣shell

2.3. 基於IP的虛擬主機

不一樣虛擬主機IP地址也不一樣 windows

3. 虛擬主機配置

3.1. 基於域名的虛擬主機

切換到nginx配置目錄下面瀏覽器

cd /application/nginx/conf

過濾掉包含#號和空行,生成新的配置文件app

egrep -v "#|^$" nginx.conf.default > nginx.conf

更改配置文件nginx.confcurl

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80; # 端口,默認就是80
        server_name  www.ljmict.com;  # 域名
        location / {
            root   html/www;
            index  index.html index.htm;
        }
    }
}

建立域名www.ljmict.com對應的站點目錄及文件ide

mkdir /application/nginx/html/www
echo "http://www.ljmict.com" > ../html/www/index.html

查看www.ljmict.com對應的首頁文件index.html測試

cat ../html/www/index.html
# 查看結果
http://www.ljmict.com

檢查修改過的nginx文件配置是否有語法問題網站

/application/nginx/sbin/nginx -t

從新啓動nginx服務

/application/nginx/sbin/nginx -s reload

客戶端測試
在客戶端測試以前,須要更改客戶端的hosts文件,MACLinuxhosts文件都在/etc/hosts文件,若是是windows系統hosts文件在C:\Windows\System32\drivers\etc目錄下。

echo "172.16.1.10 www.ljmict.com" >> /etc/hosts
curl www.ljmict.com
# 結果
http://www.ljmict.com

在瀏覽器中訪問
nginx虛擬主機配置(3)

3.1.1. 配置多個基於域名的虛擬主機

更改nginx配置文件

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  www.ljmict.com;
        location / {
            root   html/www;
            index  index.html index.htm;
        }   
    }
    server {
        listen       80;
        server_name  bbs.ljmict.com;
        location / {
            root   html/bbs;
            index  index.html index.htm;
        }
    }
    server {
        listen       80;
        server_name  blog.ljmict.com;
        location / {
            root   html/blog;
            index  index.html index.htm;
        }
    }
}

建立對應的站點目錄及文件

mkdir /application/nginx/html/bbs
echo "http://bbs.ljmict.com" > /application/nginx/html/bbs/index.html
mkdir /application/nginx/html/blog
echo "http://blog.ljmict.com" > /application/nginx/html/blog/index.html

從新啓動nginx服務

/application/nginx/sbin/nginx -s reload

添加hosts記錄

echo "172.16.1.10 bbs.ljmict.com" >> /etc/hosts
echo "172.16.1.10 blog.ljmict.com" >> /etc/hosts

客戶端訪問
nginx虛擬主機配置(3)
nginx虛擬主機配置(3)
nginx虛擬主機配置(3)

3.2. 基於端口的虛擬主機

更改nginx配置文件

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;   # 端口保持不變
        server_name  www.ljmict.com;
        location / {
            root   html/www;
            index  index.html index.htm;
        }
    }
    server {
        listen       81;  # 端口更改爲81
        server_name  bbs.ljmict.com;
        location / {
            root   html/bbs;
            index  index.html index.htm;
        }
    }
    server {
        listen       82;  # 端口更改爲82
        server_name  blog.ljmict.com;
        location / {
            root   html/blog;
            index  index.html index.htm;
        }
    }
}

重啓nginx服務

/application/nginx/sbin/nginx -s reload

客戶端訪問
nginx虛擬主機配置(3)
nginx虛擬主機配置(3)
nginx虛擬主機配置(3)

3.3. 基於IP的虛擬主機

若是要配置基於IP的虛擬主機,就須要讓每一個虛擬主機有不一樣的IP地址,在這裏我臨時在ens37網卡上增長2個不一樣的IP

ip addr add 172.16.1.11/24 dev ens37
ip addr add 172.16.1.12/24 dev ens37
# 注意:根據本身的網卡名稱來配置IP

更改nginx配置文件

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  172.16.1.10;
        location / {
            root   html/www;
            index  index.html index.htm;
        }
    }
    server {
        listen       81;
        server_name  172.16.1.11; # 臨時配置的IP
        location / {
            root   html/bbs;
            index  index.html index.htm;
        }
    }
    server {
        listen       82;
        server_name  172.16.1.12; # 臨時配置的IP
        location / {
            root   html/blog;
            index  index.html index.htm;
        }
    }
}

從新啓動nginx服務

/application/nginx/sbin/nginx -s reload

客戶端訪問
nginx虛擬主機配置(3)
nginx虛擬主機配置(3)
nginx虛擬主機配置(3)

相關文章
相關標籤/搜索