新聞中心
小編給大家分享一下nginx如何實現(xiàn)虛擬機配置,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
成都創(chuàng)新互聯(lián)公司專業(yè)為企業(yè)提供西烏珠穆沁網(wǎng)站建設(shè)、西烏珠穆沁做網(wǎng)站、西烏珠穆沁網(wǎng)站設(shè)計、西烏珠穆沁網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計與制作、西烏珠穆沁企業(yè)網(wǎng)站模板建站服務(wù),十余年西烏珠穆沁做網(wǎng)站經(jīng)驗,不只是建網(wǎng)站,更提供有價值的思路和整體網(wǎng)絡(luò)服務(wù)。
什么是虛擬主機:
虛擬主機是一種特殊的軟硬件技術(shù),它可以將網(wǎng)絡(luò)上的每一臺計算機分成多個虛擬主機,每個虛擬主機可以獨立對外提供www服務(wù),這樣就可以實現(xiàn)一臺主機對外提供多個web服務(wù),每個虛擬主機之間是獨立的,互不影響。
nginx可以實現(xiàn)虛擬主機的配置,nginx支持三種類型的虛擬主機配置。
1、基于域名的虛擬主機 (server_name來區(qū)分虛擬主機——應(yīng)用:外部網(wǎng)站)
2、基于ip的虛擬主機, (一塊主機綁定多個ip地址)
3、基于端口的虛擬主機 (端口來區(qū)分虛擬主機——應(yīng)用:公司內(nèi)部網(wǎng)站,外部網(wǎng)站的管理后臺)
范例:
一、 基于域名的虛擬主機
1、配置通過域名區(qū)分的虛擬機
[root@MySQL03 nginx]# cat conf/nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
server {
listen 80;
server_name www.nginx01.com;
location / {
root html;
index index.html index.htm;
}
}
server {
listen 80;
server_name www.nginx02.com;
location / {
root /root/html;
index index.html index.htm;
}
}
}
2、 為 域名為 www.nginx02.com 的虛擬機,創(chuàng)建 index 文件
[root@mysql03 ~]# mkdir -p /root/html
[root@mysql03 ~]# cd /root/html/
[root@mysql03 html]# vi index.html
[root@mysql03 html]# cat index.html
this is my nginx
3、重新加載配置文件
[root@mysql03 nginx]# ./sbin/nginx -s reload
4、客戶端配置路由映射
在 C:\Windows\System32\drivers\etc\hosts 文件中添加兩行
10.219.24.26 www.nginx01.com
10.219.24.26 www.nginx02.com
如圖:
5、 測試訪問
瀏覽器輸入:http://www.nginx01.com/
瀏覽器輸入:http://www.nginx02.com/
>成功!
補充:如果配置不能正常訪問, 試參考 http://blog.csdn.NET/zhang123456456/article/details/73252148
二、 基于ip的虛擬主機
1. 一塊網(wǎng)卡綁定多個ip
[root@mysql03 nginx]# ifconfig eth0:1 10.219.24.27
[root@mysql03 nginx]# ifconfig
eth0 Link encap:Ethernet HWaddr 00:0C:29:79:F4:02
inet addr:10.219.24.26 Bcast:10.255.255.255 Mask:255.0.0.0
...
eth0:1 Link encap:Ethernet HWaddr 00:0C:29:79:F4:02
inet addr:10.219.24.27 Bcast:10.255.255.255 Mask:255.0.0.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
2. 配置通過ip區(qū)分的虛擬機
[root@mysql03 nginx]# cat conf/nginx.conf
user root root; #說明:這里的user根據(jù) 自己的nginx.conf文件所在的目錄的屬主屬性而定
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
server {
listen 10.219.24.26:80;
server_name www.nginx01.com;
location / {
root html;
index index.html index.htm;
}
}
server {
listen 10.219.24.27:80;
server_name www.nginx01.com;
location / {
root /root/html;
index index.html index.htm;
}
}
}
3. reopen nginx
[root@mysql03 nginx]# ./sbin/nginx -s reopen
補充:
-- 刪除綁定的vip
ifconfig eth0:1 10.219.24.27 down
三、 基于端口的虛擬主機
配置通過端口區(qū)分的虛擬機
[root@mysql03 nginx]# cat conf/nginx.conf
user root root; #說明:這里的user根據(jù) 自己的nginx.conf文件所在的目錄的屬主屬性而定
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
server {
listen 80;
server_name www.nginx01.com;
location / {
root html;
index index.html index.htm;
}
}
server {
listen 8080;
server_name www.nginx01.com;
location / {
root /root/html;
index index.html index.htm;
}
}
}
以上是“nginx如何實現(xiàn)虛擬機配置”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學習更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
網(wǎng)頁題目:nginx如何實現(xiàn)虛擬機配置
文章路徑:http://fisionsoft.com.cn/article/pgehcc.html