详述Linux系统中Nginx虚拟主机的配置

Nginx虚拟主机应用

Nginx支持的虚拟主机有三种

  • 基于域名的虚拟主机.
  • 基于IP的虚拟主机
  • 基于端口的虚拟主机

通过"server{}"配置段实现

  • 本篇实验接着上一篇搭建Nginx服务继续搭建,前面Nginx的编译安装不在介绍

    基于域名的虚拟主机

    [[email protected] nginx-1.12.2]# mkdir -p /var/www/html/accp   //递归创建accp网页站点目录
    [[email protected] nginx-1.12.2]# mkdir -p /var/www/html/kgc     //递归创建kgc网页站点目录
    [[email protected] nginx-1.12.2]# cd /var/www/html/      //进入站点目录
    [[email protected] html]# ls           //查看
    accp  kgc
    [[email protected] html]# echo "this is kgc web" > kgc/index.html    //创建站点文件
    [[email protected] html]# echo "this is accp web" > accp/index.html  //创建站点文件
    [[email protected] html]# ls accp/   //查看accp站点目录
    index.html
    [[email protected] html]# ls kgc/   //查看kgc站点目录
    index.html
    [[email protected] conf]# vim /etc/named.rfc1912.zones     //编辑DNS服务区域配置文件
    ...//省略部分内容...
    zone "kgc.com" IN {
        type master;
        file "kgc.com.zone";
        allow-update { none; };
    };
    zone "accp.com" IN {
        type master;
        file "accp.com.zone";                //添加accp网页域名解析
        allow-update { none; };
    };
    ...//省略部分内容...
    :wq
    [[email protected] conf]# cd /var/named/
    [[email protected] named]# cp -p named.localhost kgc.com.zone    //复制区域数据文件
    [[email protected] named]# vim kgc.com.zone              //编辑kgc区域数据文件
    $TTL 1D
    @       IN SOA  @ rname.invalid. (
                                        0       ; serial
                                        1D      ; refresh
                                        1H      ; retry
                                        1W      ; expire
                                        3H )    ; minimum
        NS      @
        A       127.0.0.1
    www IN  A       192.168.144.133
    :wq
    [[email protected] named]# cp -p kgc.com.zone accp.com.zone
    //复制kgc区域数据文件保持权限不变,复制为accp区域数据文件
    [[email protected] named]# systemctl restart named          //重启DNS服务
    [[email protected] init.d]# cd /usr/local/nginx/conf/
    [[email protected] conf]# vim nginx.conf               //编辑Nginx主配置文件
    ...//省略部分内容...
    server {
        listen       80;
        server_name  www.kgc.com;         //设置kgc域名访问条目
        charset utf-8;                   //更改字符串类型
        access_log  logs/www.kgc.com.access.log;    //更改日志文件名称
        location / {
            root   /var/www/html/kgc;           //更改站点位置
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
    server {
        listen       80;
        server_name  www.accp.com;
        charset utf-8;
        access_log  logs/www.accp.com.access.log;
        location / {
            root   /var/www/html/accp;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
    ...//省略部分内容...
    :wq
    [[email protected] conf]# nginx -t    //检测语法
    nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
    nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
    [[email protected] conf]# service nginx restart     //重启服务
    [[email protected] conf]# systemctl restart named   //重启DNS服务
  • 在客户机中测试网页

基于端口的虚拟主机

[[email protected] conf]# vim nginx.conf    //编辑Nginx主配置文件
...//省略部分内容...
#    server {
#        listen       80;
#        server_name  www.kgc.com;
#        charset utf-8;
#        access_log  logs/www.kgc.com.access.log;
#        location / {
#            root   /var/www/html/kgc;             //注释掉此部分内容
#            index  index.html index.htm;
#        }
#        error_page   500 502 503 504  /50x.html;
#        location = /50x.html {
#            root   html;
#        }
#    }

  server {
        listen       192.168.144.133:80;          //端口条目钱添加IP地址
        server_name  www.accp.com;
        charset utf-8;
        access_log  logs/www.accp.com.access.log;
        location / {
            root   /var/www/html/accp;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

    server {                                         //复制上面部分的内容
        listen       192.168.144.133:8080;           //设置相同IP监听不同端口
        server_name  www.accp.com;
        charset utf-8;
        access_log  logs/www.accp8080.com.access.log;    //更改日志文件名称
        location / {
            root   /var/www/html/accp8080;         //整改站点文件目录名称
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
...//省略部分内容...
:wq
[[email protected] conf]# cd /var/www/html/      //进入站点目录
[[email protected] html]# mkdir accp8080           //创建目录
[[email protected] html]# echo "this is accp8080 web" > accp8080/index.html   //编辑网页内容
[[email protected] html]# cd /usr/local/nginx/conf/
[[email protected] conf]# nginx -t             //检测语法
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[[email protected] conf]# service nginx restart   //重启服务
[[email protected] conf]# netstat -ntap | grep 80     //查看端口
tcp        0      0 192.168.144.133:8080    0.0.0.0:*            LISTEN      11967/nginx: master
tcp        0      0 192.168.144.133:80      0.0.0.0:*            LISTEN      11967/nginx:
  • 在客户机中访问测试

基于不同IP的虚拟主机

  • 首先给Linux虚拟机添加网卡

[[email protected] conf]# ifconfig     //查看新添加的网卡信息,并记录IP地址
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.144.133  netmask 255.255.255.0  broadcast 192.168.144.255
        inet6 fe80::a85a:c203:e2e:3f3c  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:5b:d3:a0  txqueuelen 1000  (Ethernet)
        RX packets 67362  bytes 72060261 (68.7 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 16836  bytes 1825469 (1.7 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens36: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.144.145  netmask 255.255.255.0  broadcast 192.168.144.255
        inet6 fe80::deb1:3cec:3e26:5ec2  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:5b:d3:aa  txqueuelen 1000  (Ethernet)
        RX packets 6  bytes 926 (926.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 27  bytes 4513 (4.4 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
[[email protected] conf]# vim /var/named/kgc.com.zon       //更改DNS区域数据文件中kgc网页解析的IP地址
$TTL 1D
@       IN SOA  @ rname.invalid. (
                                        0       ; serial
                                        1D      ; refresh
                                        1H      ; retry
                                        1W      ; expire
                                        3H )    ; minimum
        NS      @
        A       127.0.0.1
www IN  A       192.168.144.145      //更改为新添加网卡的IP地址
:wq
[[email protected] conf]# vim nginx.conf    //编辑主配置文件
...//省略部分内容...
 server {
        listen       192.168.144.145:80;       //去掉上面配置基于端口时全面添加的注释,并添加监听的IP地址
        server_name  www.kgc.com;
        charset utf-8;
        access_log  logs/www.kgc.com.access.log;
        location / {
            root   /var/www/html/kgc;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }   

    server {
        listen       192.168.144.133:80;
        server_name  www.accp.com;
        charset utf-8;
        access_log  logs/www.accp.com.access.log;            //保持不变
        location / {
            root   /var/www/html/accp;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    } 

#    server {
#        listen       192.168.144.133:8080;
#        server_name  www.accp.com;
#        charset utf-8;
#        access_log  logs/www.accp8080.com.access.log;          //注释掉此部分内容
#        location / {
#            root   /var/www/html/accp8080;
#            index  index.html index.htm;
#        }
#        error_page   500 502 503 504  /50x.html;
#        location = /50x.html {
#            root   html;
#        }
#    }
...//省略部分内容...
:wq
[[email protected] conf]# nginx -t          //检测语法
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[[email protected] conf]# service nginx restart    //重启服务
[[email protected] conf]# systemctl restart named   //重启DNS服务
  • 在客户机中访问测试(此处建议新开启一台客户机做访问测试)

原文地址:https://blog.51cto.com/14473285/2449542

时间: 2024-07-30 12:59:45

详述Linux系统中Nginx虚拟主机的配置的相关文章

linux笔记之Nginx虚拟主机的配置

1.源码编译安装Nginx.并配置基于端口的虚拟主机 配置要求:主机ip地址为172.16.249.96,要求分别为本机的80端口.8080端口做虚拟主机. 在第一章中已经讲解怎么编译安装Nginx,所有这里就不再演示安装过程了,直接基于已经安装的基础上,配置基于端口的虚拟主机. 步骤一:安装nginx.vim使得使用vim编辑nginx配置文件时有语法高亮(这不是必须的,只是为了方便编辑配置文件). (1)下载nginx.vim(下载页面:http://www.vim.org/scripts/

网站是PHP程序写的,我为什么说要选linux系统的php虚拟主机?

本文标签:  php程序 LAMP构架 php网站 apache配置 虚拟主机   服务器 在国内,空间市场可谓之"枝繁叶茂",有关php空间.php虚拟主机在百度搜索一下有多达到上百万条记录,每条记录无不都在告诉你,选我吧,选我把,我是最好的?我是最稳定的?那么,我们如何在这良莠不分的海量信息中购买php空间类?一个选购php空间的原则:认准linux系统永远都是php语言最好的伙伴,php程序只有在linux系统上才能全力发挥它的"火力",要买就买linux空间

linux系统中apache虚拟目录配置

在搭建网站的时候,我们会经常在同一台服务器上面搭建多个站点,这时候就需要用到apache的虚拟机知识.下面就linux系统下apache虚拟目录的配置说简要说明: 1.源代码安装apache + PHP + MySQL(请查考我的博文lamp环境搭建): 2.在本地计算机的hosts文件加入如下行: 192.168.137.10 erp.100msh.com 192.168.137.10 mopadmin.100msh.com 然后cmd中 ping erp.100msh.com 检查是否网络通

详述Linux系统中搭建Nginx动静分离

Nginx动静分离介绍 Nginx的静态处理能力很强,但是动态处理能力不足,因此,在企业中常用动静分离技术 针对PHP的动静分离 静态页面交给Nginx处理 动态页面交给PHP-FPM模块或Apache处理 在Nginx的配置中,是通过location配置段配合正则匹配实现静态与动态页面的不同处理方式 反向代理原理 Nginx不仅能作为Web服务器,还具有反向代理.负载均衡和缓存的功能 Nginx通过proxy模块实现将客户端的请求代理至上游服务器,此时nginx与. 上游服务器的连接是通过ht

Linux系统中nginx日志每天定时切割实现方法详解

本文和大家分享的是使用Linux中自带的命令logrotate对Nginx日志进行切割相关实现方法,希望帮助大家更好的学习linux系统. Nginx安装目录:/usr/local/nginx/ Nginx日志目录:/usr/local/nginx/logs/./usr/local/nginx/logs/nginx_logs/ 1.添加nginx日志切割脚本 cd /etc/logrotate.d #进入目录 vi /etc/logrotate.d/nginx #编辑脚本 /usr/local/

详述Linux系统中Apache网页与安全优化(二)

网页缓存 配置网页的缓存时间 通过mod_ expire模块配置Apache,使网页能在客户端浏览器缓存一段时间,以避免重复请求 启用mod_ expire模块后,会自动生成页面头部信息中的Expires标签和Cache-Control标签,从而降低客户端的访问频率和次数,达到减少不必要的流量和增加访问速度的目的 启用网页缓存功能步骤 查看是否安装mod_ expire模块 修改配置文件启用缓存功能 抓包测试 查看是否安装了mod_ expire模块 /usr/local/apache/bin/

Nginx 虚拟主机 VirtualHost 配置

Nginx 是一个轻量级高性能的 Web 服务器, 并发处理能力强, 对资源消耗小, 无论是静态服务器还是小网站, Nginx 表现更加出色, 作为 Apache 的补充和替代使用率越来越高. 我在<Apache 虚拟主机 VirtualHost 配置>介绍了在不同操作系统上使用 Apahce 虚拟主机的方法, 还有那么些朋友想知道 Nginx 虚拟主机配置方法, 本文作为补充也介绍如何 Nginx 上添加虚拟主机. 绝大多数的 Nginx 运行在 Linux 机器上, 虽然有 Windows

详述Linux系统中Apache网页深入优化

ab压力测试 Apache自带压力测试工具ab,简单易用,且可以模拟各种条件对Web服务器发起测试请求 ab工具可以直接在Web服务器本地发起测试请求,这对于需要了解服务器的处理性能至关重要,因为它不包括数据的网络传输时间以及用户PC本地的计算时间,从而可以通过观测各种时间指标判断Web服务器的性能,以便进行参数的优化调整 ab压力测试工具 在进行性能调整优化过程中,可用ab压力测试工具进行优化效果的测试 优化前先使用ab进行压力测试 优化后,重启服务,再使用ab进行压力测试 对比两次测试的结果

详述Linux系统中搭建LNMP架构+Discuz论坛

LNMP架构解读 LNMP平台就是Linux.Ngnix.MySQL.PHP的组合架构,需要Linux服务器.MySQL 数据库.PHP解析环境 搭建Nginx服务 下载Nginx源码包 Nginx源码包下载 在Linux虚拟机中挂载存放源码包的目录 [[email protected] ~]# mount.cifs //192.168.100.10/lnmp /mnt/ //挂载目录 Password for [email protected]//192.168.100.10/lnmp: [[