Nginx配置虛擬主機實例

Nginx下,一個server標籤就是一個虛擬主機,分爲以下三種情況:

基於端口:應用於公司內部網站和外部網站的管理後臺

基於域名:應用於外部網站

基於IP:幾乎不用

1)基於端口的虛擬主機

在html目錄下新建welcome.html

 

<!DOCTYPE html>
<html>
<head>
<title>Welcome to my  nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to my nginx!</h1>
<p>IP is localhost: 172.28.6.190<p>
<p> Base Port:8001<p>
</body>
</html>


修改nginx.conf,添加以下內容

 

 

#new server based port
    server{
        listen  8001;
        server_name     localhost;
       #access_log      logs/domain2.access.log  main;
        location / {
            root   html;
            index  welcome.html;
        }
}


重啓nginx服務,使用瀏覽器訪問

 

2)基於域名的虛擬主機

 

 

在windows下添加host域名,方法如下:

在C:\Windows\System32\drivers\etc中的hosts文件裏,添加一個與虛擬機(ip:172.28.6.190)對應的域名(www.domain.com)

在html目錄下新建domain.html

 

<!DOCTYPE html>
<html>
<head>
<title>Welcome to domain  nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to domain nginx!</h1>
<p>IP is 172.28.6.190<p>
<p> Base Domain:www.domain.com<p>
</body>
</html>


修改nginx.conf,添加以下內容

 

 

#new server based domain
        server{
                listen  80;
                server_name     www.domain.com;
                #access_log      logs/domain2.access.log  main;
                location / {
                        root   html;
                        index  domain.html;
                }
        }


重啓nginx服務,使用瀏覽器訪問

 

3)基於IP的虛擬主機

 

在html目錄下新建ip.html

 

<!DOCTYPE html>
<html>
<head>
<title>Welcome to my ip nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to my ip nginx!</h1>
<p>IP  is  172.28.6.190<p>
<p> Base IP<p>
</body>
</html>


修改nginx.conf,添加以下內容

 

 

#new server based ip
        server{
                listen  80;
                server_name     172.28.6.190;
                #access_log      logs/domain2.access.log  main;
                location / {
                        root   html;
                        index  ip.html;
                }
        }

 

重啓nginx服務,使用瀏覽器訪問

發佈了40 篇原創文章 · 獲贊 52 · 訪問量 11萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章