码迷,mamicode.com
首页 > 其他好文 > 详细

Nginx虚拟主机配置实例

时间:2019-12-24 20:36:01      阅读:69      评论:0      收藏:0      [点我收藏+]

标签:nginx   com   rest   重要   网卡   命令   star   http   stop   

Nginx虚拟主机

结合上篇文章:手工编译NginxNginx虚拟主机的搭建过程,虚拟主机的概念在之前的Apache虚拟主机搭建实验时已讲述过有关知识点,原文链接:Apache web 虚拟主机

结合上篇文章的配置进行下面的配置操作(Nginx服务是开启状态)

[root@localhost named]# netstat -natp | grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 79214/nginx: master

Nginx虚拟主机配置

1.域名解析配置(环境准备)

[root@localhost ~]# yum install -y bind
...//省略部分内容
  dhclient.x86_64 12:4.2.5-77.el7.centos                                         
  dhcp-common.x86_64 12:4.2.5-77.el7.centos                                      
  dhcp-libs.x86_64 12:4.2.5-77.el7.centos                                        

Complete!
[root@localhost ~]# vim /etc/named.conf 
[root@localhost ~]# head -21 /etc/named.conf |tail 
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 
[root@localhost ~]# vim /etc/named.rfc1912.zones 
[root@localhost ~]# head -34 /etc/named.rfc1912.zones | tail 
zone "ll.com" IN {
        type master;
        file "ll.com.zone";
        allow-update { none; };
};

zone "cc.com" IN {
        type master;
        file "cc.com.zone";
        allow-update { none; };
[root@localhost ~]# cd /var/named/
[root@localhost named]# ls
data  dynamic  named.ca  named.empty  named.localhost  named.loopback  slaves
[root@localhost named]# cp -p named.localhost ll.com.zone
[root@localhost named]# vim ll.com.zone 
[root@localhost named]# cp -p ll.com.zone cc.com.zone
[root@localhost named]# cat ll.com.zone 
$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.68.145
[root@localhost named]# cat cc.com.zone 
$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.68.145

[root@localhost named]# systemctl start named
[root@localhost named]# systemctl stop firewalld.service 
[root@localhost named]# setenforce 
usage:  setenforce [ Enforcing | Permissive | 1 | 0 ]
[root@localhost named]# setenforce 0

2.在win10虚拟机上使用nslookup命令测试是否正常解析

技术图片

3.创建站点

[root@localhost ~]# mkdir -p /var/www/html/ll
[root@localhost ~]# mkdir -p /var/www/html/cc
[root@localhost ~]# cd /var/www/html/
[root@localhost html]# ls
cc  ll
[root@localhost html]# echo "this is ll test web" > ll/index.html
[root@localhost html]# echo "this is cc test web" > cc/index.html
[root@localhost html]# ls ll/
index.html
[root@localhost html]# ls cc/
index.html

4.基于不同域名的服务解析设置

[root@localhost html]# cd /usr/local/nginx/conf/
[root@localhost conf]# vim /usr/local/nginx/conf/nginx.conf
[root@localhost conf]# sed -n ‘35,63p‘ nginx.conf
    server {
        listen       80;
        server_name  www.ll.com;
        charset utf-8;
        access_log  logs/www.ll.com.access.log;
        location / {
            root   /var/html/ll;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

    server {
        listen       80;
        server_name  www.cc.com;
        charset utf-8;
        access_log  logs/www.cc.com.access.log;
        location / {
            root   /var/html/cc;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   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
[root@localhost conf]# service nginx restart 

5.不同域名的测试

技术图片

技术图片

Nginx基于不同端口访问

继续根据上面的第四步的配置

[root@localhost conf]# sed -n ‘35,63p‘ nginx.conf
    server {
        listen       192.168.68.144:80;
        server_name  www.ll.com;
        charset utf-8;
        access_log  logs/www.ll.com.access.log;
        location / {
            root   /var/www/html/ll;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

    server {
        listen      192.168.68.144:8080;
        server_name  www.cc.com;
        charset utf-8;
        access_log  logs/www.cc8080.com.access.log;
        location / {
            root   /var/www/html/cc8080;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
     nginx -t
[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 

检测:

技术图片

Nginx基于不同IP地址访问

添加一块网卡选择nat模式

我的是192.168.68.150

1.修改区域数据配置文件

[root@localhost conf]# vim /var/named/cc.com.zone 
[root@localhost conf]# cat /var/named/cc.com.zone 
$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.68.150
[root@localhost conf]# systemctl restart named

查看解析是否成功:

技术图片

2.更改配置文件

[root@localhost conf]# vim nginx.conf
[root@localhost conf]# sed -n ‘35,63p‘ nginx.conf
    server {
        listen      192.168.68.144:80;
        server_name  www.ll.com;
        charset utf-8;
        access_log  logs/www.ll.com.access.log;
        location / {
            root   /var/www/html/ll;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

    server {
        listen      192.168.68.150:80;
        server_name  www.cc.com;
        charset utf-8;
        access_log  logs/www.cc.com.access.log;
        location / {
            root   /var/www/html/cc;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   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
[root@localhost conf]# service nginx restart 

检查测试:

技术图片

技术图片

总结

本文主要是通过Nginx手工编译安装的基础上对Nginx的虚拟主机的相关配置,分别对应的是基于不同域名、不同端口和不同ip进行的相关配置。重要的是对Nginx的配置文件nginx.conf的配置。这里的域名解析的相关配置需要比较娴熟。

下一篇我们将介绍LNMP架构的搭建过程

Nginx虚拟主机配置实例

标签:nginx   com   rest   重要   网卡   命令   star   http   stop   

原文地址:https://blog.51cto.com/14557673/2461421

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!