基于端口的虚拟主机在生产环境中的应用也不多见,仅偶尔会用到,一搬是为公司内部人员提供访问的,如页面的后台、CMS发布、phpmyadmin等。

1.  配置监听的端口

[root@NFS-Serverextra]# vi nginx_vhosts.conf

server {

        listen       8060;

        server_name  www.etiantian.org;

       location / {

            root   /data0/www/www;

            index  index.html index.htm;

            access_log /app/logs/www_access.logmain;

        }

   }

        ###

     server {

        listen    8070;

        server_name bbs.etiantian.org;

       location / {

            root   /data0/www/bbs;

            index  index.html index.htm;

              access_log /app/logs/bbs_access.logmain;

        }

   }

       ###

        server {

        listen       8080;  #此端口引起了后患

        server_name  blog.etiantian.org etiantian.org;

       location / {

            root   /data0/www/blog;

            index  index.html index.htm;

              access_log/app/logs/blog_access.log main;

        }

   }

 ###配置ngnix状态虚拟主机信息

server

{

 listen 80;

 server_name status.etiantian.org;

 location /{

 stub_status on;

 access_log off;

  }

}

[root@NFS-Serverextra]# ../../sbin/nginx -t

nginx:the configuration file /application/nginx-1.10.1/conf/nginx.confsyntax is ok

nginx:configuration file /application/nginx-1.10.1/conf/nginx.conftest is successful

[root@NFS-Serverextra]# ../../sbin/nginx -s reload

[root@NFS-Serverextra]# netstat -lnt|grep 8   #以上3个端口没有监听到,原因是8080端口冲突引起,tomcat设置的也是8080端口

tcp        0     0 0.0.0.0:32803               0.0.0.0:*                   LISTEN     

tcp        0     0 0.0.0.0:80                  0.0.0.0:*                   LISTEN     

tcp        0     0 :::32803                    :::*                        LISTEN     

tcp        0     0 ::ffff:127.0.0.1:8005      :::*                        LISTEN     

tcp        0     0 :::8009                    :::*                        LISTEN     

tcp        0     0 :::8080                     :::*                        LISTEN     

[root@NFS-Serverextra]# vi  nginx_vhosts.conf

server {

        listen       8060;

        server_name  www.etiantian.org;

       location / {

            root   /data0/www/www;

            index  index.html index.htm;

            access_log /app/logs/www_access.logmain;

        }

   }

        ###

     server {

        listen       8070;

        server_name bbs.etiantian.org;

       location / {

            root   /data0/www/bbs;

            index  index.html index.htm;

              access_log/app/logs/bbs_access.log main;

        }

   }

       ###

       server {

        listen       8081;

        server_name  blog.etiantian.org etiantian.org;

       location / {

            root   /data0/www/blog;

           index  index.html index.htm;

        }

  }

[root@NFS-Serverextra]# ../../sbin/nginx -t

nginx:the configuration file /application/nginx-1.10.1/conf/nginx.confsyntax is ok

nginx:configuration file /application/nginx-1.10.1/conf/nginx.conftest is successful

[root@NFS-Serverextra]# ../../sbin/nginx -s reload

[root@NFS-Serverextra]# netstat -lnt|grep 80

tcp        0     0 0.0.0.0:8060                0.0.0.0:*                   LISTEN     

tcp        0     0 0.0.0.0:32803               0.0.0.0:*                   LISTEN     

tcp        0     0 0.0.0.0:8070                0.0.0.0:*                   LISTEN     

tcp        0     0 0.0.0.0:80                  0.0.0.0:*                   LISTEN     

tcp        0     0 0.0.0.0:8081                0.0.0.0:*                   LISTEN     

tcp        0     0 :::32803                   :::*                        LISTEN     

tcp        0     0 ::ffff:127.0.0.1:8005      :::*                        LISTEN     

tcp        0     0 :::8009                    :::*                        LISTEN     

tcp        0     0 :::8080                     :::* 

 

访问测试:

通过浏览器访问如下3个地址:测试结果如下:

http://www.etiantian.org:8060/

http://www.etiantian.org:8070/

http://www.etiantian.org:8081/

 nginx基于端口的虚拟主机配置_第1张图片