文章目录

  • 6. Nginx虚拟主机
    • 6.1 nginx 单虚拟主机配置
      • 6.1.1 静态网站的搭建
    • 6.2 案例1:多网卡多IP配置虚拟主机
      • 6.2.1 增加一块网卡
      • 6.2.2 编辑配置文件,基于每个IP创建一个虚拟主机
    • 6.3 案例2:单网卡多端口配置虚拟主机
    • 6.4 案例3:基于域名配置虚拟主机
    • 6.5 Nginx 虚拟主机日志定义

6. Nginx虚拟主机

虚拟主机使用的是特殊的软硬件技术,它把一台运行在因特网上的服务器主机分成一台台“虚拟”的主机,每台虚拟主机都可以是一个独立的网站,可以具有独立的域名,
具有完整的Intemet服务器功能(WWW、FTP、Email等),同一台主机上的虚拟主机之间是完全独立的。从网站访问者来看,每一台虚拟主机和一台独立的主机完全一样。nginx 支持多虚拟机,可以在一台机器上同时运行多个网站个功能nginx 的多虚拟机,可以基于:基于域名的虚拟主机 : 不同的域名、相同的IP(此方式应用最广泛)基于端口的虚拟主机 : 不使用域名、IP来区分不同站点的内容,而是用不同的TCP端口号基于IP地址的虚拟主机 : 不同的域名、不同的IP ( 需要加网络接口 ,应用的不广泛) 基于IP地址

6.1 nginx 单虚拟主机配置

6.1.1 静态网站的搭建
# 在 nginx.conf 配置文件中定义虚拟主机
http{# 配置一个 server{} 标签就可以理解是一个虚拟机站点,配置 N 个就代表有 N 个站点# server{} 默认加载顺序是自上而下的匹配规则(前提是如果没有其他定义的情况下,如基于域名的匹配,基于端口的匹配)server{}# 编写第二个server{}
}server {# 定义虚拟主机站点端口号,也是用户访问网站的入口listen       8088;# 域名配置,没有域名可以写 localhost 或者 _ # 过个域名之间使用空格隔开# server_name www.mondaygarden.com www.baidu.comserver_name  localhost;# 定义网站编码charset utf-8;#access_log  logs/host.access.log  main;# nginx 的路径匹配规则# 如下的规则是最低级匹配,任何 nginx 请求都会进入如下 localhost 的配置,去他所定义的路径下查找内容location / {# 这个 root 是定义网页根目录的,这个 html 是以 nginx 安装的路径相对的root   html;# index关键词,定义 nginx 的首页文件名,默认在访问 nginx 根目录下时去访问这个文件index  index.html index.htm;}#error_page  404              /404.html;# redirect server error pages to the static page /50x.html#error_page   500 502 503 504  /50x.html;location = /50x.html {root   html;}# proxy the PHP scripts to Apache listening on 127.0.0.1:80##location ~ \.php$ {#    proxy_pass   http://127.0.0.1;#}# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000##location ~ \.php$ {#    root           html;#    fastcgi_pass   127.0.0.1:9000;#    fastcgi_index  index.php;#    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;#    include        fastcgi_params;#}# deny access to .htaccess files, if Apache's document root# concurs with nginx's one##location ~ /\.ht {#    deny  all;#}}

6.2 案例1:多网卡多IP配置虚拟主机

6.2.1 增加一块网卡

# 查看 ip 地址
[root@nginx ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500inet 192.168.169.152  netmask 255.255.255.0  broadcast 192.168.169.255inet6 fe80::b824:f1be:b7d:9555  prefixlen 64  scopeid 0x20<link>ether 00:0c:29:b1:2e:fe  txqueuelen 1000  (Ethernet)RX packets 91871  bytes 92509486 (88.2 MiB)RX errors 0  dropped 0  overruns 0  frame 0TX packets 34112  bytes 39677157 (37.8 MiB)TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0ens37: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500inet 192.168.169.137  netmask 255.255.255.0  broadcast 192.168.169.255inet6 fe80::6225:5b3c:6a17:dc90  prefixlen 64  scopeid 0x20<link>ether 00:0c:29:b1:2e:08  txqueuelen 1000  (Ethernet)RX packets 7  bytes 986 (986.0 B)RX errors 0  dropped 0  overruns 0  frame 0TX packets 11  bytes 1434 (1.4 KiB)TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536inet 127.0.0.1  netmask 255.0.0.0inet6 ::1  prefixlen 128  scopeid 0x10<host>loop  txqueuelen 1000  (Local Loopback)RX packets 1116266  bytes 280663165 (267.6 MiB)RX errors 0  dropped 0  overruns 0  frame 0TX packets 1116266  bytes 280663165 (267.6 MiB)TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
6.2.2 编辑配置文件,基于每个IP创建一个虚拟主机
# 因为在主配置文件中使用 include 添加的配置文件信息,所以这里可以在指定目录下新建 .conf 文件
vim /usr/local/nginx/conf/conf_file/ip.conf
server {listen       192.168.169.152:8080;server_name  localhost;charset utf-8;location / {root html/ip_ens33;index   index.html;}
}server {listen       192.168.169.137:8080;server_name  localhost;charset utf-8;location / {root    html/ip_ens37;index   index.html;}
}
mkdir -p /usr/local/nginx/html/ip_ens33
mkdir -p /usr/local/nginx/html/ip_ens37echo "ens33" >> ip_ens33/index.html
echo "ens37" >> ip_ens37/index.html
# 检查配置文件是否正确
nginx -t# 重新加载
nginx -s reload
  • 测试:
    192.168.169.137:8080

  • 测试:
    192.168.169.152:8080

6.3 案例2:单网卡多端口配置虚拟主机

# 修改配置文件
# 因为在主配置文件中使用 include 添加的配置文件信息,所以这里可以在指定目录下新建 .conf 文件
vim /usr/local/nginx/conf/conf_file/port.conf
server {listen 81;location / {root server {listen       81;server_name  localhost;charset utf-8;location / {root    html/port_81;index   index.html;}}server {listen       82;server_name  localhost;charset utf-8;location / {root    html/port_82;index   index.html;}}
mkdir -p /usr/local/nginx/html/port_81
mkdir -p /usr/local/nginx/html/port_82echo "port81" >> /usr/local/nginx/html/port_81/index.html
echo "port82" >> /usr/local/nginx/html/port_82/index.html
# 检查配置文件是否正确
nginx -t# 重新加载
nginx -s reload
  • 测试:
    192.168.169.152:81

测试:
192.168.169.152:82

6.4 案例3:基于域名配置虚拟主机

# 修改配置文件
vim /usr/local/nginx/conf/conf_file/test1.conf
server {listen 8080;server_name test1.nginx.com;location / {root        html/test1;index       index.html;}
}vim /usr/local/nginx/conf/conf_file/test2.conf
server {listen 8081;server_name test2.nginx.com;location / {root        html/test1;index       index.html;}
}
mkdir -p /usr/local/nginx/html/test1
mkdir -p /usr/local/nginx/html/test2echo "test1" >> /usr/local/nginx/html/test1/index.html
echo "test2" >> /usr/local/nginx/html/test2/index.html
# 配置域名解析
[root@nginx ]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6192.168.169.152   test1.nginx.com
192.168.169.152 test2.nginx.com
# 配置 Windows 域名解析,本次测试使用
C:\Windows\System32\drivers\etc\hosts
192.168.169.152    test1.nginx.com
192.168.169.152    test2.nginx.com
  • 测试:
    test1.nginx.com:8080

  • 测试:
    test1.nginx.com:8081

6.5 Nginx 虚拟主机日志定义

问题:因为每配置一个 server{} 站点,在访问时就会产生一个相对应的访问日志,如果访问不同站点的日志都存在一个 access.log 日志中不方便查看各个站点的访问信息
解决:在对应的 server{} 中添加 access_log      logs/test1.logserver {listen 80;server_name     test1.nginx.com;access_log      logs/test1.log;location / {root        html/test1;index       index.html;}}

6. Nginx虚拟主机相关推荐

  1. Nginx教程--02.Nginx虚拟主机的配置

    1.Nginx虚拟主机的配置 1.1 在conf目录下,使用命令 : vim nginx.conf 对上图解释: //全局区 worker _processes 1; //表示当前有1个工作的子进程, ...

  2. nginx虚拟主机概念和类型介绍

    nginx虚拟主机配置实战 1,虚拟主机概念和类型介绍 所谓虚拟主机,在web服务里就是一个独立的网站站点,这个站点对应独立的域名(也可能是IP或端口),具有独立的程序及资源目录,可以独立地对外提供服 ...

  3. Nginx虚拟主机、Nginx模块的安装使用(加密模块--with-http_ssl_module)

    一,Nginx虚拟主机(一个nginx实现多个网站) 1,基于域名的虚拟主机 1)修改Nginx服务配置,添加相关虚拟主机配置如下 # vim /usr/local/nginx/conf/nginx. ...

  4. Nginx 虚拟主机 VirtualHost 配置

    原文地址: http://www.neoease.com/nginx-virtual-host/ 增加 Nginx 虚拟主机 1. 进入 /usr/local/nginx/conf/目录, 创建虚拟主 ...

  5. nginx虚拟主机解析php文件,window停nginx虚拟主机不能解析php

    window下nginx虚拟主机不能解析php 本地window7配置nginx 1.011虚拟主机不能解析php问题,导致:no input file specified ? ? nginx.con ...

  6. linux基于域名的虚拟主机,Nginx虚拟主机应用——基于域名、IP、端口的虚拟主机...

    Nginx支持的虚拟主机有三种 ●基于域名的虚拟主机 ●基于IP的虚拟主机 ●基于端口的虚拟主机 每一种虚拟主机均可通过"server{}" 配置段实现各自的功能 基于域名的虚拟主 ...

  7. php管理nginx虚拟主机shell脚本

    使用php作为shell脚本是一件很方便的事情.理所当然,我们可以使用php脚本来管理 nginx虚拟主机,下面是笔者的 脚本 文件供各位参考 代码如下 复制代码 #!/usr/bin/php -q ...

  8. php编译安装, 编译安装nginx, yum安装nginx, nginx虚拟主机,默认虚拟主机

    编译安装PHP-7.3.0 1. 下载程序 下载php 7.3.0 cd /usr/local/src wget http://cn2.php.net/distributions/php-7.3.0. ...

  9. web服务器 ---nginx 虚拟主机的创建(基于 域名 . 端口 . ip )以及nginx访问控制

    文章目录 前言 一:Nginx服务基础(理论) 二:Nginx虚拟主机实验 2.1:Nginx虚拟主机应用 2.2 具体步骤,配置基于域名的虚拟主机 2.2.1 安装环境软件软件 2.2.2 编译安装 ...

  10. 编写一键备份MYSQL数据库脚本; 一键Nginx虚拟主机添加、删除脚本;

    1.编写一键备份MYSQL数据库脚本: 1)支持任意单个或者多个数据库的备份: 2)支持多个数据库.所有库备份: 思路: 备份mysql数据库命令 #到处mysql所有库的数据到mysql_all.s ...

最新文章

  1. 可以打开md_热议MD有病!旭旭宝宝怒斥CG偷开“录像轮播”:人家不开还不行?...
  2. 3.1 普通型生成函数
  3. Java中的引用与C中的指针
  4. Ubuntu Server 上在安装Nginx时执行./confgiure后提示:C compiler cc is not found
  5. [OS复习]进程互斥与同步1
  6. Zabbix 3.2.6通过SNMP和iDRAC监控DELL服务器
  7. C语言之常见错误解决办法
  8. 功能Java示例 第6部分–用作参数
  9. android 应用在启动后进行全局的的初始化操作
  10. c语言数据交换的算法流程图,C语言冒泡排序算法浅析
  11. 带宽与码元的关系_再遇到码元、速率、、带宽【9】
  12. 驱动人生安装驱动计算机无法启动,驱动人生打开时出错怎么办
  13. UIPageControl---iOS-Apple苹果官方文档翻译
  14. Comet杀人游戏开发日志-1(问题记录-于核心功能测试成功转向实际开发阶段)
  15. 光纤非线性效应对光OFDM信号的影响研究
  16. mac上如何安装oracle,在mac上安装oracle instant client 和 sqlplus
  17. Drool学习记录(一) 概念、Helloworld
  18. php实现mkv视频播放,mkv怎么合并视频文件
  19. python安装jupter在线ide
  20. 11.1.4 子线程与主线程通信实例

热门文章

  1. CreationOfKillzone3(杀戮地带3的制作)
  2. php卡片式排版显示,css实现卡片式图片效果
  3. dynaform ansys ls-dyna
  4. [C/C++] 算法提高 5-3日历
  5. fedora11下,关于google音乐无法显示和校内网阳光牧场字体显示的问题
  6. win7下电脑与电视连接后如何各自独立播放声音
  7. css行间距 line-height
  8. uniapp——好看的球型、跃动的气球、发光的圆、发光按钮(边框,阴影,半圆角,半透明)、圆形多层阴影
  9. 【离散数学】第一章 笔记(完)
  10. 无中生有Photoshop CS6背景素材技法ps教程 [无中生有超多案例]