配置Nginx虚拟主机

前端之家收集整理的这篇文章主要介绍了配置Nginx虚拟主机前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

<h2 id="实验环境">实验环境

  1. 一台最小化安装的CentOS 7.3虚拟机

Nginx">1. 安装Nginx

yum install -y epel-*
yum isntall -y Nginx vim

站点根目录">2. 建立虚机主机的站点根目录

mkdir /var/wwwroot
mkdir /var/wwwroot/site1
mkdir /var/wwwroot/site2
echo -e "site1" >> /var/wwwroot/site1/index.html
echo -e "site2" >> /var/wwwroot/site2/index.html

关闭centos的防火墙">3. 关闭CentOS的防火墙

setenforce 0
systemctl stop firewalld
systemctl disable firewalld

Nginx配置文件">1. 编辑Nginx配置文件

vim /etc/Nginx/conf.d/vhosts.conf

添加以下内容">2. 添加以下内容

Nginx">server {
    listen 8081;
    root /var/wwwroot/site1;
    index index.html;
location / {
}

}
server {
listen 8082;
root /var/wwwroot/site2;
index index.html;

location / {
}
}

Nginx服务">3. 启动Nginx服务

systemctl start Nginx

站点">4. 在宿主机访问两个站点

http://192.168.204.135:8081/http://192.168.204.135:8082/

Nginx配置文件">1. 重新编辑Nginx配置文件

vim /etc/Nginx/conf.d/vhosts.conf

删除原内容重新添加以下内容">2. 删除内容,重新添加以下内容

Nginx">server {
    listen 80;
    server_name site1.test.com;
    root /var/wwwroot/site1;
    index index.html;
location / {
}

}
server {
listen 80;
server_name site2.test.com;
root /var/wwwroot/site2;
index index.html;

location / {
}
}

Nginx服务">3. 重启Nginx服务

systemctl restart Nginx

修改hosts文件">4. 在Windows上修改hosts文件

编辑C:\Windows\System32\drivers\etc\hosts文件添加以下内容(根据实际情况自己修改

192.168.204.135   site1.test.com  
192.168.204.135   site2.test.com

站点-1">5. 在宿主机访问两个站点

http://site1.test.com/http://site2.test.com/

增加两个ip地址">1. 在虚拟机增加两个IP地址

ifconfig ens33:1 192.168.204.151
ifconfig ens33:2 192.168.204.152

Nginx配置文件-1">2. 重新编辑Nginx配置文件

vim /etc/Nginx/conf.d/vhosts.conf

删除原内容重新添加以下内容-1">3. 删除内容,重新添加以下内容

Nginx">server {
    listen 192.168.204.151:80;
    root /var/wwwroot/site1;
    index index.html;
location / {
}

}
server {
listen 192.168.204.152:80;
root /var/wwwroot/site2;
index index.html;

location / {
}
}

Nginx服务-1">4. 重启Nginx服务

systemctl restart Nginx

站点-2">5. 在宿主机访问两个站点

http://192.168.204.151/http://192.168.204.152/

本文链接Nginx-vhost.html" class="uri">https://www.cnblogs.com/connect/p/Nginx-vhost.html

猜你在找的Nginx相关文章