Nginx配置虚拟主机
一. 基于域名的虚拟主机配置
1. 需求方案
-
两个域名指向同一台 Nginx 服务器,用户访问不同的域名显示不同的网页内容;
-
两个域名是 service.shop.com 和 web.shop.com;
-
Nginx 服务器使用虚拟机 192.168.87.108:80端口.
-
通过 host 文件指定 service.shop.com 和web.shop.com 对应 192.168.87.108 虚拟机:
-
修改 windows或linux系统的 的 hosts 文件:(C:\Windows\System32\drivers\etc)与(/etc/hosts)
配置/etc/hosts文件


在 /usr/local/nginx/www 目录下创建 htmlserver 和 htmlweb 两个目录,并分辨创建两个 index.html 文件,以便区分.
user www-data; worker_processes 1; events { worker_connections 1024; } http { include /etc/nginx/mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; #基于域名的虚拟主机配置方案 #配置http://service.shop.com:80虚拟主机 server { listen 80; #监听端口号 server_name service.shop.com; #域名 location / { root /usr/local/nginx/www/htmlserver; #静态资源存储位置 index index.html index.htm; } } #配置http://web.shop.com:80虚拟主机 server { listen 80; server_name web.shop.com; location / { root /usr/local/nginx/www/htmlweb; index index.html index.htm; } } }
注意:重新加载一下配置文件
sudo service nginx reload
配置好后的效果: