nginx配置虚拟主机的详细步骤


Posted in Servers onJuly 21, 2021

虚拟主机使用的是特殊的软硬件技术,它把一台运行在因特网上的服务器主机分成一台台“虚拟”的主机,每台虚拟主机都可以是一个独立的网站,可以具有独立的域名,具有完整的Intemet服务器功能(WWW、FTP、Email等),同一台主机上的虚拟主机之间是完全独立的。从网站访问者来看,每一台虚拟主机和一台独立的主机完全一样。

nginx配置虚拟主机的详细步骤

利用虚拟主机,不用为每个要运行的网站提供一台单独的Nginx服务器或单独运行一组Nginx进程。虚拟主机提供了在同一台服务器、同一组Nginx进程上运行多个网站的功能。

配置虚拟主机有三种方法:

  • 基于域名的虚拟主机 : 不同的域名、相同的IP(此方式应用最广泛)
  • 基于端口的虚拟主机 : 不使用域名、IP来区分不同站点的内容,而是用不同的TCP端口号
  • 基于IP地址的虚拟主机 : 不同的域名、不同的IP ( 需要加网络接口 ,应用的不广泛) 基于IP地址

nginx配置虚拟主机的详细步骤

方式一:多网卡多IP

两个物理网卡,两个IP

# 两张物理网卡ens32和ens34
[root@nginx network-scripts]# ifconfig ens32 | awk 'NR==2 {print $2}'  
192.168.126.41

[root@nginx network-scripts]# ifconfig ens34 | awk 'NR==2 {print $2}'  
192.168.126.42

编辑配置文件,基于每个IP创建一个虚拟主机

# 为防止 /etc/nginx/conf.d/default.conf 配置文件影响,对其进行重命名
[root@nginx ~]# mv /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default	 

[root@nginx ~]# vim /etc/nginx/conf.d/ip.conf
# ens32网卡对应的虚拟主机
server {
  listen 192.168.126.41:80;

  location / {
    root /ip_ens32;
    index index.html;
  }
}

# ens34 网卡对应的虚拟主机
server {
  listen 192.168.126.42:80;

  location / {
    root /ip_ens34;
    index index.html;
  }
}

创建虚拟主机的网页文件目录及文件

[root@nginx ~]# mkdir /ip_ens32
[root@nginx ~]# mkdir /ip_ens34

[root@nginx ~]# echo "ens32" > /ip_ens32/index.html
[root@nginx ~]# echo "ens34" > /ip_ens34/index.html

检查配置文件的语法

[root@nginx ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

重载nginx服务

[root@nginx ~]# systemctl reload nginx

测试

[root@nginx ~]# curl 192.168.126.41
ens32
[root@nginx ~]# curl 192.168.126.42
ens34

nginx配置虚拟主机的详细步骤nginx配置虚拟主机的详细步骤

方式二:单网卡多IP

为一个物理网卡配置多个ip

ip addr add IP/MASK dev 网卡名

# 删除
ip addr del IP/MASK dev 网卡名

其余步骤同上面多网卡多IP的配置

基于端口

nginx配置虚拟主机的详细步骤

多使用于公司内部,无法使用域名或没有域名时

配置

[root@nginx ~]# vim /etc/nginx/conf.d/port.conf
server {
  listen 81;

  location / {
    root /port_81;
    index index.html;
  }
}

server {
  listen 82;

  location / {
    root /port_82;
    index index.html;
  }
}

[root@nginx ~]# mkdir /port_{81..82}
[root@nginx ~]# echo "81" > /port_81/index.html
[root@nginx ~]# echo "82" > /port_82/index.html

[root@nginx ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@nginx ~]# systemctl reload nginx

测试

[root@nginx ~]# curl 192.168.126.41:81
81
[root@nginx ~]# curl 192.168.126.41:82
82

nginx配置虚拟主机的详细步骤nginx配置虚拟主机的详细步骤

基于域名

nginx配置虚拟主机的详细步骤

配置

一般一个域名对应一个配置文件,便于管理

[root@nginx ~]# vim /etc/nginx/conf.d/test1.dxk.com.conf
server {
  listen 80;
  server_name test1.dxk.com;

  location / {
    root /test1;
    index index.html;
  }
}

[root@nginx ~]# vim /etc/nginx/conf.d/test2.dxk.com.conf
server {
  listen 80;
  server_name test2.dxk.com;

  location / {
    root /test2;
    index index.html;
  }
}

[root@nginx ~]# mkdir /test{1..2}
[root@nginx ~]# echo "test1" > /test1/index.html
[root@nginx ~]# echo "test2" > /test2/index.html

[root@nginx ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

[root@nginx ~]# systemctl reload nginx

测试

# 配置域名解析
[root@nginx ~]# echo -e "192.168.126.41 test1.dxk.com\n192.168.126.41 test2.dxk.com" >> /etc/hosts
[root@nginx ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.126.41 test1.dxk.com
192.168.126.41 test2.dxk.com

[root@nginx ~]# curl test1.dxk.com
test1
[root@nginx ~]# curl test2.dxk.com
test2

nginx配置虚拟主机的详细步骤
nginx配置虚拟主机的详细步骤
nginx配置虚拟主机的详细步骤

这里有个问题:

如果在配置域名解析时由于写错了,那么访问该错误域名(未配置该错误域名的虚拟主机)时竟然还会返回网页内容。

[root@nginx ~]# vim /etc/hosts
192.168.126.41 test1.dxk.com
192.168.126.41 test3.dxk.com   # 这里本应该是 test2.dxk.com ,但是由于写错了,而且对应test3.dxk.com域名的虚拟主机并不存在

访问该错误域名

[root@nginx ~]# curl test3.dxk.com
test1

# 可以看到,还是会返回网页信息

因为在配置域名解析时,虽然域名写错了,但是IP是对的,那么此时服务端默认会返回满足是该IP且端口为80的排在第一个的虚拟主机的网页信息给客户端

[root@nginx ~]# ll /etc/nginx/conf.d/
-rw-r--r--. 1 root root  112 Jul  3 21:23 test1.dxk.com.conf
-rw-r--r--. 1 root root  112 Jul  3 21:22 test2.dxk.com.conf

这是需要注意的

到此这篇关于nginx虚拟主机的文章就介绍到这了,更多相关nginx虚拟主机内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Servers 相关文章推荐
提升Nginx性能的一些建议
Mar 31 Servers
Nginx反向代理配置的全过程记录
Jun 22 Servers
Nginx内网单机反向代理的实现
Nov 07 Servers
NGINX 权限控制文件预览和下载的实现原理
Jan 18 Servers
Apache Hudi集成Spark SQL操作hide表
Mar 31 Servers
深入解析Apache Hudi内核文件标记机制
Mar 31 Servers
Kubernetes中Deployment的升级与回滚
Apr 01 Servers
阿里云日志过滤器配置日志服务
Apr 09 Servers
IIS服务器中设置HTTP重定向访问HTTPS
Apr 29 Servers
如何开启Apache,Nginx和IIS服务器的GZIP压缩功能
Apr 29 Servers
nginx配置指令之server_name的具体使用
Aug 14 Servers
Elasticsearch6.2服务器升配后的bug(避坑指南)
Sep 23 Servers
nginx的zabbix 5.0安装部署的方法步骤
nginx请求限制配置方法
使用goaccess分析nginx日志的详细方法
Jul 09 #Servers
nginx作grpc的反向代理踩坑总结
使用 Apache Superset 可视化 ClickHouse 数据的两种方法
使用nginx配置访问wgcloud的方法
Jun 26 #Servers
Nginx反向代理配置的全过程记录
You might like
php实现memcache缓存示例讲解
2013/12/04 PHP
基于PHP代码实现中奖概率算法可用于刮刮卡、大转盘等抽奖算法
2015/12/20 PHP
JavaScript Archive Network 集合
2007/05/12 Javascript
javascript 函数参数限制说明
2010/11/19 Javascript
Js获取下拉框选定项的值和文本的实现代码
2014/02/26 Javascript
jQuery给动态添加的元素绑定事件的方法
2015/03/09 Javascript
JavaScript中reduce()方法的使用详解
2015/06/09 Javascript
jQuery滚动加载图片实现原理
2015/12/14 Javascript
JS中多种方式创建对象详解
2016/03/22 Javascript
JavaScript兼容性总结之获取非行间样式案例
2016/08/07 Javascript
Bootstrap基本插件学习笔记之模态对话框(16)
2016/12/08 Javascript
bootstrap table表格客户端分页实例
2017/08/07 Javascript
关于vue项目中搜索节流的实现代码
2019/09/17 Javascript
Python 流程控制实例代码
2009/09/25 Python
Python连接mssql数据库编码问题解决方法
2015/01/01 Python
实践Python的爬虫框架Scrapy来抓取豆瓣电影TOP250
2016/01/20 Python
python中安装模块包版本冲突问题的解决
2017/05/02 Python
VSCode下配置python调试运行环境的方法
2018/04/06 Python
Python实现去除列表中重复元素的方法小结【4种方法】
2018/04/27 Python
python数据结构之线性表的顺序存储结构
2018/09/28 Python
Python面向对象之类和实例用法分析
2019/06/08 Python
详解python 利用echarts画地图(热力图)(世界地图,省市地图,区县地图)
2019/08/06 Python
tensorflow mnist 数据加载实现并画图效果
2020/02/05 Python
解决Jupyter Notebook开始菜单栏Anaconda下消失的问题
2020/04/13 Python
Python正则表达式如何匹配中文
2020/05/27 Python
Numpy 多维数据数组的实现
2020/06/18 Python
如何使用python记录室友的抖音在线时间
2020/06/29 Python
pytorch下的unsqueeze和squeeze的用法说明
2021/02/06 Python
CSS3下的渐变文字效果实现示例
2018/03/02 HTML / CSS
定义一结构体变量,用其表示点坐标,并输入两点坐标,求两点之间的距离
2015/08/17 面试题
Android笔试题总结
2014/11/29 面试题
庆七一活动方案
2014/01/25 职场文书
2015年元旦晚会活动总结(学生会)
2014/11/28 职场文书
2015国庆节宣传语
2015/07/14 职场文书
三八妇女节致辞
2015/07/31 职场文书
如何基于python实现单目三维重建详解
2022/06/25 Python