Nginx虚拟主机[多域名、多端口]

发布时间 2023-06-02 13:25:45作者: QUDE

多域名虚拟主机

创建各自的配置文件

分别写入配置 ,基于域名的虚拟主机,这样写
域名1:dnf.com
[root@web /etc/nginx/conf.d]#touch dnf.conf

server {

    listen 80;
    server_name dnf.com; # 这里写的是域名
    charset utf-8;
    location /  {
        root  /www/dnf/;  # 根目录dnf
        index  index.html;
    }

}

域名2:lol.com
[root@web /etc/nginx/conf.d]#touch lol.conf

server {

    listen 80;
    server_name lol.com; # 这里写的是域名
    charset utf-8;
    location /  {
        root  /www/lol/;  # 根目录lol
        index  index.html;
    }

}
#然后重启nginx生效
[root@web /etc/nginx/conf.d]#systemctl restart nginx

 

多端口虚拟主机

在一个配置文件中,定义多个虚拟主机

vim /etc/nginx/conf.d/port.conf
# 平级
server {

    listen 10.0.0.8:81; #这里写ip:端口
    server_name _;  #_代表空
    charset utf-8;
    location /  {
        root  /www/data81/;
        index  index.html;
    }

}
 # 平级
server {

    listen 10.0.0.8:82;#这里写ip:端口
    server_name _; #_代表空
    charset utf-8;
    location /  {
        root  /www/data82/;
        index  index.html;
    }

}
#然后重启nginx生效
[root@web /etc/nginx/conf.d]#systemctl restart nginx