本篇介绍N多种web容器共享80端口方法,用上大名鼎鼎的NGINX。提到IIS,我们知道它只能安装在WINDOWS系统,所以本篇重在讲解windows系统的安装和配置.

步骤一:

下载和安装nginx

到官网下载nginx软件,网址:nginx: download

生产环境尽量选择stable版本的

把下载的压缩文件解压到指定文件夹

步骤二:

启动和停止nginx

打开一个cmd命令窗口,切换到刚解压缩的目录,输入 nginx 或 nginx.exe 或者 start nginx ,即可启动nginx

这个时候到浏览器地址栏输入 http://127.0.0.1/ 或 http://localhost 回车,可以看到nginx已经工作了

输入nginx命令  nginx -s stop (快速停止nginx)  或  nginx -s quit (完整有序的停止nginx)
可以终止nginx。

步骤三:

做好了以上准备工作,现在进入本篇最核心的环节,配置nginx让它可以多个域名共享80端口。在conf目录中找到nginx.conf并打开它。

在nginx.conf中添加域名和对应的本地应用,这里用的是本机不同端口对应不同应用。nginx还有很多种用法,比如多个域名对应一个应用、多个域名对应多个ip地址等等。这里只介绍不同端口的。

比如myweb1是IIS中的应用,使用9000端口;myweb2是TOMCAT中的应用使用9001端口。

 server{
        listen 80;
        server_name www.myweb1.com;
        location / {
            #降价联盟
            proxy_pass http://localhost:9000;
        }
        ##### other directive
    }
    
    server{
        listen 80;
        server_name myweb1.com;
        location / {
            #降价联盟
            proxy_pass http://localhost:9000;
        }
        ##### other directive
    }
    
    server{
        listen 80;
        server_name www.myweb2.com;
        location / {
            #xwnlx
            proxy_pass http://localhost:9001;
        }
        ##### other directive
    }
    
    server{
        listen 80;
        server_name myweb2.com;
        location / {
            #xwnlx
            proxy_pass http://localhost:9001;
        }
        ##### other directive
    }

 

工作原理:

通过域名+80端口,找到Nginx服务

Nginx通过server_name域名,进行区分,转向哪个location.

proxy_pass,反向代理,地址可以用localhost,也可以用IP或者域名