454luikty 发表于 2017-8-21 10:13:39

nginx基于端口的虚拟主机实战

nginx基于端口的虚拟主机,就是以端口来区分。只要端口不同就可以。

    实战1:    端口不一样,域名不一样。编辑nginx.conf配置文件,不通域名修改为不通端口,listen 80 81 82三个端口

   
# pwd
/application/nginx/conf
# cat nginx.conf
worker_processes1;
events {
    worker_connections1024;
}
http {
    include       mime.types;
    default_typeapplication/octet-stream;
    sendfile      on;
    keepalive_timeout65;
    server {
      listen       80;
      server_namewww.iyunv.org;
      location / {
            root   html/www;
            indexindex.html index.htm;
      }
    }
    server {
      listen       81;
      server_namebbs.iyunv.org;
      location / {
            root   html/bbs;
            indexindex.html index.htm;
      }
    }
    server {
      listen       82;
      server_nameblog.iyunv.org;
      location / {
            root   html/blog;
            indexindex.html index.htm;
      }
    }
}

    1.1    查看nginx服务是否启动(查看进程和端口都可以)

   
# ps -ef|grep nginx
root       1437   13130 10:36 pts/1    00:00:00 grep nginx

    如上查看nginx服务对应的进程不存在就说明nginx服务没有启动,启动nginx服务。
   
# /application/nginx/sbin/nginx

    1.2    验证

      1.2.1    windows的hosts文件添加本地dns解析

      10.0.0.8 www.iyunv.org bbs.iyunv.org blog.iyunv.org

      1.2.2    验证

            分别在windows的ie中输入地址www.iyunv.org:80

            bbs.iyunv.org:81    blog.iyunv.org:82    这三个网址,看是否有对应的内容,有就说明配置成功了。(原理就是域名解析成ip,ip再对应端口)


    实战2:    nginx站点都用一个域名www.iyunv.org,但是端口分别是80、81、82

   
# pwd
/application/nginx/conf
# cat nginx.conf
worker_processes1;
events {
    worker_connections1024;
}
http {
    include       mime.types;
    default_typeapplication/octet-stream;
    sendfile      on;
    keepalive_timeout65;
    server {
      listen       80;
      server_namewww.iyunv.org;
      location / {
            root   html/www;
            indexindex.html index.htm;
      }
    }
    server {
      listen       81;
      server_namewww.iyunv.org;
      location / {
            root   html/bbs;
            indexindex.html index.htm;
      }
    }
    server {
      listen       82;
      server_namewww.iyunv.org;
      location / {
            root   html/blog;
            indexindex.html index.htm;
      }
    }
}

    2.1    检查nginx语法,并平滑重启
   
# /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
   
# /application/nginx/sbin/nginx -s reload
页: [1]
查看完整版本: nginx基于端口的虚拟主机实战