因为IP地址有限,因此经常存在多个主机域名对应着同一个IP地址的情况,可以通过配置虚拟主机来解决这个问题。
在nginx.conf中,每个server块就是一个虚拟主机,它只会处理与其server_name配置项对应的主机域名请求。
1.建立站点目录
mkdir /usr/local/nginx/website1 mkdir /usr/local/nginx/website2
2.建立存放日志的目录
mkdir /var/log/nginx/website1 mkdir /var/log/nginx/website2
3.建立测试页
echo "This is website1" > /usr/local/nginx/website1/index.html echo "This is website2" > /usr/local/nginx/website2/index.html
4.修改nginx.conf配置文件,添加两个server节点(我的主机IP时192.168.11.98)
server{ listen 192.168.11.99:80; server_name www.website1.com; #charset koi8-r; access_log /var/log/nginx/website1/access.log; error_log /var/log/nginx/website1/error.log; location / { root /usr/local/nginx/website1; index index.html index.htm; } }
server{ listen 192.168.11.99:80; server_name www.website2.com; #charset koi8-r; access_log /var/log/nginx/website2/access.log; error_log /var/log/nginx/website2/error.log; location / { root /usr/local/nginx/website2; index index.html index.htm; } }
5.重启nginx服务
/usr/local/nginx/sbin/nginx -s reload
6.修改windows下的hosts文件(C:\Windows\System32\drivers\etc\hosts),添加如下一行,最后键入回车
192.168.11.98 www.website1.com www.website2.com
7.测试。在浏览器中分别输入www.website1.com跟www.website2.com。