在一臺服務器上,訪問不同的網站
通常有兩種區分方式:
1.通過監聽的端口號
2.通過域名
1.通過端口訪問不同的主機:
Nginx的配置文件:
/usr/local/nginx/conf/nginx.conf
Centos文件默認編碼格式 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;
}
##一個http節點
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 節點,即 你需要訪問網站的配置
#一個server節點,就是一個虛擬主機
server {
listen 80; #監聽的端口號,訪問網站 默認是80端口
server_name localhost; #即訪問的域名
#charset koi8-r;
#access_log logs/host.access.log main;
location / { #定位
root html; #定位的是nginx根目錄下的 html文件夾
index index.html index.htm; #設置網站首頁
}
}
}
此時 可以配置多個server,也就是配置了不同的主機
添加虛擬主機:(通過端口號 區別)
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;
``
}
編輯好文件之後,我們重新加載配置文件
通過命令: ./nginx -s reload
效果:
我們知道,當一個服務器上配置多個網站時,我們不可能通過端口號來區分它們,所以接下來 我需要通過域名來區分
2.通過域名區分不同的虛擬主機
什麼是域名??
域名就是網址
例如:www.baidu.com
通常我們在訪問域名的時候,我們需要通過dns服務器解析域名
Dns服務器:把域名解析爲ip地址。保存的就是域名和ip的映射關係。
一個域名對應一個ip地址,一個ip地址可以被多個域名綁定。
本地測試可以修改hosts文件。
修改window的hosts文件:(C:\Windows\System32\drivers\etc)
可以配置域名和ip的映射關係,如果hosts文件中配置了域名和ip的對應關係,不需要走dns服務器!!!!
在剛剛的nginx.conf文件下 繼續配置:
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服務
觀察下效果: