nginx虚拟主机配置推荐(nginx配置虚拟主机的详细步骤)
类别:服务器 浏览量:471
时间:2021-10-01 01:03:17 nginx虚拟主机配置推荐
nginx配置虚拟主机的详细步骤虚拟主机使用的是特殊的软硬件技术,它把一台运行在因特网上的服务器主机分成一台台“虚拟”的主机,每台虚拟主机都可以是一个独立的网站,可以具有独立的域名,具有完整的Intemet服务器功能(WWW、FTP、Email等),同一台主机上的虚拟主机之间是完全独立的。从网站访问者来看,每一台虚拟主机和一台独立的主机完全一样。
利用虚拟主机,不用为每个要运行的网站提供一台单独的Nginx服务器或单独运行一组Nginx进程。虚拟主机提供了在同一台服务器、同一组Nginx进程上运行多个网站的功能。
配置虚拟主机有三种方法:- 基于域名的虚拟主机 : 不同的域名、相同的IP(此方式应用最广泛)
- 基于端口的虚拟主机 : 不使用域名、IP来区分不同站点的内容,而是用不同的TCP端口号
- 基于IP地址的虚拟主机 : 不同的域名、不同的IP ( 需要加网络接口 ,应用的不广泛) 基于IP地址
方式一:多网卡多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
方式二:单网卡多IP
为一个物理网卡配置多个ip
ip addr add IP/MASK dev 网卡名 # 删除 ip addr del IP/MASK dev 网卡名
其余步骤同上面多网卡多IP的配置
基于端口
配置
[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
一般一个域名对应一个配置文件,便于管理
[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
这里有个问题:
如果在配置域名解析时由于写错了,那么访问该错误域名(未配置该错误域名的虚拟主机)时竟然还会返回网页内容。
[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虚拟主机内容请搜索开心学习网以前的文章或继续浏览下面的相关文章希望大家以后多多支持开心学习网!
您可能感兴趣
- nginx服务器有什么用(Nginx的作用详解,为什么在Web服务器中Nginx的比例越来越高?)
- nginx 使用手册(nginx服务器的下载安装与使用详解)
- nginx配置文件的几大模块(nginx简单配置多个server的方法)
- 宝塔nginx装哪个版本(宝塔面板nginx关闭状态,启动不了解决办法)
- nginx配置目录(nginx配置文件使用环境变量的操作方法)
- nginx事件模型有几种(Python实现监控Nginx配置文件的不同并发送邮件报警功能示例)
- tomcat docker 性能(Docker Nginx容器和Tomcat容器实现负载均衡与动静分离操作)
- nginx怎么配置静态文件(Nginx 过滤静态资源文件的访问日志的实现)
- nginx宝塔配置修改(宝塔面板默认的404页面不生效怎么办Nginx?)
- 宝塔试用网站再安装nginx不起作用(宝塔面板Nginx防恶意解析图文教程)
- 如何提高nginx性能(提升Nginx性能的一些建议)
- nginx跨域怎么用(如何利用map实现Nginx允许多个域名跨域)
- linux下安装nginx常见问题(Linux安装Nginx步骤详解)
- nginx过滤http请求协议(nginx对http请求处理的各个阶段详析)
- nginx更改html内容(apache与iis下让html格式的页面也同样具有shtml的动态解析)
- nginx报错能看到错误代码(nginx提示:500 Internal Server Error错误的解决方法)
- 五代十国南唐历代国君(五代十国南唐历代国君)
- 飞机引进工程师杨隆 匠人匠心,只争朝夕(飞机引进工程师杨隆)
- 三人行,她们是育人路上的 铁三角 团队(她们是育人路上的)
- 阴阳师 孟婆山兔CP不倒 新皮肤草稿 孟婆兔 让痒痒鼠点赞(阴阳师孟婆山兔CP不倒)
- 阴阳师孟婆御魂推荐 孟婆御魂搭配毕业套(阴阳师孟婆御魂推荐)
- 袁冰妍终于接到新剧,饰演反追男主,看到合作演员 眼光果然毒辣(袁冰妍终于接到新剧)
热门推荐
- vue 组件布局(Vue实现固定底部组件的示例)
- html53d效果代码(HTML5 直播疯狂点赞动画实现代码 附源码)
- python复杂的验证码处理(Python 通过打码平台实现验证码的实现)
- mysql数据恢复时间点(MySQL 基于时间点的快速恢复方案)
- easyUI DataGrid 显示可排序的列
- css3旋转立方体效果(利用CSS实现立方体360度旋转效果实例代码)
- 海外云服务器的优势是什么?(海外云服务器的优势是什么?)
- MongoDB聚合函数
- vscode react插件(基于visual studio code + react 开发环境搭建过程)
- 怎么用python实现链表(Python3实现的判断回文链表算法示例)
排行榜
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9