1. 程式人生 > >Nginx入門篇(三)之虛擬主機配置

Nginx入門篇(三)之虛擬主機配置

dex 三種 ifconf lag php 創建 body 基於 vhosts

  • 一、虛擬主機概念

  所謂虛擬主機,在Web服務當中就是一個獨立的網站站點,這個站點對應獨立的域名(也有可能是IP或者端口),具有獨立的程序和資源目錄,可以獨立地對外提供服務供用戶訪問。

  這個獨立的站點在配置裏是由一定格式的標簽進行標記,和apache相對比,apache的虛擬主機的標簽段通常是以<VirtualHost></VirtualHost>進行標註的,而Nginx則是以Server{}標簽段來標示一個虛擬主機。一個Web服務中支持多個虛擬主機站點。

  • 二、虛擬主機類型

和apache一樣,虛擬主機主要有3種:

(1)基於域名的虛擬主機

(2)基於端口的虛擬主機

(3)基於IP的虛擬主機

  • 三、三種虛擬主機類型配置演練

(1)基於域名域名的虛擬主機配置

(1)修改主配置文件nginx.conf,加載虛擬主機配置
[root@localhost conf]# grep -Ev "^$|#" nginx.conf user nginx; worker_processes auto; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; log_format main
$remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" "$http_x_forwarded_for"; sendfile on; tcp_nopush on; keepalive_timeout 65; include /usr/local/nginx/conf/vhosts/*
.conf;  #包含虛擬主機配置 }

(2)創建虛擬主機配置文件,並增加虛擬主機 [root@localhost conf]# mkdir vhosts && cd vhosts/ [root@localhost vhosts]# vim www.abc.org.conf server { listen 80; server_name www.abc.org; root /vhosts/html/www; index index.html index.htm index.php; } [root@localhost vhosts]# cp www.abc.org.conf bbs.abc.org.conf [root@localhost vhosts]# cp www.abc.org.conf blog.abc.org.conf [root@localhost vhosts]# vim bbs.abc.org.conf server { listen 80; server_name bbs.abc.org; root /vhosts/html/bbs; index index.html index.htm index.php; } [root@localhost vhosts]# vim blog.abc.org.conf server { listen 80; server_name blog.abc.org; root /vhosts/html/blog; index index.html index.htm index.php; }

(3)創建虛擬主機主頁 [root@localhost vhosts]# mkdir /vhosts/html/{www,bbs,blog} [root@localhost vhosts]# echo "welcome to www.abc.org" >> /vhosts/html/www/index.html [root@localhost vhosts]# echo "welcome to bbs.abc.org" >> /vhosts/html/bbs/index.html [root@localhost vhosts]# echo "welcome to blog.abc.org" >> /vhosts/html/blog/index.html

(4)檢查語法,重載nginx [root@localhost vhosts]# /usr/local/nginx/sbin/nginx -t nginx: the configuration file /usr/local/nginx1.15.1/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx1.15.1/conf/nginx.conf test is successful [root@localhost vhosts]# /usr/local/nginx/sbin/nginx -s reload

windows下做hosts解析

192.168.56.11 www.abc.org bbs.abc.org blog.abc.org 分別訪問

技術分享圖片技術分享圖片技術分享圖片

(2)基於端口的虛擬主機配置

(1)修改bbs和blog站點監聽端口
[root@localhost vhosts]# vim bbs.abc.org.conf listen
8081; [root@localhost vhosts]# vim blog.abc.org.conf listen 8082 [root@localhost vhosts]# export PATH=/usr/local/nginx/sbin/:$PATH

(2)檢查語法,重載nginx [root@localhost vhosts]# nginx
-t nginx: the configuration file /usr/local/nginx1.15.1/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx1.15.1/conf/nginx.conf test is successful [root@localhost vhosts]# nginx -s reload

(3)測試訪問頁面 [root@localhost
~]# curl www.abc.org welcome to www.abc.org [root@localhost ~]# curl bbs.abc.org:8081 welcome to bbs.abc.org [root@localhost ~]# curl blog.abc.org:8082 welcome to blog.abc.org

以上端口可以隨意更改,但是不能和已有服務沖突,原則上應該是大於1024小於65535的任意端口

(3)基於IP的虛擬主機配置

(1)增加虛擬網卡eth0:0和eth0:1
[root@localhost ~]# ifconfig eth0:0 192.168.56.110/24 up [root@localhost ~]# ifconfig eth0:1 192.168.56.111/24 up [root@localhost ~]# ifconfig eth0:0 eth0:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.56.110 netmask 255.255.255.0 broadcast 192.168.56.255 ether 00:0c:29:ce:31:fd txqueuelen 1000 (Ethernet) [root@localhost ~]# ifconfig eth0:1 eth0:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.56.111 netmask 255.255.255.0 broadcast 192.168.56.255 ether 00:0c:29:ce:31:fd txqueuelen 1000 (Ethernet)

(2)修改虛擬主機配置server_name為ip訪問 [root@localhost vhosts]# vim bbs.abc.org.conf listen
8081; server_name 192.168.56.110; [root@localhost vhosts]# vim blog.abc.org.conf listen 8082; server_name 192.168.56.111;

(3)檢測語法,重載nginx,測試訪問 [root@localhost vhosts]# nginx
-t nginx: the configuration file /usr/local/nginx1.15.1/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx1.15.1/conf/nginx.conf test is successful [root@localhost vhosts]# nginx -s reload [root@localhost ~]# curl http://192.168.56.110:8081/ welcome to bbs.abc.org [root@localhost ~]# curl http://192.168.56.111:8082/ welcome to blog.abc.org

Nginx入門篇(三)之虛擬主機配置