一、什么是虚拟主机:
在服务器中,我们可以指定多个虚拟主机,一般来说,一个虚拟主机即为一个网站,此虚拟主机会监听此电脑的指定端口号以及到此端口的请求的域名。
二、虚拟主机的配置:
1.listen与server_name:
一个虚拟主机的经典配置是(注意nginx的server是写在http中的):
server {
listen 8081;
server_name localhost;
location / {
root html;
index index.html;
}
}
此为创建一个虚拟主机,其监听的是8081端口,以及域名为localhost。
可能会有疑问,什么叫域名为localhost呢?其实是这样,比如一个机器可能要有几个虚拟主机,其都要监听80端口(因为网页默认就是80端口),想要happyheng.com访问一个虚拟主机,doc.happyheng.com访问另外一个虚拟主机,就可以这样
server {
listen 80;
server_name happyheng.com;
location / {
xxx
}
}
server {
listen 80;
server_name doc.happyheng.com;
location / {
xxx
}
}
因为nginx会得到http头部的域名,将其映射到对应的虚拟主机上。
还有比如:
server {
listen 8081;
server_name localhost;
location / {
root html;
index index.html;
}
}
server {
listen 8081;
server_name localhost111;
location / {
root html;
index index11.html;
}
}
我将本机的hosts增加
127.0.0.1 localhost111
那么访问localhost111:8081的时候,即访问的是第二个server,主页是index11.html
2.location参数:
2.1.root与index参数:
location参数即为访问位置对应的返回结果,比如上述
location / {
root html;
index index.html;
}
其直接访问域名时,即localhost:8081访问的的即是这个location,其目录为html目录,主页是index.html页面,即 当访问localhost:8081时,其返回的主页面是nginx安装目录的html目录下面的index.html页面
3.proxy_pass反向代理参数:
3.1.正向代理与反向代理:
我们在使用vpn的时候,即使用的是正向代理,比如我要访问www.google.com,其实访问的是 代理服务器,代理服务器在去访问www.google.com。在正向代理中,我们是知道最终访问的服务器的域名的。
而在反向代理中,我们直接访问的是代理服务器,代理服务器在将我们请求转发给最终的服务器得到最终的结果,然后在返回回来,在这里,我们不知道最终是哪个服务器提供了服务。
3.2.反向代理的配置:
在nginx中,我们可以这样配置反向代理:
server {
listen 9010;
server_name localhost;
location / {
proxy_pass http://127.0.0.1:11000;
}
}
server {
listen 11000;
server_name localhost;
location / {
root html;
index index11000.html;
}
}
在服务器中,我们可以指定多个虚拟主机,一般来说,一个虚拟主机即为一个网站,此虚拟主机会监听此电脑的指定端口号以及到此端口的请求的域名。
二、虚拟主机的配置:
1.listen与server_name:
一个虚拟主机的经典配置是(注意nginx的server是写在http中的):
server {
listen 8081;
server_name localhost;
location / {
root html;
index index.html;
}
}
此为创建一个虚拟主机,其监听的是8081端口,以及域名为localhost。
可能会有疑问,什么叫域名为localhost呢?其实是这样,比如一个机器可能要有几个虚拟主机,其都要监听80端口(因为网页默认就是80端口),想要happyheng.com访问一个虚拟主机,doc.happyheng.com访问另外一个虚拟主机,就可以这样
server {
listen 80;
server_name happyheng.com;
location / {
xxx
}
}
server {
listen 80;
server_name doc.happyheng.com;
location / {
xxx
}
}
因为nginx会得到http头部的域名,将其映射到对应的虚拟主机上。
还有比如:
server {
listen 8081;
server_name localhost;
location / {
root html;
index index.html;
}
}
server {
listen 8081;
server_name localhost111;
location / {
root html;
index index11.html;
}
}
我将本机的hosts增加
127.0.0.1 localhost111
那么访问localhost111:8081的时候,即访问的是第二个server,主页是index11.html
2.location参数:
2.1.root与index参数:
location参数即为访问位置对应的返回结果,比如上述
location / {
root html;
index index.html;
}
其直接访问域名时,即localhost:8081访问的的即是这个location,其目录为html目录,主页是index.html页面,即 当访问localhost:8081时,其返回的主页面是nginx安装目录的html目录下面的index.html页面
3.proxy_pass反向代理参数:
3.1.正向代理与反向代理:
我们在使用vpn的时候,即使用的是正向代理,比如我要访问www.google.com,其实访问的是 代理服务器,代理服务器在去访问www.google.com。在正向代理中,我们是知道最终访问的服务器的域名的。
而在反向代理中,我们直接访问的是代理服务器,代理服务器在将我们请求转发给最终的服务器得到最终的结果,然后在返回回来,在这里,我们不知道最终是哪个服务器提供了服务。
3.2.反向代理的配置:
在nginx中,我们可以这样配置反向代理:
server {
listen 9010;
server_name localhost;
location / {
proxy_pass http://127.0.0.1:11000;
}
}
server {
listen 11000;
server_name localhost;
location / {
root html;
index index11000.html;
}
}
这样,在访问localhost:9010的时候nginx就会请求http://127.0.0.1:11000来得到最终的结果。