news 2026/9/21 20:55:59

Nginx虚拟主机实验

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Nginx虚拟主机实验

文章目录

      • 实验环境
      • 一、基于域名虚拟主机配置
      • 二、基于端口虚拟主机配置
      • 三、基于主机别名配置

实验环境

安装好Nginx

[root@web01 ~]# yum -y install nginx[root@web01 nginx]# systemctl start nginx[root@web01 nginx]# systemctl enable nginxCreated symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.

一、基于域名虚拟主机配置

1)创建web站点目录

[root@web01 ~]# mkdir -p /soft/code/{www,bbs}[root@web01 ~]# echo "www" > /soft/code/www/index.html[root@web01 ~]# echo "bbs" > /soft/code/bbs/index.html

2)配置不同域名的虚拟主机

[root@web01 conf.d]# cat bbs.confserver{listen80;server_name bbs.server.com;root /soft/code/bbs;index index.html;}[root@web01 conf.d]# cat www.confserver{listen80;server_name www.server.com;root /soft/code/www;index index.html;}

3)主机映射

[root@web01 conf.d]# cat /etc/hosts10.0.0.7 web01 www.server.com bbs.server.com

4)访问域名

[root@web01 bbs]# curl www.server.comwww[root@web01 bbs]# curl bbs.server.combbs 如果不管怎么访问都是默认的网页可以检查一下主配置文件(/etc/nginx/nginx.conf)中 必须包含include /etc/nginx/conf.d/*.conf;(加载conf.d目录下的所有.conf文件),否则你的虚拟主机文件不会被加载

二、基于端口虚拟主机配置

让同一台 Nginx 服务器通过不同端口,如<font style="color:rgba(0, 0, 0, 0.85);">8080</font><font style="color:rgba(0, 0, 0, 0.85);">8081</font>返回不同页面

1)创建站点目录

[root@web01 bbs]# mkdir -p /usr/share/nginx/test8080[root@web01 bbs]# mkdir -p /usr/share/nginx/test8081[root@web01 bbs]# echo '<h1>Welcome to Port 8080!</h1>' > /usr/share/nginx/test8080/index.html[root@web01 bbs]# echo '<h1>Welcome to Port 8081!</h1>' > /usr/share/nginx/test8081/index.html

2)修改配置文件

[root@web01 bbs]# cat /etc/nginx/conf.d/8080.confserver{listen8080;# 核心:监听8080端口(非默认80端口)server_name _;# 无需域名,用_匹配所有域名/IP# 8080端口对应的站点根目录root /usr/share/nginx/test8080;# 索引页(默认访问index.html)index index.html index.htm;}[root@web01 bbs]# cat /etc/nginx/conf.d/8081.confserver{listen8081;# 核心:监听8081端口server_name _;# 8081端口对应的站点根目录root /usr/share/nginx/test8081;index index.html index.htm;}[root@web01 bbs]# nginx -tnginx: the configurationfile/etc/nginx/nginx.conf syntax is ok nginx: configurationfile/etc/nginx/nginx.conftestis successful[root@web01 bbs]# systemctl restart nginx

3)访问不同端口

[root@web01 bbs]# curl 127.0.0.1:8080<h1>Welcome to Port8080!</h1>[root@web01 bbs]# curl 127.0.0.1:8081<h1>Welcome to Port8081!</h1>

三、基于主机别名配置

实现用户访问多个域名对应同一个网站, 比如用户访问 www.server.com 和访问 server.com 内容一致

1)创建别名对应的站点目录和测试页面

[root@web01 bbs]# mkdir -p /usr/share/nginx/test_main[root@web01 bbs]# echo "<h1>Welcome to Main Site! (www.test.com / test.com / abc.test.com)</h1>" > /usr/share/nginx/test_main/index.html

2)配置 Nginx 虚拟主机别名

[root@web01 bbs]# cat /etc/nginx/conf.d/vhosts_alias.confserver{listen80;# 核心:配置主域名+多个别名(空格分隔)server_name www.test.com test.com abc.test.com;# 指向主站点目录root /usr/share/nginx/test_main;index index.html index.htm;}[root@web01 bbs]# nginx -tnginx: the configurationfile/etc/nginx/nginx.conf syntax is ok nginx: configurationfile/etc/nginx/nginx.conftestis successful[root@web01 bbs]# systemctl restart nginx

3)配置hosts文件

[root@web01 bbs]# cat /etc/hosts127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain610.0.0.5 lb0110.0.0.6 lb0210.0.0.7 web01 www.server.com bbs.server.com www.test.com test.com abc.test.com

3)访问不同域名看看内容是否一致

[root@web01 ~]# curl www.test.com<h1>Welcome to Main Site!(www.test.com / test.com / abc.test.com)</h1>[root@web01 ~]# curl test.com<h1>Welcome to Main Site!(www.test.com / test.com / abc.test.com)</h1>[root@web01 ~]# curl abc.test.com<h1>Welcome to Main Site!(www.test.com / test.com / abc.test.com)</h1>
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/9/21 22:04:51

【稀缺技术揭秘】:资深工程师都在用的Azure量子作业状态诊断流程

第一章&#xff1a;Azure量子作业的状态查询在使用 Azure Quantum 进行量子计算开发时&#xff0c;提交作业后对其执行状态的监控是关键环节。由于量子计算任务通常在远程硬件上异步执行&#xff0c;开发者必须通过 API 查询作业的当前状态&#xff0c;以判断其是否完成、失败或…

作者头像 李华
网站建设 2026/9/22 1:37:55

免费视频增强神器:字节跳动SeedVR2让老视频秒变4K超清画质

免费视频增强神器&#xff1a;字节跳动SeedVR2让老视频秒变4K超清画质 【免费下载链接】SeedVR-7B 项目地址: https://ai.gitcode.com/hf_mirrors/ByteDance-Seed/SeedVR-7B 还在为模糊不清的老视频而烦恼吗&#xff1f;那些记录着珍贵回忆的毕业旅行、家庭聚会视频&am…

作者头像 李华
网站建设 2026/9/21 23:35:08

VSCode Azure QDK 调试失败?(90%开发者都忽略的配置细节)

第一章&#xff1a;VSCode Azure QDK 调试失败&#xff1f;常见现象与根本原因在使用 Visual Studio Code 搭配 Azure Quantum Development Kit&#xff08;QDK&#xff09;进行量子程序开发时&#xff0c;调试失败是开发者常遇到的问题。尽管环境配置看似完整&#xff0c;但运…

作者头像 李华
网站建设 2026/9/21 20:16:52

hsweb-framework Easy-ORM终极指南:企业级数据访问实战手册

hsweb-framework Easy-ORM终极指南&#xff1a;企业级数据访问实战手册 【免费下载链接】hsweb-framework hsweb (haʊs wɛb) 是一个基于spring-boot 2.x开发 ,首个使用全响应式编程的企业级后台管理系统基础项目。 项目地址: https://gitcode.com/gh_mirrors/hs/hsweb-fram…

作者头像 李华
网站建设 2026/9/22 5:36:38

8、量子计算与技术发展:从理论根源到实际应用

量子计算与技术发展:从理论根源到实际应用 一、量子力学的理论根源与基础概念 1.1 量子力学基础的奠定 量子力学的发展在 1900 - 1930 年间经历了创造性的爆发、混乱与冲突。1927 年的第五届索尔维会议将相关辩论推向高潮,此次会议聚焦于量子力学。1930 年,著名科学家保罗…

作者头像 李华
网站建设 2026/9/21 17:47:01

30、RTA API 详解:功能、使用与错误处理

RTA API 详解:功能、使用与错误处理 1. TBLDEF 结构定义 TBLDEF 结构用于定义表的相关信息,其代码如下: int ncol; /** Save file. Path and name of a file which stores* the non-volatile part of the table. The file has* all of the UPDATE statements ne…

作者头像 李华