一、概述

虚拟主机的意思就是说把一台物理的主机分割成多个虚拟的主机,每个主机都能监听独立的域名并且拥有独立的目录。

使用nginx 实现多态虚拟主机需要用到server 模块和location 模块

二、location配置

location / {
        root   /data/www; # 目录地址
        index  index.php index.html index.htm; # 默认首页
}

配置一个简单的虚拟主机

修改上篇中的server 模块为以下代码,同样也能实现相同的功能:

server {
        listen       8099;
        server_name  192.168.87.131;
        location / {
                root   /data/www; 
                index  index.php index.html index.htm; 
        }
        charset utf-8;
        access_log  logs/demo.access.log  main;
        error_log  logs/demo.error.log  error;
}

或者把location 部分的代码修改为如下代码也可以:

 location /index.html {
        alias /data/www/index.html;
}

这里可以省略index.html 字段,因为省略首页名的时候,nginx 会自动搜索index.html ,前提是/data/www 目录下得有index.html 文件

 location /static {
        alias /data/www;
}
最后修改:2018 年 12 月 16 日
如果觉得我的文章对你有用,请随意赞赏