环境说明
首先,我的电脑的操作系统是windows10,IP是192.168.1.114。
然后,我分别建立的虚拟机A(ubuntu网关)和虚拟机B(ubuntu内网)。
虚拟机A的操作系统是ubuntu网卡是host-only模式网卡,IP是192.168.56.102
虚拟机B的操作系统是ubuntu有两个网卡,分别是:
桥接网卡,IP是192.168.1.103
host-only网卡,IP是192.168.56.101
使用内网模式而不是host-only模式将具有更好的代表性,但是我用的virtualbox的内网模式,网卡一直获取不了IP地址,所以只能用host-only模式测试了 =.=
分别在虚拟机A和B上安装nginx
安装nginx:sudo apt-get install nginx
nginx配置文件路径:/etc/nginx/nginx.conf
nginx运行脚本路径:/usr/sbin/nginx
nginx测试命令:/usr/sbin/nginx -t
nginx重新加载配置文件命令:/usr/sbin/nginx -s reload
在虚拟机B上配置虚拟主机
在nginx.conf中添加虚拟主机配置信息:
##
# Virtual Host Configs
##
server{
listen 80;
server_name test.nginx.com;
root /var/www;
}
然后我们在/var/www目录下建立index.html文件:
<!DOCTYPE html>
<html>
<meta charset="utf-8">
<head>
<title>hello world</title>
</head>
<body>
<p><h1>反向代理设置成功</h1></p>
<p><h1>你现在访问的是网站的真实地址</h1></p>
</body>
</html>
在虚拟机A上配置反向代理
在nginx.conf中添加反向代理配置信息:
##
# Virtual Host Configs
##
server{
listen 80;
server_name test.nginx.com;
location / {
proxy_pass http://test.nginx.com;
}
}
上面配置的意思是:当test.nginx.com域名解析ip到虚拟机A时,虚拟机A从test.nginx.com获取内容。
配置主机hosts
在c:/windows/system32/drivers/etc/hosts添加
192.168.1.103 test.nginx.com
配置虚拟机A的host
在/etc/hosts添加
192.168.56.102 test.nginx.com