虚拟站点配置

基于域名的虚拟主机

假设我们在本地有2个项目,分别在 /etc/hosts 里映射到本地的 127.0.0.1 上:

127.0.0.1 test1.xiaoyulive.top
127.0.0.1 test2.xiaoyulive.top

这2个项目分别对应于以下的2个文件夹,我们用域名对应文件夹名字,这样子好记:

/usr/nginx/test1.xiaoyulive.top
/usr/nginx/test2.xiaoyulive.top

每个目录下都有一个index.php文件,简单的输入:

# /usr/nginx/test1.xiaoyulive.top
<?php
echo 'hello';
# /usr/nginx/test2.xiaoyulive.top
<?php
echo 'world';

下面我们就来搭建这2个域名的虚拟主机,很显然,我们要新建2个conf文件来完成。

为了看起来简洁好看,我们使用 include 在 nginx.conf 中包含外面的2个conf文件,这样就清晰了很多。不会使得这个 nginx.conf 内容太多:

main
events {
  ....
}
http {
  ....
  include /etc/nginx/vhost/test1.xiaoyulive.top;
  include /etc/nginx/vhost/test2.xiaoyulive.top;
  # 或者用 *.conf  包含某个文件夹下的所有 conf 文件
  # include /etc/nginx/vhost/*.conf
}

然后撰写两个conf文件:

# /etc/nginx/vhost/test1.xiaoyulive.top
server {
    listen 80;
    server_name test1.xiaoyulive.top;
    root /usr/nginx/test1.xiaoyulive.top;
    index index.php index.html index.htm;
    access_log /usr/local/var/log/nginx/test1.xiaoyulive.top.log main;
    error_log /usr/local/var/log/nginx/test1.xiaoyulive.top.log error;
    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        include        fastcgi.conf;
    }
}
# /etc/nginx/vhost/test2.xiaoyulive.top
server {
    listen 80;
    server_name test2.xiaoyulive.top;
    root /usr/nginx/test2.xiaoyulive.top;
    index index.php index.html index.htm;
    access_log /usr/local/var/log/nginx/test2.xiaoyulive.top.log main;
    error_log /usr/local/var/log/nginx/test2.xiaoyulive.top.log error;
    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        include        fastcgi.conf;
    }
}

这样2个很精简的虚拟域名就搭建好了。重启下nginx,然后打开浏览器访问一下这2个域名,就能看到对应的域名内容了。

基于IP的距离主机

为本机绑定多个IP以便于测试:

$ ifconfig ens33:1 192.168.2.151/24 up
$ ifconfig ens33:2 192.168.2.152/24 up
$ ifconfig ens33:3 192.168.2.153/24 up

创建三个站点目录并写入文件:

$ mkdir www && cd www
$ mkdir test1
$ mkdir test2
$ mkdir test3
$ echo 'test1' > test1/index.html
$ echo 'test2' > test2/index.html
$ echo 'test3' > test3/index.html

创建3个虚拟主机文件:

$ vim /etc/nginx/conf.d/test1.conf
server {
    listen       192.168.2.151:80;
    location / {
        root   /root/www/test1;
        index  index.html index.htm;
    }
}
$ vim /etc/nginx/conf.d/test2.conf
server {
    listen       192.168.2.152:80;
    location / {
        root   /root/www/test2;
        index  index.html index.htm;
    }
}
$ vim /etc/nginx/conf.d/test3.conf
server {
    listen       192.168.2.153:80;
    location / {
        root   /root/www/test3;
        index  index.html index.htm;
    }
}

测试

$ curl 192.168.2.151
test1
$ curl 192.168.2.152
test2
$ curl 192.168.2.153
test3

基于端口的虚拟主机

还是创建两个虚拟站点:

$ echo 'test1' > /root/www/test1/index.html
$ echo 'test2' > /root/www/test2/index.html

这个就很容易了, 不需要更多额外的操作, 直接在配置文件中指定端口即可:

$ vim /etc/nginx/conf.d/test1.conf
server {
    listen       127.0.0.1:8081;
    location / {
        root   /root/www/test2;
        index  index.html index.htm;
    }
}
$ vim /etc/nginx/conf.d/test2.conf
server {
    listen       127.0.0.1:8082;
    location / {
        root   /root/www/test2;
        index  index.html index.htm;
    }
}

测试:

$ curl 127.0.0.1:8081
test1
$ curl 127.0.0.1:8082
test2

MIT Licensed | Copyright © 2018-present 滇ICP备16006294号

Design by Quanzaiyu | Power by VuePress