随笔 - 639, 文章 - 19, 评论 - 34, 阅读 - 115万
  博客园  :: 首页  :: 管理

在nginx中基于主机名-域名-实现多个虚拟主机的配置,这种也是生产环境使用最多的一种多站点配置方式 

核心配置如下,需要配置到nginx的配置文件中的 http 段内:

复制代码
server {
     listen       80;
     server_name  www.web01.com;

     location / {
        root     /usr/share/nginx/html/www.web01.com;
        index    index.html index.htm;
     }
}


server {
     listen       80;
     server_name  www.web02.com;

     location / {
        root     /usr/share/nginx/html/www.web02.com;
        index    index.html index.htm;
     }
}
复制代码

说明:root 字段笔者是写到 location字段内的,其实写到 server 内,与 listen 和 server_name 同级也是可以的

然后可以在客户端进行测试,这里使用curl命令行客户端工具测试(下方host是不区分大小写的,笔者习惯是将首字母大写)

[root@localhost qq-5201351]# curl -H 'Host:www.web01.com'  1.1.1.1
web01
[root@localhost qq-5201351]# curl --header 'Host:www.web02.com'  1.1.1.1
web02

选项 -H, --header <header/@file> Pass custom header(s) to server

说明:无论最后指定的是IP还是其他域名,Nginx收到请求后,实质是根据Host字段,决定使用那个server段的内容进行响应

 

 

 

尊重别人的劳动成果 转载请务必注明出处:https://www.cnblogs.com/5201351/p/17716801.html

 

相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)
历史上的今天:
2021-09-20 Activate MFA报错:MFADevice entity at the same path and name already exists
点击右上角即可分享
微信分享提示