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


Snip20170326_3.png

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 1.ngnix介绍 ngnix www服务软件 俄罗斯人开发 开源 性能很高 本身是一款静态WWW软件 静态小文件...
    逗比punk阅读 2,215评论 1 6
  • 《老男孩Linux运维》笔记 隐藏Nginx软件版本号 一般来说,软件的漏洞都和版本有关。因此要尽量隐藏对访问用户...
    Zhang21阅读 3,853评论 0 28
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,338评论 19 139
  • 上一篇《WEB请求处理一:浏览器请求发起处理》,我们讲述了浏览器端请求发起过程,通过DNS域名解析服务器IP,并建...
    七寸知架构阅读 81,662评论 21 356
  • Nginx简介 解决基于进程模型产生的C10K问题,请求时即使无状态连接如web服务都无法达到并发响应量级一万的现...
    魏镇坪阅读 2,181评论 0 9

友情链接更多精彩内容