一、Nginx基于多域名的虚拟主机配置
本实验为本地模拟,真实环境下,需要购买域名并解析多个域名即可
1、环境准备,先在客户端本地 修改hosts文件信息
windows C:\Windows\System32\drivers\etc\hosts
2、修改hosts文件,添加如下信息
42.192.180.238 learn_nginx.com 42.192.180.238 learn_nginx_cc.com 42.192.180.238 learn_nginx_yy.com
3、修改服务端的配置
主配置文件中http配置
[root@VM-4-2-centos conf]# pwd /usr/local/nginx/conf

创建一个extra目录
[root@VM-4-2-centos conf]# ll total 72 drwxr-xr-x 2 root root 4096 Jul 29 14:39 extra -rw-r--r-- 1 root root 3596 Jul 29 14:37 nginx.conf

在extra中建立相应文件:

内容如下:
[root@VM-4-2-centos conf]# cat extra/learn_nginx.conf
server {
listen 80;
server_name learn_nginx.com;
charset utf-8;
location / {
root /www/vod;
index index.html index.htm;
}
}
[root@VM-4-2-centos conf]# cat extra/learn_nginx_cc.conf
server {
listen 80;
server_name learn_nginx_cc.com;
charset utf-8;
location / {
root /www/vod;
index index.html index.htm;
}
}
[root@VM-4-2-centos conf]# cat extra/learn_nginx_yy.conf
server {
listen 80;
server_name learn_nginx_yy.com;
charset utf-8;
location / {
root /www/vod;
index index.html index.htm;
}
}检查语法是否正确:
[root@node-a extra]# /usr/local/nginx/sbin/nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
4、重新加载nginx.conf 配置文件
# nginx -s reload
5、通过虚拟域名访问

6、补充
当配置有多个虚拟主机server时,默认访问的是第一个,如果想改变第一个为默认,则在需要的listen后天就参数 default_server;

添加后重新加载配置,访问ip即改变了第一个server
