Nginx虚拟主机的六种配置指南
<div id="navCategory"><h5 class="catalogue">目录</h5><ul class="first_class_ul"><li>简介</li><li>一、访问状态统计配置</li><li>二、基于授权的访问控制</li><li> 三、基于客户端的访问控制 </li><li>四、基于域名的 Nginx 虚拟主机</li><li> 五、基于IP 的 Nginx 虚拟主机</li><li> 六、基于端口的 Nginx 虚拟主机</li></ul></div><p class="maodian"></p><h2>简介</h2><p>利用虚拟主机,不用为每个要运行的网站提供一台单独的Nginx服务器或单独运行一组Nginx进程。虚拟主机提供了在同一台服务器、同一组Nginx进程上运行多个网站的功能。</p>
<p class="maodian"></p><h2>一、访问状态统计配置</h2>
<p>查看已安装的 Nginx 是否包含 HTTP_STUB_STATUS 模块</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" height="81" src="https://zhuji.jb51.net/uploads/allimg/20250909/2-250Z9114309514.png" width="693" /></p>
<p>修改 nginx.conf 配置文件,指定访问位置并添加 stub_status 配置</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" height="42" src="https://zhuji.jb51.net/uploads/allimg/20250909/2-250Z9114310537.png" width="579" /></p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" height="278" src="https://zhuji.jb51.net/uploads/allimg/20250909/2-250Z9114315W7.png" width="546" /></p>
<p>重启服务,访问测试</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" height="59" src="https://zhuji.jb51.net/uploads/allimg/20250909/2-250Z9114315Z9.png" width="692" /></p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" height="103" src="https://zhuji.jb51.net/uploads/allimg/20250909/2-250Z9114321E5.png" width="557" /></p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" height="92" src="https://zhuji.jb51.net/uploads/allimg/20250909/2-250Z9114321145.png" width="692" /></p>
<p class="maodian"></p><h2>二、基于授权的访问控制</h2>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" height="187" src="https://zhuji.jb51.net/uploads/allimg/20250909/2-250Z9114321436.png" width="692" /></p>
<p>修改配置文件</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" height="311" src="https://zhuji.jb51.net/uploads/allimg/20250909/2-250Z9114321542.png" width="613" /></p>
<p>生成用户密码认证文件</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" height="98" src="https://zhuji.jb51.net/uploads/allimg/20250909/2-250Z9114321612.png" width="692" /></p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" height="70" src="https://zhuji.jb51.net/uploads/allimg/20250909/2-250Z9114321P8.png" width="659" /></p>
<p>重启服务,访问测试</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" height="295" src="https://zhuji.jb51.net/uploads/allimg/20250909/2-250Z9114322539.png" width="493" /></p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" height="173" src="https://zhuji.jb51.net/uploads/allimg/20250909/2-250Z9114322H8.png" width="518" /></p>
<p class="maodian"></p><h2> 三、基于客户端的访问控制 </h2>
<p>修改配置文件</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" height="133" src="https://zhuji.jb51.net/uploads/allimg/20250909/2-250Z9114322960.png" width="580" /></p>
<p>重启服务 </p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" height="48" src="https://zhuji.jb51.net/uploads/allimg/20250909/2-250Z91143225O.png" width="639" /></p>
<p>允许192.168.174.101访问</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" height="212" src="https://zhuji.jb51.net/uploads/allimg/20250909/2-250Z9114322152.png" width="594" /></p>
<p>不允许192.168.174.10访问</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" height="255" src="https://zhuji.jb51.net/uploads/allimg/20250909/2-250Z9114323O8.png" width="549" /></p>
<p class="maodian"></p><h2>四、基于域名的 Nginx 虚拟主机</h2>
<p>为虚拟主机提供域名解析</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" height="35" src="https://zhuji.jb51.net/uploads/allimg/20250909/2-250Z9114323555.png" width="692" /></p>
<p>修改Nginx的配置文件</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" height="358" src="https://zhuji.jb51.net/uploads/allimg/20250909/2-250Z9114323Q7.png" width="632" /></p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" height="340" src="https://zhuji.jb51.net/uploads/allimg/20250909/2-250Z9114323L6.png" width="534" /></p>
<p>为虚拟主机准备网页文档 </p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" height="60" src="https://zhuji.jb51.net/uploads/allimg/20250909/2-250Z9114323255.png" width="692" /></p>
<p>重启服务,访问测试</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" height="63" src="https://zhuji.jb51.net/uploads/allimg/20250909/2-250Z91143234X.png" width="693" /></p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" height="60" src="https://zhuji.jb51.net/uploads/allimg/20250909/2-250Z91143234c.png" width="692" /></p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" height="259" src="https://zhuji.jb51.net/uploads/allimg/20250909/2-250Z91143245a.png" width="692" /></p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" height="196" src="https://zhuji.jb51.net/uploads/allimg/20250909/2-250Z9114324N5.png" width="692" /></p>
<p class="maodian"></p><h2> 五、基于IP 的 Nginx 虚拟主机</h2>
<p>配置虚拟Ip地址</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" height="245" src="https://zhuji.jb51.net/uploads/allimg/20250909/2-250Z9114324339.png" width="692" /></p>
<p>修改配置文件的ip地址:</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" height="250" src="https://zhuji.jb51.net/uploads/allimg/20250909/2-250Z9114324540.png" width="528" /></p>
<p>重启服务,访问测试</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" height="16" src="https://zhuji.jb51.net/uploads/allimg/20250909/2-250Z9114324634.png" width="693" /></p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" height="231" src="https://zhuji.jb51.net/uploads/allimg/20250909/2-250Z91143254b.png" width="418" /></p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" height="224" src="https://zhuji.jb51.net/uploads/allimg/20250909/2-250Z9114325218.png" width="441" /></p>
<p class="maodian"></p><h2> 六、基于端口的 Nginx 虚拟主机</h2>
<p>修改配置文件内的端口号:</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" height="248" src="https://zhuji.jb51.net/uploads/allimg/20250909/2-250Z9114325B5.png" width="553" /></p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" height="260" src="https://zhuji.jb51.net/uploads/allimg/20250909/2-250Z91143254S.png" width="527" /></p>
<p>重启服务,访问测试</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" height="280" src="https://zhuji.jb51.net/uploads/allimg/20250909/2-250Z9114325405.png" width="451" /></p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" height="250" src="https://zhuji.jb51.net/uploads/allimg/20250909/2-250Z911432LW.png" width="438" /></p>
<p><strong>总结:虽然只有一台服务器,但是这台服务器上运行着多个网站,访问不同的域名 就可访问到不同的网站内容。</strong></p>
<p>以上就是Nginx虚拟主机的六种配置的详细内容,更多相关资料请阅读琼殿技术社区其它文章!</p>
頁:
[1]