博客
关于我
LNMP架构 nginx默认虚拟主机
阅读量:803 次
发布时间:2023-02-06

本文共 1943 字,大约阅读时间需要 6 分钟。

Nginx 默认虚拟主机配置

在 Nginx 中,虚拟主机是处理多个域名或子域名请求的核心功能之一。以下将介绍如何配置默认虚拟主机以及如何为多个站点配置 Nginx。

默认虚拟主机配置

默认虚拟主机是 Nginx 最基础的配置方式。当没有定义对应的域名或子域名时,请求将自动转向默认虚拟主机。

  • 删除 nginx.conf 中的默认配置

    打开 /usr/local/nginx/conf/nginx.conf 文件,删除文件末尾的默认配置内容:

    server {    listen 80;    server_name localhost;    index index.html index.htm index.php;    root /usr/local/nginx/html;    location ~ \.php$ {        include fastcgi_params;        fastcgi_pass unix:/tmp/php-fcgi.sock;        fastcgi_index index.php;        fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;    }}
  • 新增 include 语句

    在文件末尾新增一行,告诉 Nginx 导入 vhost 目录下的配置文件:

    include vhost/*.conf;
  • 创建 vhost 目录并新建默认配置文件

    • 创建 vhost 目录:
      mkdir -p /usr/local/nginx/conf/vhost
    • 打开新建的默认配置文件 /usr/local/nginx/conf/vhost/default.conf 并填写以下内容:
    server {    listen 80 default_server;    server_name aaa.com;    index index.html index.htm index.php;    root /data/wwwroot/aaa;}
  • 创建站点文件夹并准备测试页面

    • 创建站点主目录:
      mkdir -p /data/wwwroot/aaa
    • 新建站点首页文件(可用 curl 测试):
    echo "this is aaa.com site" > /data/wwwroot/aaa/index.html
  • 验证 Nginx 配置

    • 检查配置文件语法:
      /usr/local/nginx/sbin/nginx -t
    • 重新加载配置:
      /usr/local/nginx/sbin/nginx -s reload
  • 测试访问

    • 测试默认主机访问:

      curl localhost

      预期输出:this is aaa.com site

    • 测试非存在域名访问:

      curl -x 127.0.0.1:80 bbb.com

      预期输出:this is aaa.com site

  • 注意事项
    • 如果访问一个不存在的域名, Nginx 会自动将请求转向默认虚拟主机(aaa.com)。
    • 使用 systemctl restart nginx 会清空旧的缓存文件,建议在新建配置后使用此命令。

    Nginx 多虚拟主机配置

    通过上述方法,我们已经配置好了默认虚拟主机。接下来将介绍如何为多个站点配置 Nginx。

  • 进入 vhost 目录

    cd /usr/local/nginx/conf/vhost
  • 新建站点配置文件

    • 打开新的站点配置文件(例如 abc.conf)并填写以下内容:
    server {    server_name abc.com;    index index.html index.htm index.php;    root /data/wwwroot/abc;}
  • 创建站点主目录并准备测试页面

    • 创建站点主目录:
      mkdir -p /data/wwwroot/abc
    • 新建站点首页文件:
    echo "abc.com" > /data/wwwroot/abc/index.html
  • 测试访问

    • 测试访问 abc.com

      curl -x 127.0.0.1:80 abc.com

      预期输出:abc.com

    • 测试访问不存在的域名 cba.com

      curl -x 127.0.0.1:80 cba.com

      预期输出:this is aaa.com site(默认虚拟主机响应)


  • 总结

    通过以上配置,您已经成功搭建了 Nginx 的默认虚拟主机和多域名支持。默认虚拟主机自动处理未定义域名的请求,而多虚拟主机配置则允许您为不同域名设置独立的文件存储路径和访问权限。

    转载地址:http://hyufk.baihongyu.com/

    你可能感兴趣的文章
    NAT类型与NAT模型详解
    查看>>
    NAT网络地址转换配置实战
    查看>>
    NAT网络地址转换配置详解
    查看>>
    navbar navbar-inverse 导航条设置颜色
    查看>>
    Navicat for MySQL 命令列 执行SQL语句 历史日志
    查看>>
    Navicat for MySQL 查看BLOB字段内容
    查看>>
    Navicat for MySQL(Ubuntu)过期解决方法
    查看>>
    Navicat Premium 12 卸载和注册表的删除
    查看>>
    Navicat 导入sql文件
    查看>>
    navicat 添加外键1215错误
    查看>>
    navicat 系列软件一点击菜单栏就闪退
    查看>>
    navicat 自动关闭_干掉Navicat!MySQL官方客户端到底行不行?
    查看>>
    Navicat 设置时间默认值(当前最新时间)
    查看>>
    navicat 连接远程mysql
    查看>>
    navicat:2013-Lost connection to MySQL server at ‘reading initial communication packet解决方法
    查看>>
    Navicate for mysql 数据库设计-数据库分析
    查看>>
    Navicat下载和破解以及使用
    查看>>
    Navicat中怎样将SQLServer的表复制到MySql中
    查看>>
    navicat创建连接 2002-can‘t connect to server on localhost(10061)且mysql服务已启动问题
    查看>>
    Navicat可视化界面导入SQL文件生成数据库表
    查看>>