什么是虚拟主机?
虚拟主机是一种特殊的软硬件技术,它可以将网络上的每一台计算机分成多个虚拟主机,每个虚拟主机可以独立对外提供www服务,这样就可以实现一台主机对外提供多个web服务,每个虚拟主机之间是独立的,互不影响。
![](https://img.haomeiwen.com/i20499864/e861ba6ebcedb303.png)
nginx可以实现虚拟主机的配置,nginx支持三种类型的虚拟主机配置。
1、基于域名的虚拟主机 (server_name来区分虚拟主机——应用:外部网站)
2、基于ip的虚拟主机, (一块主机绑定多个ip地址)
3、基于端口的虚拟主机 (端口来区分虚拟主机——应用:公司内部网站,外部网站的管理后台)
1、 基于域名的虚拟主机**
1、配置通过域名区分的虚拟机
[root@localhost ~]# cat /etc/nginx/nginx.conf
worker_processes 4;
#error_log logs/error.log;
worker_rlimit_nofile 102400;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
server {
listen 80;
server_name web.testpm.com;
location / {
root /var/www/nginx/;
index index.html index.htm;
limit_rate 2k;
}
}
server {
listen 80;
server_name web.1000phone.com;
location / {
root /1000phone/html;
index index.html index.htm;
}
}
}
2、 为 域名为 web.1000phone.com 的虚拟机,创建 index 文件
[root@localhost ~]# mkdir -p /1000phone/html
[root@localhost ~]# vim /1000phone/html/index.html
<html>
<p>
this is my 1000phone
</p>
</html>
3、重新加载配置文件
# 如果编译安装的执行
[root@nginx]# /usr/local/nginx/sbin/nginx -s reload
# 如果 yum 安装的执行
[root@nginx]# nginx -s reload
4、客户端配置路由映射
在 C:\Windows\System32\drivers\etc\hosts 文件中添加两行(linux:/etc/hosts)
10.0.105.199 web.testpm.com
10.0.105.199 web.1000phone.com
5、 测试访问
浏览器输入:http://web.testpm.com/
网友评论