12.7 默认虚拟主机

时间:2022-04-27
本文章向大家介绍12.7 默认虚拟主机,主要内容包括默认虚拟主机目录概要、默认虚拟主机、基本概念、基础应用、原理机制和需要注意的事项等,并结合实例形式分析了其使用技巧,希望通过本文能帮助到大家理解应用这部分内容。

默认虚拟主机目录概要

  • vim /usr/local/nginx/conf/nginx.conf //增加include vhost/*.conf
  • mkdir /usr/local/nginx/conf/vhost
  • cd !$; vim default.conf //加入如下内容
server
{
    listen 80 default_server;  // 有这个标记的就是默认虚拟主机
    server_name aaa.com;
    index index.html index.htm index.php;
    root /data/wwwroot/default;
}
  • mkdir -p /data/wwwroot/default/
  • echo “This is a default site.”>/data/wwwroot/default/index.html
  • /usr/local/nginx/sbin/nginx -t
  • /usr/local/nginx/sbin/nginx -s reload
  • curl localhost
  • curl -x127.0.0.1:80 123.com

默认虚拟主机

  1. 首先删除/usr/local/nginx/conf/nginx.conf 中的一部分内容——>目的是修改nginx.cnf配置,删除默认的虚拟主机配置,重新定义虚拟主机配置所在路径
[root@hanfeng conf]# vim /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;
        }
    }
  1. 然后在配置文件中增加一行,include vhost/*.conf
[root@hanfeng conf]# vim /usr/local/nginx/conf/nginx.conf 

增加其中的一行
    application/xml;
    include vhost/*.conf
}      
   
然后保存退出
  1. 新建/usr/local/nginx/conf/vhost目录
[root@hanfeng conf]# mkdir /usr/local/nginx/conf/vhost
[root@hanfeng conf]# 
  1. 进入到/usr/local/nginx/conf/vhost目录下
[root@hanfeng conf]# cd /usr/local/nginx/conf/vhost
[root@hanfeng vhost]# 
  1. 定义新增虚拟主机的配置
[root@hanfeng vhost]# vim aaa.com.conf

添加的文件内容
server
{
    listen 80 default_server;  
    server_name aaa.com;
    index index.html index.htm index.php;
    root /data/wwwroot/default;
}

然后保存退出
  1. 创建目录
[root@hanfeng vhost]# mkdir -p /data/wwwroot/default/
[root@hanfeng vhost]# 
  1. 切换到/data/wwwroot/default/目录下,在目录下写入一些东西
[root@hanfeng vhost]# cd /data/wwwroot/default/
[root@hanfeng default]# 
  1. 新建index.html,写入一些东西
[root@hanfeng default]# vim index.html
写入
This is the default site.
然后保存退出
  1. 建测配置文件是否存在语法错误
[root@hanfeng default]# /usr/local/nginx/sbin/nginx -t
  • 错误
nginx: [emerg] unexpected "}" in /usr/local/nginx/conf/nginx.conf:47
nginx: configuration file /usr/local/nginx/conf/nginx.conf test failed
  • 解决方法:
    • 是因为在/usr/local/nginx/conf/nginx.conf文件中include vhost/*.co后面缺少了;
在后面添加 ; 即可
include vhost/*.conf;
  1. 再来检查配置文件是否存在语法错误
[root@hanfeng default]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
  1. 再修改配置文件后,一般都 -t 去检查下,防止误操作
  2. 修改完,重启nginx或者重新加载nginx
  • 使用/etc/init.d/nginx restart 或者 /usr/local/nginx/sbin/nginx -s reload重新加载
[root@hanfeng default]# /usr/local/nginx/sbin/nginx -s reload
[root@hanfeng default]# 
  1. 测试访问默认页
  • 出来的就是之前/data/wwwroot/default/index.html里面定义的内容
[root@hanfeng default]# curl localhost
This is the default site.
[root@hanfeng default]# curl -x127.0.0.1:80 bbb.com
This is the default site.
[root@hanfeng default]# 
  1. nginx支持include这种语法

定义默认虚拟主机

因为修改了nginx.conf的配置,现在看到的默认索引页,是我们刚刚新增的vhost的虚拟主机的索引页了 定义默认虚拟主机的两种办法: 1.默认虚拟主机,是根据目录的第一个.conf了进行选择,所以只需要在vhost目录下依次创建就可以了,当然这种方法不智能 2.只需要在vhost目录的.conf配置文件内,加上一个“default_server ”即可,把当前的这个配置对应的网站设置为第一个默认虚拟主机