-
所谓虚拟主机,在 Web 服务里就是一个独立的网站站点,这个站点对应独立的域名(也可能是IP 或端口),具有独立的程序及资源,可以独立地对外提供服务供用户访问。
-
在 Nginx 中,使用一个 server{} 标签来标识一个虚拟主机,一个 Web 服务里可以有多个虚拟主机标签对,即可以同时支持多个虚拟主机站点。
-
虚拟主机有三种类型:基于域名的虚拟主机、基于端口的虚拟主机、基于 IP 的虚拟主机。
域名的虚拟主机是生产环境中最常用的。
注意点:请求的时候必须用域名,不能用ip地址访问
工作流程:
1、用请求某个url地址,地址中必然包含了域名
2、请求头送给nginx后,nginx解包分析,根据不同的域名匹配下面不同的server_name字段
所以,如果你是本地测试,你必须在hosts文件里添加解析记录,用域名访问,让请求头里带着域名,nginx才能正常解析到
windows下hosts文件路径:C:\Windows\System32\drivers\etc\hosts
例如:
| 192.168.71.15 www.egonlin.com bbs.egonlin.com blog.egonlin.com |
配置如下
| [root@localhost conf] |
| worker_processes 1; |
| events { |
| worker_connections 1024; |
| } |
| http { |
| include mime.types; |
| default_type application/octet-stream; |
| sendfile on; |
| keepalive_timeout 65; |
| server { |
| listen 80; |
| server_name www.egonlin.com; |
| location / { |
| root /usr/share/nginx/html/www; |
| index index.html index.htm; |
| } |
| } |
| |
| server { |
| listen 80; |
| server_name bbs.egonlin.com; |
| location / { |
| root /usr/share/nginx/html/bbs; |
| index index.html index.htm; |
| } |
| } |
| |
| server { |
| listen 80; |
| server_name blog.egonlin.com; |
| location / { |
| root /usr/share/nginx/html/blog; |
| index index.html index.htm; |
| } |
| } |
随着虚拟主机越来越多,所有server都放置在一个文件中并不方便管理,我们可以进行拆分
1、将每个虚拟主机配置成单独的文件,放在统一目录中(如:/etc/nginx/vhosts)
2、然后在主配置文件/etc/nginx/nginx.conf中include引入/etc/nginx/vhosts/*.conf
| mkdir -p /etc/nginx/vhosts |
在主配置文件中引入
| [root@web01 ~] |
| ...... |
| http { |
| include mime.types; |
| default_type application/octet-stream; |
| sendfile on; |
| keepalive_timeout 65; |
| ...... |
| include /etc/nginx/conf.d/*.conf; |
| include /etc/nginx/vhosts/*.conf; |
| } |
拆分成单独的配置文件
| |
| cat > /etc/nginx/vhosts/www.egonlin.com.conf << EOF |
| server { |
| listen 80; |
| server_name www.egonlin.com; |
| location / { |
| root /usr/share/nginx/html/www; |
| index index.html index.htm; |
| } |
| } |
| EOF |
| |
| |
| cat > /etc/nginx/vhosts/bbs.egonlin.com.conf << EOF |
| server { |
| listen 80; |
| server_name bbs.egonlin.com; |
| location / { |
| root /usr/share/nginx/html/bbs; |
| index index.html index.htm; |
| } |
| } |
| EOF |
| |
| |
| cat > /etc/nginx/vhosts/blog.egonlin.com.conf << EOF |
| server { |
| listen 80; |
| server_name blog.egonlin.com; |
| location / { |
| root /usr/share/nginx/html/blog; |
| index index.html index.htm; |
| } |
| } |
| EOF |
重新加载配置(不能随意重启)
为每个站点创建好目录,与测试文件
| mkdir /usr/share/nginx/html/www |
| echo www > /usr/share/nginx/html/www/index.html |
| |
| mkdir /usr/share/nginx/html/bbs |
| echo bbs > /usr/share/nginx/html/bbs/index.html |
| |
| mkdir /usr/share/nginx/html/blog |
| echo blog > /usr/share/nginx/html/blog/index.html |
测试验证:
如果你是本地测试,你必须在hosts文件里添加解析记录,用域名访问,让请求头里带着域名,nginx才能正常解析到
windows下hosts文件路径:C:\Windows\System32\drivers\etc\hosts
注意:必须用域名访问(如果谷歌浏览器有问题,请尝试用火狐)
基于端口的虚拟主机生产环境不多见,只需要修改主机监听端口就可以了,域名相同也可以,因为基于端口的虚拟主机就是他通过端口来唯一分区不通的虚拟主机的,只要端口不同就是不同的虚拟主机。
注意:请求的时候关键是端口的不同,至于你用域名还是ip地址并不重要
配置文件
| |
| cat > /etc/nginx/vhosts/www.egonlin.com.conf << EOF |
| server { |
| listen 8080; |
| server_name www.egonlin.com; |
| location / { |
| root /usr/share/nginx/html/www; |
| index index.html index.htm; |
| } |
| } |
| EOF |
| |
| |
| cat > /etc/nginx/vhosts/bbs.egonlin.com.conf << EOF |
| server { |
| listen 8081; |
| server_name bbs.egonlin.com; |
| location / { |
| root /usr/share/nginx/html/bbs; |
| index index.html index.htm; |
| } |
| } |
| EOF |
| |
| |
| cat > /etc/nginx/vhosts/blog.egonlin.com.conf << EOF |
| server { |
| listen 8082; |
| server_name blog.egonlin.com; |
| location / { |
| root /usr/share/nginx/html/blog; |
| index index.html index.htm; |
| } |
| } |
| EOF |
测试(你用域名也可以,只要能解析到192.168.71.15就行,关键是端口的不同)
| [root@web01 /etc/nginx/vhosts] |
| www |
| [root@web01 /etc/nginx/vhosts] |
| bbs |
| [root@web01 /etc/nginx/vhosts] |
| blog |
| [root@web01 /etc/nginx/vhosts] |
基于 IP 的虚拟主机在生产环境中不常使用,只需要将基于域名的虚拟主机中的域名修改为 IP 就可以了,前提是服务器有多个IP地址。如果需要不同的 IP 对应不同的服务,可在网站前端的负载均衡器上配置。
虚拟主机别名,就是为虚拟主机设置除了主域名以外的一个或多个域名名字,这样就能实现用户访问的多个域名对应同一个虚拟主机网站的功能。
| [root@localhost conf] |
| server { |
| listen 80; |
| server_name www.egonlin.com egonlin.com; |
| location / { |
| root /usr/share/nginx/html/www; |
| index index.html index.htm; |
| } |
| } |