Nginx系列-3.配置Nginx虚拟主机
目录 - Nginx系列
Nginx系列-1.Linux下安装Nginx
Nginx系列-2.配置LNMP(Linux、Nginx、MySQL、PHP)架构
Nginx系列-3.配置Nginx虚拟主机
Nginx系列-4.Nginx日志配置及日志切割
Nginx系列-5.配置Nginx的防盗链
Nginx系列-6.配置Nginx的HTTPS
Nginx系列-7.配置Nginx使用uwsgi支持web.py框架
Nginx系列-8.配置Nginx+Apache实现动静分离
Nginx系列-9.配置NFS实现Nginx实现动静分离
Nginx系列-10.采用Nginx搭建正向代理服务
Nginx系列-11.配置Nginx反向代理和负载均衡
实验环境
一台最小化的 CentOS 7.3 虚拟机
IP: 192.168.204.135
一、配置基本环境
-
安装nginx
yum install -y epel-* yum isntall -y nginx vim
-
建立虚机主机的站点根目录
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的防火墙
setenforce 0 systemctl stop firewalld systemctl disable firewalld
二、配置基于端口的虚拟主机
- 编辑nginx配置文件
vim /etc/nginx/conf.d/vhosts.conf
-
添加以下内容
server { listen 8081; root /var/wwwroot/site1; index index.html; location / { } } server { listen 8082; root /var/wwwroot/site2; index index.html; location / { } }
-
启动
nginx
服务systemctl start nginx
- 在宿主机访问两个站点
http://192.168.204.135:8081/
http://192.168.204.135:8082/
三、配置基于域名的虚拟主机
-
重新编辑nginx配置文件
vim /etc/nginx/conf.d/vhosts.conf
-
删除原内容,重新添加以下内容
扫描二维码关注公众号,回复: 1525690 查看本文章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
服务systemctl restart nginx
-
在Windows上修改
hosts
文件
编辑C:\Windows\System32\drivers\etc\hosts
文件,
添加以下内容(根据实际情况自己修改)192.168.204.135 site1.test.com 192.168.204.135 site2.test.com
- 在宿主机访问两个站点
http://site1.test.com/
http://site2.test.com/
四、配置基于IP的虚拟主机
-
在虚拟机增加两个IP地址
ifconfig ens33:1 192.168.204.151 ifconfig ens33:2 192.168.204.152
-
重新编辑nginx配置文件
vim /etc/nginx/conf.d/vhosts.conf
-
删除原内容,重新添加以下内容
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
服务systemctl restart nginx
- 在宿主机访问两个站点
http://192.168.204.151/
http://192.168.204.152/