冬天需要一把火 發表於 2020-12-17 17:03:00

Nginx基于多端口、多域名配置

<p><strong>一、基于端口访问</strong></p>
<p>在网卡只有一个,或者服务器资源有限的情况下有多个网站,就可以基于端口,或者基于域名来实现资源的访问。基于端口访问就是配置多个不同的端口,将域名指向同一个ip不同的端口来实现。</p>
<p>nginx.conf配置文件如下</p>
<p>#user nobody;<br>worker_processes8;</p>
<p>events {<br>    worker_connections1024;<br>}</p>
<p>http {<br>    include       mime.types;<br>    default_typeapplication/octet-stream;</p>
<p>sendfile&nbsp; &nbsp; &nbsp; &nbsp; on;</p>
<p>keepalive_timeout 65;</p>
<p>##指定不同端口访问配置server段第一个端口800</p>
<p><strong>server {</strong><br><strong>      listen       800;&nbsp; &nbsp; &nbsp;#第一端口800</strong><br><strong>      server_namelocalhost;</strong></p>
<p><strong>      location / {</strong><br><strong>            root   /www/ctm/xcy-project/yun-app-lives;&nbsp; &nbsp;##网站目录</strong><br><strong>            try_files $uri $uri/ /;</strong><br><strong>               indexindex.html index.htm;</strong></p>
<p><strong>      }</strong><br><strong>      error_page   500 502 503 504/50x.html;</strong><br><strong>      location = /50x.html {</strong><br><strong>            root   html;</strong><br><strong>      }</strong></p>
<p><strong>    }</strong></p>
<p>##指定不同端口访问配置server段第二个端口801</p>
<p><strong>server {</strong><br><strong>      listen       801;&nbsp; &nbsp;#第二个端口801</strong><br><strong>      server_namelocalhost;</strong></p>
<p><br><strong>      location / {</strong><br><strong>            root   /www/ctm/xcy-project/yun-app;&nbsp; &nbsp;#项目路径</strong><br><strong>             try_files $uri $uri/ /;</strong><br><strong>               indexindex.html index.htm;</strong></p>
<p><strong>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}</strong></p>
<p><strong>      error_page   500 502 503 504/50x.html;</strong><br><strong>      location = /50x.html {</strong></p>
<p><strong>          }</strong><br><strong>      &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</strong></p>
<p>}</p>
<p>保存重启</p>
<p><strong>测试访问800端口</strong></p>
<p>ip:800</p>
<p><img src="https://img2020.cnblogs.com/blog/1574107/202012/1574107-20201217154947002-683685712.png"></p>
<p>&nbsp;</p>
<p>测试访问801端口</p>
<p>ip:801</p>
<p><img src="https://img2020.cnblogs.com/blog/1574107/202012/1574107-20201217155414806-2062855036.png"></p>
<p>&nbsp;</p>
<p>以上就是基于同ip不同端口访问,域名指定相应ip及端口就好。</p>
<p><strong>二、基于不同域名访问</strong></p>
<p>一般都是在nginx.conf的尾部加入include,使用vhost这样每个网页用单独的配置。</p>
<p>nginx.cnof最尾部的括号内添加如下</p>
<p>include vhost/*.conf;</p>
<p><img src="https://img2020.cnblogs.com/blog/1574107/202012/1574107-20201217160403589-1692394395.png"></p>
<p>&nbsp;</p>
<p>配置文件的同级目录创建一个vhost文件夹</p>
<p><img src="https://img2020.cnblogs.com/blog/1574107/202012/1574107-20201217160527242-954283652.png"></p>
<p>进入vhost目录下创建两个文件,实现不同域名访问。文件名的最后要以.conf结尾。</p>
<p><img src="https://img2020.cnblogs.com/blog/1574107/202012/1574107-20201217164407534-349692899.png"></p>
<p>&nbsp;</p>
<p><strong>www.server111.com的配置文件如下</strong></p>
<p>##网站域名 www.server111.com<br>server {<br>      listen       80;<br>      server_namewww.server111.com;&nbsp; &nbsp; ##指定域名</p>
<p><br>      location / {<br>            root   /www/ctm/xcy-project/yun-app-lives; ##项目路径<br>             try_files $uri $uri/ /;<br>               indexindex.html index.htm;</p>
<p>      }</p>
<p>      error_page   500 502 503 504/50x.html;<br>      location = /50x.html {</p>
<p>      }<br>    }</p>
<p>&nbsp;<img src="https://img2020.cnblogs.com/blog/1574107/202012/1574107-20201217164516589-346738002.png"></p>
<p><strong>www.server222.com的配置文件如下</strong></p>
<p>其实只需要修改一下项目路径及域名即可。</p>
<p><strong><img src="https://img2020.cnblogs.com/blog/1574107/202012/1574107-20201217164612476-1178739473.png"></strong></p>
<p>配置完成保存,然后重启nginx</p>
<p>因为我们域名是自己编写的,所以需要添加到本地的hosts文件,不然是无法访问的。</p>
<p>C:\Windows\System32\drivers\etc</p>
<p><img src="https://img2020.cnblogs.com/blog/1574107/202012/1574107-20201217165902550-1074421046.png"></p>
<p>&nbsp;</p>
<p>添加你自己设置的域名+ip ,尾部添加。</p>
<p><img src="https://img2020.cnblogs.com/blog/1574107/202012/1574107-20201217170110973-568557318.png"></p>
<p>&nbsp;</p>
<p>&nbsp;保存然后就可以测试访问</p>
<p>www.server111.com</p>
<p><img src="https://img2020.cnblogs.com/blog/1574107/202012/1574107-20201217165606690-208597487.png"></p>
<p>测试访问</p>
<p>www.server222.com</p>
<p><img src="https://img2020.cnblogs.com/blog/1574107/202012/1574107-20201217165532005-1967153032.png"></p>

</div>
<div id="MySignature" role="contentinfo">
    <h5>作者: 阿隆</h5>
<h5>不足之处,欢迎技术指导。</h5><br><br>
来源:https://www.cnblogs.com/sxshaolong/p/14150618.html
頁: [1]
查看完整版本: Nginx基于多端口、多域名配置