Nginx虚拟主机实验

文章目录

实验环境

安装好Nginx

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

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

1)创建web站点目录

bash 复制代码
[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)配置不同域名的虚拟主机

bash 复制代码
[root@web01 conf.d]# cat bbs.conf
server {
 listen 80;
 server_name bbs.server.com;
 root /soft/code/bbs;
 index index.html;
}


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

3)主机映射

bash 复制代码
[root@web01 conf.d]# cat /etc/hosts
10.0.0.7 web01 www.server.com bbs.server.com

4)访问域名

bash 复制代码
[root@web01 bbs]# curl  www.server.com
www
[root@web01 bbs]# curl  bbs.server.com
bbs

如果不管怎么访问都是默认的网页可以检查一下主配置文件(/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)创建站点目录

bash 复制代码
[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)修改配置文件

bash 复制代码
[root@web01 bbs]# cat /etc/nginx/conf.d/8080.conf
server {
    listen       8080;  # 核心:监听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.conf
server {
    listen       8081;  # 核心:监听8081端口
    server_name  _;

    # 8081端口对应的站点根目录
    root   /usr/share/nginx/test8081;
    index  index.html index.htm;
}


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

3)访问不同端口

bash 复制代码
[root@web01 bbs]# curl 127.0.0.1:8080
<h1>Welcome to Port 8080!</h1>
[root@web01 bbs]# curl 127.0.0.1:8081
<h1>Welcome to Port 8081!</h1>

三、基于主机别名配置

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

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

bash 复制代码
[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 虚拟主机别名

bash 复制代码
[root@web01 bbs]# cat /etc/nginx/conf.d/vhosts_alias.conf
server {
    listen       80;
    # 核心:配置主域名+多个别名(空格分隔)
    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 -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 bbs]# systemctl restart nginx

3)配置hosts文件

bash 复制代码
[root@web01 bbs]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
10.0.0.5 lb01
10.0.0.6 lb02
10.0.0.7 web01 www.server.com bbs.server.com www.test.com test.com abc.test.com

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

bash 复制代码
[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>
相关推荐
深圳市爱派派智能科技有限公司4 分钟前
“进迭时空 RISC-V 集群服务器 CSB1-N10SPK3:10 节点 K3,600 TOPS 绿色算力“
运维·服务器·risc-v·k3·进迭时空·集群服务器
☆凡尘清心☆4 分钟前
Linux运维故障排查速查命令清单
linux·运维·服务器
极客先躯8 分钟前
高级java每日一道面试题-2026年05月11日-实战篇[Docker]-如何容器化金融产品推荐系统?
java·运维·docker·容器·金融·高级面试·金融产品推荐系统
nvd1111 分钟前
GCP L4 Passthrough 负载均衡器“假死超时”深度排查复盘
运维·php·负载均衡
qeen8721 分钟前
【Linux】make/Makefile 自动化工具的介绍
linux·运维·服务器·自动化
朱容zr33313325 分钟前
为什么推荐使用自增主键?使用UUID作为主键的优缺点是什么?
java·运维·数据库·后端·mysql·面试·性能优化
HXDGCL25 分钟前
东莞市华创力科技:专业环形导轨工厂,助力自动化产线升级
运维·科技·自动化
艾莉丝努力练剑1 小时前
【Linux:动静态库】Linux 动静态库与可执行文件
linux·运维·服务器·学习·面试·文件系统·动静态库
nvd111 小时前
GCP 4层 外部网络负载均衡器深度解析
运维·网络·负载均衡
殷忆枫1 小时前
Linux 4G模块驱动适配实战:从手动绑定到自动识别
linux·运维·服务器