在一臺(tái)服務(wù)器上,訪問(wèn)不同的網(wǎng)站
通常有兩種區(qū)分方式:
1.通過(guò)監(jiān)聽(tīng)的端口號(hào)
2.通過(guò)域名
1.通過(guò)端口訪問(wèn)不同的主機(jī):
nginx的配置文件:
/usr/local/nginx/conf/nginx.conf
centos文件默認(rèn)編碼格式 latin1
查看編碼格式的命令: :set fileencoding
#user?nobody; worker_processes?1; #error_log?logs/error.log; #error_log?logs/error.log?notice; #error_log?logs/error.log?info; #pid????logs/nginx.pid; events?{ ??worker_connections?1024; } ##一個(gè)http節(jié)點(diǎn) http?{?? ??include????mime.types; ??default_type?application/octet-stream; ??#log_format?main?'$remote_addr?-?$remote_user?[$time_local]?"$request"?' ??#?????????'$status?$body_bytes_sent?"$http_referer"?' ??#?????????'"$http_user_agent"?"$http_x_forwarded_for"'; ??#access_log?logs/access.log?main; ??sendfile????on; ??#tcp_nopush???on; ??#keepalive_timeout?0; ??keepalive_timeout?65; ??#gzip?on; ?#server?節(jié)點(diǎn),即?你需要訪問(wèn)網(wǎng)站的配置 ?#一個(gè)server節(jié)點(diǎn),就是一個(gè)虛擬主機(jī) ??server?{ ????listen????80;??#監(jiān)聽(tīng)的端口號(hào),訪問(wèn)網(wǎng)站?默認(rèn)是80端口 ????server_name?localhost;??#即訪問(wèn)的域名 ????#charset?koi8-r; ????#access_log?logs/host.access.log?main; ????location?/?{??#定位 ??????root??html;??#定位的是nginx根目錄下的?html文件夾 ??????index?index.html?index.htm;??#設(shè)置網(wǎng)站首頁(yè) ????} ??} }
此時(shí) 可以配置多個(gè)server,也就是配置了不同的主機(jī)
添加虛擬主機(jī):(通過(guò)端口號(hào) 區(qū)別)
server?{ ????listen????81; ????server_name?localhost; ????#charset?koi8-r; ????#access_log?logs/host.access.log?main; ????location?/?{ ??????root??html-81; ??#nginx根目錄下?新建的html81?文件夾 ??????index?index.html?index.htm; ????`` ??}
編輯好文件之后,我們重新加載配置文件
通過(guò)命令: ./nginx -s reload
效果:
我們知道,當(dāng)一個(gè)服務(wù)器上配置多個(gè)網(wǎng)站時(shí),我們不可能通過(guò)端口號(hào)來(lái)區(qū)分它們,所以接下來(lái) 我需要通過(guò)域名來(lái)區(qū)分
2.通過(guò)域名區(qū)分不同的虛擬主機(jī)
什么是域名??
域名就是網(wǎng)址
例如:www.baidu.com
通常我們?cè)谠L問(wèn)域名的時(shí)候,我們需要通過(guò)dns服務(wù)器解析域名
dns服務(wù)器:把域名解析為ip地址。保存的就是域名和ip的映射關(guān)系。
一個(gè)域名對(duì)應(yīng)一個(gè)ip地址,一個(gè)ip地址可以被多個(gè)域名綁定。
本地測(cè)試可以修改hosts文件。
修改window的hosts文件:(c:windowssystem32driversetc)
可以配置域名和ip的映射關(guān)系,如果hosts文件中配置了域名和ip的對(duì)應(yīng)關(guān)系,不需要走dns服務(wù)器!!!!
在剛剛的nginx.conf文件下 繼續(xù)配置:
server?{ ????listen????80; ????server_name?www.taobao.com; ????#charset?koi8-r; ????#access_log?logs/host.access.log?main; ????location?/?{ ??????root??html-taobao; ??????index?index.html?index.htm; ????} ??} ??server?{ ????listen????80; ????server_name?www.baidu.com; ????#charset?koi8-r; ????#access_log?logs/host.access.log?main; ????location?/?{ ??????root??html-baidu; ??????index?index.html?index.htm; ????} ??} }
域名的配置:
192.168.25.148 www.test.com
192.168.25.148 www.yiyou.com
重啟nginx服務(wù)
觀察下效果: