栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > 服务器

深入浅析Nginx虚拟主机

服务器 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

深入浅析Nginx虚拟主机

一 虚拟主机 1.1 虚拟主机概念

 对于Nginx而言,每一个虚拟主机相当于一个在同一台服务器中却相互独立的站点,从而实现一台主机对外提供多个 web 服务,每个虚拟主机之间是独立的,互不影响的。

1.2 虚拟主机类型

 通过 Nginx 可以实现虚拟主机的配置,Nginx 支持三种类型的虚拟主机配置:

  •  基于 IP 的虚拟主机(较少使用)
  •  基于域名的虚拟主机
  •  基于端口的虚拟主机
二 基于IP虚拟主机 2.1 配置多IP地址
 [root@nginx ~]# ifconfig eth0:0 172.24.8.70 broadcast 172.24.8.255 netmask 255.255.255.0
 [root@nginx ~]# ip addr | grep 172
 inet 172.24.8.71/24 brd 172.24.8.255 scope global noprefixroute eth0
 inet 172.24.8.72/24 brd 172.24.8.255 scope global secondary eth0:0

 提示:如上在同一台主机添加多个IP地址。

2.2 创建站点目录
 [root@nginx ~]# mkdir /usr/share/nginx/ipvhost01/
 [root@nginx ~]# mkdir /usr/share/nginx/ipvhost02/
 [root@nginx ~]# echo 'Ipvhost01' > /usr/share/nginx/ipvhost01/index.html
 [root@nginx ~]# echo 'Ipvhost02' > /usr/share/nginx/ipvhost02/index.html
2.3 配置虚拟主机
 [root@nginx ~]# vi /etc/nginx/conf.d/ipvhost.conf
 server {
 listen ; #监听端口
 server_name ipvhost.odocker.com ...; #配置虚拟主机名和IP
 location / {
 root /usr/share/nginx/ipvhost; #请求匹配路径
 index index.html; #指定主页
 access_log /var/log/nginx/ipvhost.access.log main;
 error_log /var/log/nginx/ipvhost.error.log warn;
 }
 }
 server {
 listen ;
 server_name ipvhost.odocker.com ...;
 location / {
 root /usr/share/nginx/ipvhost;
 index index.html;
 access_log /var/log/nginx/ipvhost.access.log main;
 error_log /var/log/nginx/ipvhost.error.log warn;
 }
 }
[root@nginx ~]# nginx -t -c /etc/nginx/nginx.conf #检查配置文件
 [root@nginx ~]# nginx -s reload #重载配置文件
2.4 确认验证

 浏览器访问:ipvhost01.odocker.com。
 clipboard
 浏览器访问:ipvhost02.odocker.com。
 clipboard

三 基于域名虚拟主机 3.1 创建站点目录
 [root@nginx ~]# mkdir /usr/share/nginx/webvhost01/
 [root@nginx ~]# mkdir /usr/share/nginx/webvhost02/
 [root@nginx ~]# echo 'Webvhost01' > /usr/share/nginx/webvhost01/index.html
 [root@nginx ~]# echo 'Webvhost02' > /usr/share/nginx/webvhost02/index.html
3.2 配置虚拟主机
 [root@nginx ~]# vi /etc/nginx/conf.d/webvhost.conf
 server {
 listen ;
 server_name webvhost.odocker.com;
 location / {
 root /usr/share/nginx/webvhost;
 index index.html;
 access_log /var/log/nginx/webvhost.access.log main;
 error_log /var/log/nginx/webvhost.error.log warn;
 }
 }
 server {
 listen ;
 server_name webvhost.odocker.com;
 location / {
 root /usr/share/nginx/webvhost;
 index index.html;
 access_log /var/log/nginx/webvhost.access.log main;
 error_log /var/log/nginx/webvhost.error.log warn;
 }
 }
[root@nginx ~]# nginx -t -c /etc/nginx/nginx.conf #检查配置文件
 [root@nginx ~]# nginx -s reload #重载配置文件
3.3 确认验证

 浏览器访问:webvhost01.odocker.com。
 clipboard
 浏览器访问:webvhost02.odocker.com。
 clipboard

四 基于端口虚拟主机 4.1 创建站点目录
[root@nginx ~]# mkdir /usr/share/nginx/portvhost01/
 [root@nginx ~]# mkdir /usr/share/nginx/portvhost02/
 [root@nginx ~]# echo 'Portvhost01' > /usr/share/nginx/portvhost01/index.html
 [root@nginx ~]# echo 'Portvhost01' > /usr/share/nginx/portvhost02/index.html
4.2 配置虚拟主机
 [root@nginx ~]# vi /etc/nginx/conf.d/portvhost.conf
 server {
 listen ;
 server_name portvhost.odocker.com;
 location / {
 root /usr/share/nginx/portvhost;
 index index.html;
 access_log /var/log/nginx/portvhost.access.log main;
 error_log /var/log/nginx/portvhost.error.log warn;
 }
 }
 server {
 listen ;
 server_name portvhost.odocker.com;
 location / {
 root /usr/share/nginx/portvhost;
 index index.html;
 access_log /var/log/nginx/access_portvhost.log main;
 }
 }
 [root@nginx ~]# nginx -t -c /etc/nginx/nginx.conf #检查配置文件
 [root@nginx ~]# nginx -s reload #重载配置文件
4.3 确认验证

 浏览器访问:portvhost01.odocker.com:8080
 clipboard
 浏览器访问:portvhost02.odocker.com:8081
 clipboard

到此这篇关于Nginx虚拟主机的文章就介绍到这了,更多相关Nginx虚拟主机内容请搜索考高分网以前的文章或继续浏览下面的相关文章希望大家以后多多支持考高分网!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/177133.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号