nginx配置文件nginx.conf之server及server | 您所在的位置:網(wǎng)站首頁 › 屬豬的利月是幾月了 › nginx配置文件nginx.conf之server及server |
本人在學(xué)習(xí)nginx的時(shí)候被server_name的意義困擾了很久。又是查資料,又是請(qǐng)教人。最后還是自己測(cè)試出來的。 你搜到這篇文章說明你已經(jīng)經(jīng)過了基本的配置,但是還不懂其中的含義。 server name 為虛擬服務(wù)器的識(shí)別標(biāo)志,匹配到特定的server塊,轉(zhuǎn)發(fā)到對(duì)應(yīng)的應(yīng)用服務(wù)器中去。 本文主要是解釋server_name的意義,文章最后會(huì)解釋server在整個(gè)訪問請(qǐng)求的流程;請(qǐng)注意文中紅色文字; 先上一段配置 server { listen ip:端口; # 當(dāng)listen出現(xiàn)了ip時(shí),server_name就失去了意義。所以不配置也罷了。 #server_name 域名; access_log 日志地址1; error_log 日志地址2; location / { root /data/www/151; index index.html index.htm; } }客戶端通過域名訪問服務(wù)器時(shí)會(huì)將域名與被解析的ip一同放在請(qǐng)求中。當(dāng)請(qǐng)求到了nginx中時(shí)。nginx會(huì)先去匹配ip,如果listen中沒有找到對(duì)應(yīng)的ip,就會(huì)通過域名進(jìn)行匹配,匹配成功以后,再匹配端口。當(dāng)這三步完成,就會(huì)找到對(duì)應(yīng)的server的location對(duì)應(yīng)的資源。 ? 驗(yàn)證如下: 第一步:在客戶端配置host,用于域名解析 #虛擬機(jī) 192.168.231.102 backend.xpe.com 192.168.231.151 www.test151.com 192.168.231.152 www.test152.com 192.168.231.153 www.test153.com 192.168.231.151 www.test154.com第二步:給虛擬機(jī)(服務(wù)端,以下這兩個(gè)詞會(huì)混用)配置另外三個(gè)ip,用于模擬多臺(tái)服務(wù)器; # ifconfig 打印出的結(jié)果 eth0 Link encap:Ethernet HWaddr 00:0C:29:BB:AC:FB inet addr:192.168.231.102 Bcast:192.168.231.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:febb:acfb/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:123455 errors:0 dropped:0 overruns:0 frame:0 TX packets:123953 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:34612737 (33.0 MiB) TX bytes:81191887 (77.4 MiB) 注意 eth0 下面的eth0是對(duì)應(yīng)的,你的虛擬機(jī)可能不同 # ifconfig eth0:1 192.168.231.151/24 up # ifconfig eth0:2 192.168.231.152/24 up # ifconfig eth0:3 192.168.231.153/24 up 再次執(zhí)行 # ifconfig 可以看到多了三個(gè)ip第三步:(非必須) 3個(gè)IP對(duì)應(yīng)的域名如下,配置服務(wù)端的host文件便于測(cè)試 # vim /etc/hosts # cat /etc/hosts 127.0.0.1 ? localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 ? ? ? ? localhost localhost.localdomain localhost6 localhost6.localdomain6 192.168.231.151 www.test151.com 192.168.231.152 www.test152.com 192.168.231.153 www.test153.com ? 第四步:建立相關(guān)的nginx的文件 1.建立虛擬主機(jī)存放網(wǎng)頁的根目錄,并創(chuàng)建首頁文件index.html # mkdir -p /usr/local/nginx/test/data/www # cd /usr/local/nginx/test/data/www # mkdir 151 # mkdir 152 # mkdir 153 # echo "192.168.2.151" > 151/index.html; echo "192.168.2.152" > 152/index.html;echo "192.168.2.153" > 153/index.html [root@localhost www]# ls 151 ?152 ?1532.修改nginx.conf,將虛擬主機(jī)配置文件包含進(jìn)主文件 ? ? #通過ip配置 server { listen 192.168.231.151:80; #server_name www.test151.com; access_log /usr/local/nginx/test/data/logs/www.test151.com.log main; error_log /usr/local/nginx/test/data/logs/www.test151.com.error.log; location / { root /usr/local/nginx/test/data/www/151; index index.html index.htm; } } server { listen 192.168.231.152:80; #server_name www.test152.com; access_log /usr/local/nginx/test/data/logs/www.test152.com.log main; error_log /usr/local/nginx/test/data/logs/www.test152.com.error.log; location / { root /usr/local/nginx/test/data/www/152; index index.html index.htm; } } server { listen 192.168.231.153:80; #server_name www.test153.com; access_log /usr/local/nginx/test/data/logs/www.test153.com.log main; error_log /usr/local/nginx/test/data/logs/www.test153.com.error.log; location / { root /usr/local/nginx/test/data/www/153; index index.html index.htm; } }客戶端訪問結(jié)果如下 可以使用ip和域名訪問。但是是使用的ip進(jìn)行匹配。 3.把server中的ip都刪除,把server_name的注釋都解開。重啟nginx。再訪問,發(fā)現(xiàn)用域名能訪問到對(duì)應(yīng)的資源。 但是使用ip就只會(huì)出現(xiàn)192.168.231.151的資源 這是因?yàn)橥ㄟ^其他ip也能請(qǐng)求到nginx,但是匹配不到相應(yīng)的server,這個(gè)時(shí)候就會(huì)使用第一個(gè)server。 4.將 server_name = www.test153.com 的server的listen加上ip 192.168.231.152:80 ,重啟nginx。 這個(gè)時(shí)候使用www.test152.com,或者192.168.231.152進(jìn)行訪問就可以得到 server_name = www.test153.com 的資源。 但是使用www.test153.com訪問的到的是151的資源 ? 所以我們得出的結(jié)論是,如果server中配置了ip,那么我們就使用客戶端帶來的ip進(jìn)行匹配,這個(gè)時(shí)候server_name失效。 ———————————————————————————————————————————— 分割線 —————————————————————————————————————————————— ? 看到評(píng)論中還有很多疑惑我來解釋下吧; 1、一臺(tái)服務(wù)器可以配置多個(gè)ip 2、一個(gè)ip可以有多個(gè)域名; 3、文中一臺(tái)虛擬機(jī)配置了多個(gè)ip是為了模擬局域網(wǎng)中其他服務(wù)器;而不是為了秀一個(gè)服務(wù)器可以配置多個(gè)ip,這樣干在實(shí)際中沒啥意義; 4、實(shí)際作中一般將?listen 后面接端口而不接ip,因?yàn)榻恿薸p后就只能通過ip匹配到相對(duì)應(yīng)的server; 5、舉例說明 server { listen 90; server_name www.test151.com; access_log /usr/local/nginx/test/data/logs/www.test151.com.log main; error_log /usr/local/nginx/test/data/logs/www.test151.com.error.log; location / { root /usr/local/nginx/test/data/www/151; index index.html index.htm; } }首先我們?cè)谄渌娔X上輸入 www.test151.com:90訪問nginx時(shí),這個(gè)時(shí)候找到的就是當(dāng)前這個(gè)server代碼;nginx所在服務(wù)器上會(huì)有配置host文件,里面會(huì)寫上www.test151.com指向的是局域網(wǎng)中的那臺(tái)機(jī)器,這個(gè)時(shí)候nginx就會(huì)將請(qǐng)求轉(zhuǎn)發(fā)到host中所配置的服務(wù)器的相應(yīng)端口中; |
今日新聞 |
推薦新聞 |
專題文章 |
CopyRight 2018-2019 實(shí)驗(yàn)室設(shè)備網(wǎng) 版權(quán)所有 |