007.Nginx虛擬主機
阿新 • • 發佈:2020-07-11
一 虛擬主機
1.1 虛擬主機概念
對於Nginx而言,每一個虛擬主機相當於一個在同一臺伺服器中卻相互獨立的站點,從而實現一臺主機對外提供多個 web 服務,每個虛擬主機之間是獨立的,互不影響的。1.2 虛擬主機型別
通過 Nginx 可以實現虛擬主機的配置,Nginx 支援三種型別的虛擬主機配置:- 基於 IP 的虛擬主機(較少使用)
- 基於域名的虛擬主機
- 基於埠的虛擬主機
二 基於IP虛擬主機
2.1 配置多IP地址
1 [[email protected] ~]# ifconfig eth0:0 172.24.8.70 broadcast 172.24.8.255 netmask 255.255.255.0提示:如上在同一臺主機新增多個IP地址。2 [[email protected] ~]# ip addr | grep 172
3 inet 172.24.8.71/24 brd 172.24.8.255 scope global noprefixroute eth0
4 inet 172.24.8.72/24 brd 172.24.8.255 scope global secondary eth0:0
2.2 建立站點目錄
1 [[email protected] ~]# mkdir /usr/share/nginx/ipvhost01/
2 [[email protected]~]# mkdir /usr/share/nginx/ipvhost02/
3 [[email protected] ~]# echo '<h1>Ipvhost01</h1>' > /usr/share/nginx/ipvhost01/index.html
4 [[email protected] ~]# echo '<h1>Ipvhost02</h1>' > /usr/share/nginx/ipvhost02/index.html
2.3 配置虛擬主機
1 [[email protected] ~]# vi /etc/nginx/conf.d/ipvhost01.conf2 server {
3 listen 80; #監聽埠
4 server_name ipvhost01.odocker.com 172.24.8.71; #配置虛擬主機名和IP
5 location / {
6 root /usr/share/nginx/ipvhost01; #請求匹配路徑
7 index index.html; #指定主頁
8 access_log /var/log/nginx/ipvhost01.access.log main;
9 error_log /var/log/nginx/ipvhost01.error.log warn;
10 }
11 }
12 server {
13 listen 80;
14 server_name ipvhost02.odocker.com 172.24.8.72;
15 location / {
16 root /usr/share/nginx/ipvhost02;
17 index index.html;
18 access_log /var/log/nginx/ipvhost02.access.log main;
19 error_log /var/log/nginx/ipvhost02.error.log warn;
20 }
21 }
1 [[email protected] ~]# nginx -t -c /etc/nginx/nginx.conf #檢查配置檔案
2 [[email protected] ~]# nginx -s reload #過載配置檔案
2.4 確認驗證
瀏覽器訪問:ipvhost01.odocker.com。 瀏覽器訪問:ipvhost02.odocker.com。三 基於域名虛擬主機
3.1 建立站點目錄
1 [[email protected] ~]# mkdir /usr/share/nginx/webvhost01/
2 [[email protected] ~]# mkdir /usr/share/nginx/webvhost02/
3 [[email protected] ~]# echo '<h1>Webvhost01</h1>' > /usr/share/nginx/webvhost01/index.html
4 [[email protected] ~]# echo '<h1>Webvhost02</h1>' > /usr/share/nginx/webvhost02/index.html
3.2 配置虛擬主機
1 [[email protected] ~]# vi /etc/nginx/conf.d/webvhost.conf
2 server {
3 listen 80;
4 server_name webvhost01.odocker.com;
5 location / {
6 root /usr/share/nginx/webvhost01;
7 index index.html;
8 access_log /var/log/nginx/webvhost01.access.log main;
9 error_log /var/log/nginx/webvhost01.error.log warn;
10 }
11 }
12 server {
13 listen 80;
14 server_name webvhost02.odocker.com;
15 location / {
16 root /usr/share/nginx/webvhost02;
17 index index.html;
18 access_log /var/log/nginx/webvhost02.access.log main;
19 error_log /var/log/nginx/webvhost02.error.log warn;
20 }
21 }
3.3 確認驗證
瀏覽器訪問:webvhost01.odocker.com。 瀏覽器訪問:webvhost02.odocker.com。四 基於埠虛擬主機
4.1 建立站點目錄
1 [[email protected] ~]# mkdir /usr/share/nginx/portvhost01/
2 [[email protected] ~]# mkdir /usr/share/nginx/portvhost02/
3 [[email protected] ~]# echo '<h1>Portvhost01</h1>' > /usr/share/nginx/portvhost01/index.html
4 [[email protected] ~]# echo '<h1>Portvhost01</h1>' > /usr/share/nginx/portvhost02/index.html
3.2 配置虛擬主機
1 [[email protected] ~]# vi /etc/nginx/conf.d/portvhost.conf
2 server {
3 listen 8080;
4 server_name portvhost01.odocker.com;
5 location / {
6 root /usr/share/nginx/portvhost01;
7 index index.html;
8 access_log /var/log/nginx/portvhost01.access.log main;
9 error_log /var/log/nginx/portvhost01.error.log warn;
10 }
11 }
12 server {
13 listen 8081;
14 server_name portvhost02.odocker.com;
15 location / {
16 root /usr/share/nginx/portvhost02;
17 index index.html;
18 access_log /var/log/nginx/access_portvhost02.log main;
19 }
20 }