nginx认识、nginx虚拟主机【基于域名虚拟主机、基于IP地址虚拟主机、基于端口虚拟主机】


配置DNS域名

nginx认识
Nginx是一款高性能的Web服务器软件,主要用于提供网上信息浏览服务,为高并发网站的应用场景而设计,可以在Linux、macOS和Windows等操作系统中运行,它的优点包括性能高、稳定性好、结构模块化、配置简单以及资源消耗非常低等。拥有HTTPS访问、gzip压缩、虚拟主机和URL重写等功能,不但可以搭配FastCGI程序处理动态请求,还可以用于代理、反向代理、负载均衡和缓存服务器等功能。

在这里插入图片描述

nginx网页优化

基于域名虚拟主机

配置DNS

root@localhost ~]# yum -y install bind
[root@localhost ~]# vim /etc/named.conf 

options {
        listen-on port 53 { any; };
        listen-on-v6 port 53 { ::1; };
        directory       "/var/named";
        dump-file       "/var/named/data/cache_dump.db";
        statistics-file "/var/named/data/named_stats.txt";
        memstatistics-file "/var/named/data/named_mem_stats.txt";
        recursing-file  "/var/named/data/named.recursing";
        secroots-file   "/var/named/data/named.secroots";
        allow-query     { any; };

[root@localhost ~]# vim /etc/named.rfc1912.zones 
zone "benet.com" IN {
        type master;
        file "benet.com.zone";
        allow-update { none; };
};
zone "accp.com" IN {
        type master;
        file "accp.com.zone";
        allow-update { none; };
};

[root@localhost ~]# cd /var/named/
[root@localhost named]# cp -p named.localhost benet.com.zone
[root@localhost named]# vim benet.com.zone 
www  IN A      192.168.136.10
[root@localhost named]# setenforce 0
[root@localhost named]# iptables -F
[root@localhost named]# systemctl stop firewalld
[root@localhost named]# systemctl start named

image-20200808141104046

安装环境

[root@localhost named]# yum -y install gcc gcc-c++ pcre-devel zlib-devel

安装nginx

[root@localhost named]# tar zxvf nginx-1.12.2.tar.gz -C /opt/
[root@localhost opt]# cd nginx-1.12.2/
[root@localhost opt]#useradd -M -s /sbin/nologin nginx
[root@localhost nginx-1.12.2]# ./configure \
> --prefix=/usr/local/nginx \
> --user=nginx \
> --group=nginx \
> --with-http_stub_status_module
[root@localhost nginx-1.12.2]#make && make install
[root@localhost nginx-1.12.2]# cd /usr/local/nginx/
[root@localhost nginx]# ls
conf配置文件  html 默认站点 logs日志  sbin

制作管理脚本


[root@localhost nginx]# ln -s /usr/local/nginx/sbin/* /usr/local/sbin/   便于系统可以识别
 [root@localhost nginx]# cd /etc/init.d/   重建服务
[root@localhost init.d]# vim nginx    创建管理脚本
#!/bin/bash
# chkconfig: - 99 20
#description: nginx service Control Script
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
case "$1" in
  start)
    $PROG
    ;;
   stop)
    kill -s QUIT $(cat $PIDF)
    ;;
    restart)
    $0 stop
    $0 start
    ;;
    reload)
      kill -s HUP $(cat $PIDF)
      ;;
    *)
     echo "Usage: $0 {start|stop|restart|reload}"
     exit 1
 esac
exit 0
[root@localhost init.d]# chmod +x nginx
[root@localhost init.d]# chkconfig --add nginx   便于servers管理
[root@localhost init.d]# yum -y install net-tools
[root@localhost init.d]# netstat -ntap | grep 80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      25954/nginx: master 
tcp        0      0 192.168.136.10:22       192.168.136.2:59246     ESTABLISHED 8049/sshd: root@pts 
tcp        0      0 192.168.136.10:48214    59.111.0.251:80         TIME_WAIT   -
[root@localhost init.d]#service nginx star

image-20200808141200030

不同的域名访问不同得到网站

[root@localhost named]# cd /usr/local/nginx/conf/
[root@localhost conf]# vim nginx.conf
        server_name  www.benet.com;     

        charset utf-8;   开启字符级
        access_log  logs/www.kgc.com.access.log;   去掉#开启日志  加入www.kgc.com 去掉main
 
 location / {
            root   /var/www/html/kgc;         开启站点
            index  index.html index.htm;
删除21到24 行

删除44到47

删除49到56 行

删除49到54 行

删除49到56行

创建虚拟主机

 46     server {
 47         listen       80;    监听地址
 48         server_name  www.benet.com;     域名
 49         charset utf-8;        对应字符级
 50         access_log  logs/www.benet.com.access.log;    访问日志
 51         location / {
 52             root   /var/www/html/benet;    站点目录
 53             index  index.html index.htm;    支持文件类型
 54         }
 55         error_page   500 502 503 504  /50x.html;         错误故障
 56         location = /50x.html {
 57             root   html;
 58         }
 59     }

在这里插入图片描述

开启服务

[root@localhost 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

写入网页

[root@localhost ~]# mkdir -p /var/www/html/kgc
[root@localhost ~]# mkdir -p /var/www/html/benet
[root@localhost html]# echo "this is accp web" > kgc/index.html
[root@localhost html]# echo "this is benet web" > benet/index.html
[root@localhost conf]#  service nginx restart

image-20200808142256349

基于不同端口不同配置

[root@localhost html]# cd /usr/local/nginx/conf/
[root@localhost conf]# vim nginx.conf

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

    server {
        listen        192.168.136.10:8080;
        server_name  www.benet.com;
        charset utf-8;
        access_log  logs/www.benet8080.com.access.log;
        location / {
            root   /var/www/html/benet8080;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

在这里插入图片描述

写入网页

[root@localhost conf]# cd /var/www//html/
[root@localhost html]# mkdir benet8080
[root@localhost html]# echo "this is benet8080 web" > benet8080/index.html
[root@localhost 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

image-20200808142800400

image-20200808142736672

访问不同地址的域名

添加一个网卡ens36并查看一下ens36网卡信息,修改一下区域配置文件

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-6IsvCphd-1597107624710)(D:\新建文件夹\qq4EE558CF6F90A08FC3A2FBB01BA179E8\f9eed4a3aa434227a8c647f2f9a74c17\clipboard.png)]

配置DNS区域配置文件

[root@localhost named]# vim kgc.com.zone 
www  IN A     192.168.136.136
[root@localhost named]# systemctl restart named

在win10中查看一下解析地址

image-20200808144004092

注释掉8080端口的内容,保证不影响

image-20200808144359784

修改一下kgc的ip地址

[root@localhost named]# cd /usr/local/nginx/conf/
[root@localhost conf]# vim nginx.conf

image-20200808144933744

验证一下是否成功并开启

[root@localhost 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
[root@localhost conf]# service nginx restart

image-20200808145706906

image-20200808145723295

访问不同域名的IP地址

[root@localhost conf]# vim nginx.conf

image-20200808150459273

开启服务

[root@localhost conf]# service nginx restart

开一台win7的服务器

解析一下地址

image-20200808150902592

网页查看一下

image-20200808150709081

image-20200808150757017

访问不同域名的IP地址

[root@localhost conf]# vim nginx.conf

在这里插入图片描述

开启服务

[root@localhost conf]# service nginx restart

`

开一台win7的服务器

解析一下地址
在这里插入图片描述

网页查看一下

在这里插入图片描述

win10的网页查看
在这里插入图片描述

结论:当我们配置没有问题时候可以找别的方法去解决,比如换一台客户终端测试(可能是客户端的问题)

猜你喜欢

转载自blog.csdn.net/weixin_47151717/article/details/107927359