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


web服务器上的虚拟主机_第1张图片
Snip20170326_3.png

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

你可能感兴趣的:(web服务器上的虚拟主机)