前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >12.7 默认虚拟主机

12.7 默认虚拟主机

作者头像
运维小白
发布2018-02-06 15:45:39
7.7K0
发布2018-02-06 15:45:39
举报
文章被收录于专栏:运维小白运维小白

默认虚拟主机目录概要

  • vim /usr/local/nginx/conf/nginx.conf //增加include vhost/*.conf
  • mkdir /usr/local/nginx/conf/vhost
  • cd !$; vim default.conf //加入如下内容
代码语言:javascript
复制
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配置,删除默认的虚拟主机配置,重新定义虚拟主机配置所在路径
代码语言:javascript
复制
[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
代码语言:javascript
复制
[root@hanfeng conf]# vim /usr/local/nginx/conf/nginx.conf 

增加其中的一行
    application/xml;
    include vhost/*.conf
}      
   
然后保存退出
  1. 新建/usr/local/nginx/conf/vhost目录
代码语言:javascript
复制
[root@hanfeng conf]# mkdir /usr/local/nginx/conf/vhost
[root@hanfeng conf]# 
  1. 进入到/usr/local/nginx/conf/vhost目录下
代码语言:javascript
复制
[root@hanfeng conf]# cd /usr/local/nginx/conf/vhost
[root@hanfeng vhost]# 
  1. 定义新增虚拟主机的配置
代码语言:javascript
复制
[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. 创建目录
代码语言:javascript
复制
[root@hanfeng vhost]# mkdir -p /data/wwwroot/default/
[root@hanfeng vhost]# 
  1. 切换到/data/wwwroot/default/目录下,在目录下写入一些东西
代码语言:javascript
复制
[root@hanfeng vhost]# cd /data/wwwroot/default/
[root@hanfeng default]# 
  1. 新建index.html,写入一些东西
代码语言:javascript
复制
[root@hanfeng default]# vim index.html
写入
This is the default site.
然后保存退出
  1. 建测配置文件是否存在语法错误
代码语言:javascript
复制
[root@hanfeng default]# /usr/local/nginx/sbin/nginx -t
  • 错误
代码语言:javascript
复制
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后面缺少了;
代码语言:javascript
复制
在后面添加 ; 即可
include vhost/*.conf;
  1. 再来检查配置文件是否存在语法错误
代码语言:javascript
复制
[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重新加载
代码语言:javascript
复制
[root@hanfeng default]# /usr/local/nginx/sbin/nginx -s reload
[root@hanfeng default]# 
  1. 测试访问默认页
  • 出来的就是之前/data/wwwroot/default/index.html里面定义的内容
代码语言:javascript
复制
[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 ”即可,把当前的这个配置对应的网站设置为第一个默认虚拟主机

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 默认虚拟主机目录概要
  • 默认虚拟主机
    • 定义默认虚拟主机
    相关产品与服务
    轻量应用服务器
    轻量应用服务器(TencentCloud Lighthouse)是新一代开箱即用、面向轻量应用场景的云服务器产品,助力中小企业和开发者便捷高效的在云端构建网站、Web应用、小程序/小游戏、游戏服、电商应用、云盘/图床和开发测试环境,相比普通云服务器更加简单易用且更贴近应用,以套餐形式整体售卖云资源并提供高带宽流量包,将热门开源软件打包实现一键构建应用,提供极简上云体验。
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档