nginx 搭建虛擬主機

1、排錯三部曲html

第一步在客戶端上ping服務端ip  ping 10.0.0.8linux

第二部在客戶端上telnet服務器端IP、端口  telnet 10.0.0.8nginx

第三部在客戶端使用wget命令檢測   wget 10.0.0.8  模擬用戶訪問,排除http服務自身問題web

2、部署簡單的web站點apache

1)進入配置文件目錄vim

[root@oldboy conf]# cd /application/nginx
[root@oldboy nginx]# cd conf/windows

2)把備份的配置文件傳遞給配置文件瀏覽器

[root@oldboy conf]# egrep -v "^$|#" nginx.conf.default >nginx.conf安全

3)修改域名服務器

  1 worker_processes  1;
  2 events {
  3     worker_connections  1024;
  4 }
  5 http {
  6     include       mime.types;
  7     default_type  application/octet-stream;
  8     sendfile        on;
  9     keepalive_timeout  65;
 10     server {
 11         listen       80;
 12         server_name  http://www.cnblogs.com/pyrene/;   --》這裏修改域名
 13         location / {
 14             root   html;
 15             index  index.html index.htm;
 16         }
 17         error_page   500 502 503 504  /50x.html;
 18         location = /50x.html {
 19             root   html;
 20         }
 21     }
 22 }

4)檢查[root@oldboy conf]# /application/nginx/sbin/nginx -t

5)優雅重啓 [root@oldboy conf]# /application/nginx/sbin/nginx -s reload

6)修改主頁 [root@oldboy nginx]# cd /application/nginx/html/

7)修改主頁內容

<html>
<head><title>pyrene</title></head>
<body>
歡迎來到pyrene的博客!
<a href="http://www.cnblogs.com/pyrene/" target=_blank><img src="bingbing.jpg">冰冰</a>
</body>
</html>

這裏的bingbing.jpg圖片要rz -y上傳到html目錄裏面
vim index.html

8)在瀏覽器中輸入10.0.0.8就能夠顯示

9)在window裏面設置域名解析  win+R 輸入drivers --》打開etc文件夾,找到hosts 輸入 域名解析 10.0.0.8 http://www.cnblogs.co

10)在cmd中輸入www.cnblogs.co 若是返回的是10.0.0.8的返回信息,而後瀏覽器中輸入www.cnblogs.co就能顯示網頁

3、nginx模塊

 核心模塊Core functionality

 

 4、nginx目錄結構和配置參數詳細介紹

 

 1 [root@A nginx]# tree
 2 .
 3 ├── client_body_temp
 4 ├── conf
 5 │   ├── fastcgi.conf
 6 │   ├── fastcgi.conf.default
 7 │   ├── fastcgi_params
 8 │   ├── fastcgi_params.default
 9 │   ├── koi-utf
10 │   ├── koi-win
11 │   ├── mime.types  媒體類型
12 │   ├── mime.types.default
13 │   ├── nginx.conf
14 │   ├── nginx.conf.default
15 │   ├── scgi_params
16 │   ├── scgi_params.default
17 │   ├── uwsgi_params
18 │   ├── uwsgi_params.default
19 │   └── win-utf
20 ├── fastcgi_temp
21 ├── html
22 │   ├── 50x.html
23 │   └── index.html
24 ├── logs   日誌
25 │   ├── access.log  訪問日誌(能夠記錄客戶從哪裏訪問的,用的什麼瀏覽器,訪問多久,等等)
26 │   ├── error.log   錯誤日誌
27 │   └── nginx.pid
28 ├── proxy_temp
29 ├── sbin
30 │   └── nginx
31 ├── scgi_temp
32 └── uwsgi_temp

5、nginx主配置文件

 全部的標籤都在http標籤內

 

 

nginx最大併發數 是 第6行 worker數 ×1024  ,調節worker數量在第一行,通常狀況下數量和核心數至關

 

 

 

上面一個server標籤就是一個虛擬主機,server_name 域名  18-21是全部默認請求就在location這邊訪問

 

 

全部等號請求在22-24好區域

22-24是優先處理的,以後處理18-21默認請求

至關於if判斷,若是有location先匹配,也有多個

 6、參數介紹

 

worker_processes  1;   #worker進程的數量   通常和cpu的數量相同的最好

events {              #      模型

    worker_connections  1024;   # 表示一個worker最大服務的用戶數量 epoll模型在這裏配置

}

http {                         #標籤  

    include       mime.types;    #包含

    default_type  application/octet-stream;   #媒體類型

    sendfile        on;                #開啓搞笑傳輸模式

    keepalive_timeout  65;      #超時時間

    server {                #對應一個網站  多個網站多個

        listen       80;     #監聽端口

        server_name  www.cnblogs.com/pyrene/;   #網站域名

        location / {                          #默認的採用這個

            root   html;                   #站點目錄

            index  index.html index.htm;       #首頁文件

        }

        error_page   500 502 503 504  /50x.html;   #優雅顯示內容

        location = /50x.html {

            root   html;        #若是顯示上面的就去這裏面找

        }

    }

}

7、虛擬主機是什麼

所謂的虛擬主機,在web服務裏面就是一個獨立的網站站點,這個站點對應獨立的域名(也就是ip或者端口)具備獨立的程序及資源目錄,能夠獨立的對提供服務供用戶訪問

這個獨立的站點在配置裏是必定格式的標籤段標記,對於apache軟件來數,一個虛擬主機標籤段一般被包含在<VirtuaHost></VirtuaHost>內部,而Nginx軟件則使用一個server{}標籤來標示一個虛擬主機,一個web服務裏能夠有多個虛擬主機標籤對,即同時能夠支持多個虛擬主機站點

8、虛擬主機類型

一、  基於域名的虛擬主機經過域名來區分虛擬主機

外部網站

二、  基於端口的虛擬主機

內部網站或者網站後臺

三、基於ip的虛擬主機

9、基於域名建立虛擬主機

 

一、cp nginx.conf  nginx.conf.pyrene.20160513V1    這裏修改要帶上用戶 時間 版本

 

二、修改配置文件,刪除掉error_page

 1 [root@oldboy conf]# vim nginx.conf
 2 worker_processes  1;
 3 events {
 4     worker_connections  1024;
 5 }
 6 http {
 7     include       mime.types;
 8     default_type  application/octet-stream;
 9     sendfile        on;
10     keepalive_timeout  65;
11     server {
12         listen       80;
13         server_name  www.cnblogs.com/pyrene/;
14         location / {
15             root   html/www;
16             index  index.html index.htm;
17         }
18     }
19     server {
20         listen       80;
21         server_name  bbs.cnblogs.com/pyrene/;
22         location / {
23             root   html/bbs;
24             index  index.html index.htm;
25         }
26     }
27 }

三、建立站點目錄

[root@A conf]# mkdir ../html/{www,bbs} -p

[root@A conf]# tree ../html/

../html/

├── 50x.html

├── bbs

├── index.html

└── www

四、在兩個目錄裏面生成首頁文件

[root@A conf]# echo "www.etiantian.org www.cnblogs.com/pyrene/" >../html/www/index.html   #把域名放到首頁文件

[root@A conf]# echo " bbs.cnblogs.com/pyrene/" >../html/bbs/index.html

[root@A conf]# cat ../html/{www,bbs}/index.html    →檢查首頁是否生成域名

www.cnblogs.com/pyrene/

bbs.cnblogs.com/pyrene/

五、由於修改了配置文件因此要重啓這個服務,可是重啓以前要檢查是否正確用-t這個參數

首先檢查是否正確 /application/nginx/sbin/nginx  -t

而後再重啓  /application/nginx/sbin/nginx -s 加載  reload  重啓,若是reload不行那麼就直接stop,而後再啓動

六、(解析很是重要)由於沒有域名,因此要本身建立在本機測試  vim /etc/hosts  10.0.0.8 www.cnblogs.com/pyrene/ bbs.cnblogs.com/pyrene/

七、查看是否鏈接ok  ping www.cnblogs.com/pyrene/

八、查看訪問是否成功

[root@A conf]# curl www.cnblogs.com/pyrene/

www.cnblogs.com/pyrene/

windows下的測試

上面6和7 是在linux本機上面測試,下面是在windows機器上面測試

一、Win+R 輸入drivers找etc打開hosts 這裏是郵件點擊得到管理員權限

二、輸入10.0.0.8 www.cnblogs.com/pyrene/ bbs.cnblogs.com/pyrene/

三、在瀏覽器中輸入域名地址ok

 

原理:

當咱們輸入網頁,在hosts文件裏面會解析成IP,這是發起了一條tcp連接,以後創建http鏈接,http鏈接裏面有請求頭,請求頭裏面對應一個主機名,當請求到達服務器的時候,會讀請求頭,,若是沒有請求頭(直接用IP訪問)就傳遞給用戶第一個虛擬主機,因此若是隻用ip訪問就會發給用戶第一個虛擬主機,定位到虛擬主機讀站點目錄,讀首頁文件,入股沒有首頁文件是403
原理

 

10、增長一個博客首頁

1、    複製一個虛擬主機 
2、在虛擬主機裏面修改域名和站點目錄
 server {
        listen       80;
        server_name  www.cnblogs.com/pyrene/;
        location / {
            root   html/blog;
            index  index.html index.htm;
        }
}
3、建立站點目錄,而且把域名放到網站首頁
[root@A conf]# mkdir ../html/blog
[root@A conf]# echo www.cnblogs.com/pyrene/ >../html/blog/index.html

4、檢查,而後重啓
[root@A conf]# /application/nginx/sbin/nginx -t
nginx: the configuration file /application/nginx-1.6.3/conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.6.3/conf/nginx.conf test is successful
[root@A conf]# /application/nginx/sbin/nginx -s reload

五、把域名寫到/etc/hosts裏面 而後查看
[root@A conf]# curl www.cnblogs.com/pyrene/
www.cnblogs.com/pyrene/
View Code

補充:

命令:curl
在Linux中curl是一個利用URL規則在命令行下工做的文件傳輸工具,能夠說是一款很強大的http命令行工具。它支持文件的上傳和下載,是綜合傳輸工具

11、基於端口的虛擬主機

偶爾會用,辦公OA系統、網站程序的後臺、CMS發佈後臺、MYSQL的web客戶端使用特殊端口是基於安全考慮的

1、    備份
首先複製原來的nginx.conf 到新的文件nginx.conf.base-name,以後再修改nginx.conf
2、vim nginx.conf
在虛擬主機中修改端口就能夠了
  server {
        listen       8001;
        server_name  www.cnblogs.com/pyrene/
;
        location / {
            root   html/www;
            index  index.html index.htm;
        }
    }
    server {
        listen       8002;
        server_name  bbs.cnblogs.com/pyrene/
;
        location / {
            root   html/bbs;
            index  index.html index.htm;
        }
    }
    server {
        listen       8003;
        server_name  blog.cnblogs.com/pyrene/
;
        location / {
            root   html/blog;
            index  index.html index.htm;
        }
3、檢查語法,而後重啓

[root@A conf]# ../sbin/nginx -t
nginx: the configuration file /application/nginx-1.6.3/conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.6.3/conf/nginx.conf test is successful
[root@A conf]# ../sbin/nginx -s reload
4、查看端口是否開啓
[root@A conf]# netstat -lntup|grep nginx
tcp        0      0 0.0.0.0:8001                0.0.0.0:*                   LISTEN      2412/nginx          
tcp        0      0 0.0.0.0:8002                0.0.0.0:*                   LISTEN      2412/nginx          
tcp        0      0 0.0.0.0:8003                0.0.0.0:*                   LISTEN      2412/nginx 
5、就能夠查看了
[root@A conf]# curl www.cnblogs.com/pyrene/:8001
www.cnblogs.com/pyrene/

總結:基於端口的虛擬主機,和基於域名的虛擬主機主要就是更改一下端口就能夠了
View Code
相關文章
相關標籤/搜索