web服务器的host文件,web服务器上的虚拟主机

将指定的域名绑定到指定的服务器目录上面,这就是web服务器的虚拟主机功能。绑定之后,打开域名就会访问服务器上指定的目录。此处用Nginx做例子说明。

1、进入nginx的conf.d目录

cd /etc/nginx/conf.d

用ls命令查看目录下内容,可以看到default.conf这个文件

2、通过复制default.conf创建新的 .conf文件。

cp default.conf test.xiangzhige.cn.conf

3、修改test.xiangzhige.cn.conf配置文件的内容

vim test.xiangzhige.cn.conf

server {

listen 80;

server_name localhost;

#charset koi8-r;

#access_log /var/log/nginx/log/host.access.log main;

location / {

root /usr/share/nginx/html;

index index.html index.htm;

}

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html

#

error_page 500 502 503 504 /50x.html;

location = /50x.html {

root /usr/share/nginx/html;

}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80

#

#location ~ \.php$ {

# proxy_pass http://127.0.0.1;

#}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

#

#location ~ \.php$ {

# root html;

# fastcgi_pass 127.0.0.1:9000;

# fastcgi_index index.php;

# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;

# include fastcgi_params;

#}

# deny access to .htaccess files, if Apache's document root

# concurs with nginx's one

#

#location ~ /\.ht {

# deny all;

#}

}

server标志定义虚拟主机开始,listen用于指定虚拟主机的服务端口(80是http默认的端口号),server_name用来指定IP地址或者域名,多个域名之间用空格分 开。index用于设定访问的默认首页地址,root指令用于指定虚拟主机的网页根目录,这个目录可以是相对路径,也可以是绝对路径。

修改server_name、root、index项的内容如下:

server_name test.xiangzhige.cn;

#charset koi8-r;

#access_log /var/log/nginx/log/host.access.log main;

root /alidata1/www/test.xiangzhige.cn;

location / {

index index.php index.html index.htm;

}

要使修改的nginx配置文件生效,需要重新加载或启动nginx

测试nginx配置文件是否有错

nginx -t

如果有错就改正,没有就执行下面命令重新加载nginx

service nginx reload

4、创建虚拟主机需要的目录

[root@liboxiangECS conf.d]# mkdir /alidata1

[root@liboxiangECS conf.d]# cd /alidata1

[root@liboxiangECS alidata1]# mkdir www

[root@liboxiangECS alidata1]# cd www

[root@liboxiangECS www]# mkdir test.xiangzhige.cn

创建index.html文件

[root@liboxiangECS www]# cd test.xiangzhige.cn/

[root@liboxiangECS test.xiangzhige.cn]# vim index.html

在index.html文件中输入hello,也可以输入其它内容

5、验证虚拟主机是否创建成功

在浏览器中输入域名,以为我用的是test.xiangzhige.cn,所以我输入test.xiangzhige.cn

558ddc333cfa

Snip20170326_3.png

如果浏览器打开显示的是4中输入的内容,则证明虚拟主机创建成功

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值