nginx 配置虛擬主機的三種方法

nginx下,一個server標籤就是一個虛擬主機。

1、基於域名的虛擬主機,通過域名來區分虛擬主機——應用:外部網站

2、基於端口的虛擬主機,通過端口來區分虛擬主機——應用:公司內部網站,外部網站的管理後臺

3、基於ip的虛擬主機,幾乎不用。

1、基於域名配置虛擬主機配置:

需要建立/data/www /data/bbs目錄,windows本地hosts添加虛擬機ip地址對應的域名解析;

對應域名網站目錄下新增index.html文件; 

nginx.conf配置文件新增如下代碼:

server {
   listen 80;
   server_name www.yong.com;
   index index.html;
   root /data/www;
}
server {
   listen 80;
   server_name bbs.yong.com;
   index index.html;
   root /data/bbs;
}

驗證結果,使用curl測試,或者瀏覽器輸入域名訪問
# curl -xlocalhost:80 www.yong.com
this is yong linux
# curl -xlocalhost:80 bbs.yong.com
this is yong bbs
2、基於端口的虛擬主機配置:

使用端口來區分,瀏覽器使用域名或ip地址:端口號 訪問

server
{
    listen 8000;
    server_name www.yong.com;
    root /data/www;
}
server
{
    listen 8001;
    server_name www.yong.com;
    root /data/bbs;
}

驗證結果,使用curl測試,或者瀏覽器輸入域名訪問
# curl www.yong.com:8000
this is yong linux
# curl www.yong.com:8001
this is yong bbs

3、基於ip地址的虛擬主機配置:

通過ip來訪問,需要配置多個ip
ifconfig eth0:1 192.168.22.21

server
{
    listen 192.168.20.20:80;
    server_name www.yong.com;
    root /data/www;
}
server
{
    listen 192.168.20.21:80;
    server_name www.yong.com;
    root /data/bbs;
}

驗證結果,使用curl測試,或者瀏覽器輸入域名訪問
# curl 192.168.22.20
this is yong linux
# curl 192.168.22.21
this is yong bbs
本文出自“模範生的學習博客”博客,請務必保留此出處http://mofansheng.blog.51cto.com/8792265/1677849
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章