虚拟主机指的是,在一台服务器中,我们使用Nginx,来配置多个网站。
如何区分不同的网站:
(1)Nginx配置文件的位置
- cd /usr/local/nginx/conf
- nginx.conf 就是Nginx的配置文件
(2)Nginx核心配置文件说明
- worker_processes 1; #work的进程数,默认为1
- #配置 影响nginx服务器与用户的网络连接
- events {
- worker_connections 1024; #单个work 最大并发连接数
- }
-
- # http块是配置最频繁的部分 可以嵌套多个server,配置代理,缓存,日志定义等绝大多数功能
- http {
- # 引入mime类型定义文件
- include mime.types;
- default_type application/octet-stream;
- sendfile on;
- keepalive_timeout 65; # 超时时间
-
- #server 配置虚拟主机的相关参数 可以有多个,一个server就是一个虚拟主机
- server {
- # 监听的端口
- listen 80;
- #监听地址
- server_name localhost;
-
- # 默认请求配置
- location / {
- root html; # 默认网站根目录
- index index.html index.htm; # 欢迎页
- }
-
- # 错误提示页面
- error_page 500 502 503 504 /50x.html;
- location = /50x.html {
- root html;
- }
- }
- }
(1)使用vim命令编辑此配置文件
- http {
- include mime.types;
- default_type application/octet-stream;
-
- sendfile on;
- #tcp_nopush on;
-
- #keepalive_timeout 0;
- keepalive_timeout 65;
-
- #gzip on;
-
- server {
- listen 80;
- server_name localhost;
- location / {
- root html;
- index index.html index.htm;
- }
- }
-
-
- # 配置新的server
- server {
- listen 81; # 修改端口
- server_name localhost;
- location / {
- root html81; # 重新制定一个目录
- index index.html index.htm;
- }
- }
- }
(2)复制一份 html目录
cp -r html html81

(3) 重新加载配置文件
sbin/nginx -s reload
(4)访问
http://192.168.80.100 访问第一个server
http://192.168.81.100:81/ 访问第二个server
配置一下nginx的映射
182.168.80.100 www.ng.com

在window系统中配置host文件
192.168.80.100 www.taobao.com
192.168.80.100 www.jd.com
- #通过域名区分虚拟主机
- server {
- listen 80;
- server_name www.taobao.com;
- location / {
- root taobao;
- index taobao.html;
- }
- }
-
- server {
- listen 80;
- server_name www.jd.com;
- location / {
- root jd;
- index jd.html;
- }
- }
-
(1)修改一下index.html 中,刷新
sbin/nginx -s reload
(2)访问

虽然只有一台服务器,但是这台服务器上运行着多个网站,访问不同的域名 就可访问到不同的网站内容。