Nginx基于端口和IP的虚拟主机实战

一、Nginx基于端口的虚拟主机实战

1.1配置nginx配置文件

[root@web01 conf]# vi nginx.conf  #进入配置文件

worker_processes  1;

events {

    worker_connections  1024;

}

http {

    include       mime.types;

    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;

    server {

        listen       80;  #80为默认端口

        server_name  www.etiantian.org;  #www.etiantian.org为域名

        location / {

            root   html/www;  #内容存放区域

            index  index.html index.htm;

        }

    }

    server {

        listen       81;

        server_name  bbs.etiantian.org;

        location / {

            root   html/bbs;

            index  index.html index.htm;

        }

    }

  server {

        listen       82;

        server_name  blog.etiantian.org;

        location / {

            root   html/blog;

            index  index.html index.htm;

        }

    }

}

"nginx.conf" 34L, 751C written

1.2检查语法并平滑重启

[root@web01 conf]# /application/nginx/sbin/nginx -t

nginx: the configuration file /application/nginx-1.6.3//conf/nginx.conf syntax is ok

nginx: configuration file /application/nginx-1.6.3//conf/nginx.conf test is successful

[root@web01 conf]# /application/nginx/sbin/nginx -s reload

2.Linux客户端查看:
[root@m01 ~]# curl www.etiantian.org  #域名后面不接端口号是因为第一个默认端口就是80

www

[root@m01 ~]# curl www.etiantian.org:81

bbs

[root@m01 ~]# curl www.etiantian.org:82

blog

3.Windows客户端浏览器访问:

 


 


 

二、Nginx基于ip的虚拟主机实战

1. 为服务器设置多个ip:

[root@web01 ~]# ifconfig eth0:0 10.0.0.81/24 up #为服务器设置一个网卡的多个ip

[root@web01 ~]# ip addr add 10.0.0.82/24 dev eth0 label eth0:1

[root@web01 ~]# ifconfig #查看配置

eth0:0    Link encap:Ethernet  HWaddr 00:0C:29:7F:05:C6  

          inet addr:10.0.0.81  Bcast:10.0.0.255  Mask:255.255.255.0

          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

 

eth0:1    Link encap:Ethernet  HWaddr 00:0C:29:7F:05:C6  

          inet addr:10.0.0.82  Bcast:0.0.0.0  Mask:255.255.255.0

          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

2. 修改配置文件并启动

[root@web01 ~]# cp /application/nginx/conf/nginx.conf nginx.conf.V4  #备份配置文件

[root@web01 ~]# vi /application/nginx/conf/nginx.conf  #修改配置文件

worker_processes  1;

events {

    worker_connections  1024;

}

http {

    include       mime.types;

    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;

    server {

        listen      10.0.0.8:80;

        server_name  www.etiantian.org;

        location / {

            root   html/www;

            index  index.html index.htm;

        }

    }

    server {

        listen       10.0.0.81:80;

        server_name  www.etiantian.org;

        location / {

            root   html/bbs;

            index  index.html index.htm;

        }

    }

  server {

        listen       10.0.0.82:80;

        server_name  www.etiantian.org;

        location / {

            root   html/blog;

            index  index.html index.htm;

        }

    }

}

[root@web01 ~]# /application/nginx/sbin/nginx -t  #检查语法错误

[root@web01 ~]# /application/nginx/sbin/nginx -s stop  #停止nginx

[root@web01 ~]# /application/nginx/sbin/nginx  #重启nginx

3. 客户端测试

[root@m01 ~]# curl 10.0.0.8

www

[root@m01 ~]# curl 10.0.0.81

bbs

[root@m01 ~]# curl 10.0.0.82

blog


 

猜你喜欢

转载自blog.csdn.net/qq_41816540/article/details/80879921