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

发布时间:2020-08-04 18:55:16 作者:SiceLc
来源:网络 阅读:6730

Nginx虚拟主机应用

Nginx支持的虚拟主机有三种

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

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

基于端口的虚拟主机

[root@localhost 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          
[root@localhost conf]# cd /var/www/html/      //进入站点目录
[root@localhost html]# mkdir accp8080           //创建目录
[root@localhost html]# echo "this is accp8080 web" > accp8080/index.html   //编辑网页内容
[root@localhost html]# cd /usr/local/nginx/conf/    
[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   //重启服务
[root@localhost 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:

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

基于不同IP的虚拟主机

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

[root@localhost 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
[root@localhost 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
[root@localhost 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
[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    //重启服务
[root@localhost conf]# systemctl restart named   //重启DNS服务

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

推荐阅读:
  1. nginx域名配置虚拟主机
  2. Nginx虚拟主机配置实例

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

nginx linux centos 7 ginx inux

上一篇:这个锅,我们运维,不背! 开发和运维都该看看--技术人生系列第四十三期

下一篇:技术杂谈-再谈软硬SDN(2)

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》