一、概述:

    众所周知,没个域名通过DNS解析后都会落在一个对应的服务器IP地址,在全球IPV4地址用尽的情况下。如果域名不断增加,这怎么办呢?或者说,我们手上只有一台服务器资源,一个公网IP地址,但是却有多个域名,这怎么办呢?有没办法把多个域名都指向到这个服务器地址、且每个域名对应的应用内容都不一样呢?答案是:可以的。
Nginx开源web服务器,就支持这种诉求,将多个域名绑定到一个共同的IP地址。下面具体描述下,基于nginx服务器,如何进行这一个诉求的操作。

二、具体操作方式:

1,环境准备:

    考虑到实验性环境,我们可以通过自己找一台linux服务器,在局域网内部进行测试验证,或者是通过本机。通过虚拟化软件搭建一台服务器,进行测试验证。

本文章使用的测试如下:
Virtualbox搭建一个Centos7服务器环境,固定IP地址为192.168.56.101
nginx版本为:

1
2
3
4
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) 
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx --with-file-aio --with-ipv6 --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-stream_ssl_preread_module --with-http_addition_module --with-http_xslt_module=dynamic --with-http_image_filter_module=dynamic --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_slice_module --with-http_stub_status_module --with-http_perl_module=dynamic --with-http_auth_request_module --with-mail=dynamic --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module --with-google_perftools_module --with-debug --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -m64 -mtune=generic' --with-ld-opt='-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E'

关闭所有的Centos防火墙。

2,操作步骤:

首先:安装nginx。
    这里网上很多教程,就不多论述了,linux下安装比较简单,具体操作步骤可以参考Nginx官网 或者搜索引擎返回的结果中寻找。

其次:修改配置文件。
    Nginx有vhost的概念,这个功能就是支持一台服务器包含多个虚拟主机。每个vhost对应一个文件即可。
本实验使用的配置文件路径是

1
/etc/nginx/nginx.conf

在目录/etc/nginx下创建一个文件夹vhost用来保存各种虚拟主机的配置文件。
1
sudo mkdir -p /etc/nginx/vhost

然后进入vhost文件夹,分别创建两个vhost配置文件:
1
2
3
cd /etc/nginx/vhost
sudo touch example1.com.conf
sudo touch example2.com.conf

利用分别输入以下内容到这两个配置文件中:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
example1.com.conf

server {
listen 80; #监听端口
server_name example1.com www.example1.com;#绑定的域名

access_log /home/www-data/log/access_example1.log main;#日志路径

location /{ #域名对应应用代码路径,首页内容等
root /home/www-data/example1.com;
index index.php index.html index.htm;
}

error_page 500 502 503 504 /50x.html; #错误页面的配置,这个可以直接使用公用的,如果内容都是一样的话
location /50x.html {
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
example2.com.conf

server {
listen 80;
server_name example2.com www.example2.com; #这里不同的域名不同的内容

access_log /home/www-data/log/access_example2.log main;

location /{
root /home/www-data/example2.com; #这里是内容不同
index index.php index.html index.htm;
}

error_page 500 502 503 504 /50x.html;
location /50x.html {
}
}

配置好这两个vhost的虚拟主机配置,然后再/etc/nginx/nginx.conf的http配置域内,通过include指令引入这些vhost配置。如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

access_log /home/www-data/log/access.log main;

sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;

include /etc/nginx/mime.types;
default_type application/octet-stream;

# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;

server {
listen 80 default_server;
listen [::]:80 default_server;
server_name wordpress_server:192-168-56-102;
root /home/www-data/html;
index index.php index.html
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;

error_page 404 /404.html;
location = /40x.html {
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
}

}
#在这里引入刚才配置的虚拟文件。
include /etc/nginx/vhosts/*;
}

到此为止,多个域名绑定到一个机器IP已经配置完毕。下面我们来做一个简单的测试。

三、测试验证

由于是测试配置的准确性,所以我们直接在两个vhost的目录下创建简单的index.html文件。

example1.com

1
echo "This Example1.com website." >> /home/www-data/exmaple1.com/index.html	

example2.com

1
echo "This Example2.com website." >> /home/www-data/example2.com/index.html

配置完毕,重启nginx

1
sudo nginx reload

到此,针对nginx的配置已经完成了。

不过还缺少一个很重要的步骤,就是本地域名的绑定。因为我们的域名都是随机定义的测试域名,需要直接在hosts文件进行域名的绑定。

1
2
sudo vim /etc/hosts

输入以下内容:

1
2
3

192.168.56.101 example1.com www.example1.com example2.com www.example2.com

打开浏览器,分别输入http://www.example1.com和http://www.exmaple2.com。
要是配置都没有问题的话,会分别在输入example1.com后看到输出是:This Example1.com website。输入example2.com后看到This Example2.com website。