成都威哥 發表於 2019-9-3 09:20:00

Nginx总结(四)基于域名的虚拟主机配置

<p>前面讲了如何安装配置Nginx,大家可以去这里看看nginx系列文章:https://www.cnblogs.com/zhangweizhong/category/1529997.html</p>
<p>今天要说的是Nginx如何基于域名配置虚拟主机。</p>
<p>需要说明的是:由于本文章是nginx系列文章中的一篇,文章里面很多其他的配置,可能前面的文章已经说讲过,然后后续就没有在介绍,如果出现有些配置没有讲,大家可能需要去看看前面的文章。</p>
<p>&nbsp;</p>
<h3>应用场景</h3>
<p>两个域名指向同一台nginx服务器,用户访问不同的域名显示不同的网页内容。</p>
<p>两个域名是aaa.test.com和bbb.test.com</p>
<p>nginx服务器使用虚拟机192.168.78.132</p>
<p>&nbsp;</p>
<h3>准备环境</h3>
<p>1. 创建192.168.78.132虚拟机,保证本地电脑和虚拟网络通畅。</p>
<p>2. 在192.168.78.132上安装nginx。</p>
<p>3. 通过host文件指定aaa.test.com和bbb.test.com对应192.168.78.132虚拟机:</p>
<p>修改window的hosts文件:(C:\Windows\System32\drivers\etc)</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(128, 0, 128, 1)">192.168</span>.<span style="color: rgba(128, 0, 128, 1)">78.132</span><span style="color: rgba(0, 0, 0, 1)"> aaa.test.com
</span><span style="color: rgba(128, 0, 128, 1)">192.168</span>.<span style="color: rgba(128, 0, 128, 1)">78.132</span> bbb.test.com</pre>
</div>
<p>&nbsp;</p>
<h3>html目录创建</h3>
<p>1. 在192.168.78.132上创建/usr/local/aaa_html,此目录为aaa.test.com域名访问的目录</p>
<p>2. 在192.168.78.132上创建/usr/local/bbb_html,此目录为bbb.test.com域名访问的目录</p>
<p>3. 将/usr/local/nginx/html中的内容拷贝分别拷贝到上边两个目录中,为了方便测试需要修改每个目录下的index.html内容使之个性化。</p>
<p>&nbsp;</p>
<h3>配置虚拟主机</h3>
<p>修改/usr/local/nginx/conf/nginx.conf文件,添加两个虚拟主机,如下:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">#配置虚拟主机aaa.test.com
server {

    #监听的ip和端口,配置本机ip和端口
    listen 192.168.78.132</span>:<span style="color: rgba(128, 0, 128, 1)">80</span><span style="color: rgba(0, 0, 0, 1)">;         

    #虚拟主机名称是aaa.test.com,请求域名aaa.test.com的url将由此server配置解析
    server_name aaa.test.com;

    #所有的请求都以</span>/<span style="color: rgba(0, 0, 0, 1)">开始,所有的请求都可以匹配此location
    location </span>/<span style="color: rgba(0, 0, 0, 1)"> {
      #使用root指令指定虚拟主机目录即网页存放目录
      #比如访问http:</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">ip/test.html将找到/usr/local/aaa_html/test.html</span>
<span style="color: rgba(0, 0, 0, 1)">
      #比如访问http:</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">ip/item/test.html将找到/usr/local/aaa_html/item/test.html</span>
      root /usr/local/<span style="color: rgba(0, 0, 0, 1)">aaa_html;      

      #指定欢迎页面,按从左到右顺序查找
      index index.html index.htm;   
    }
}

#配置虚拟主机bbb.test.com
server {
    listen 192.168.78.132</span>:<span style="color: rgba(128, 0, 128, 1)">80</span><span style="color: rgba(0, 0, 0, 1)">;
    server_name bbb.test.com;

    location </span>/<span style="color: rgba(0, 0, 0, 1)"> {
      root </span>/usr/local/<span style="color: rgba(0, 0, 0, 1)">bbb_html;
      index index.html index.htm;
    }
}</span></pre>
</div>
<p>&nbsp;</p>
<h3>测试</h3>
<p>分别访问aaa.test.com、bbb.test.com,看效果如何:&nbsp;</p>
<p><img src="https://img2018.cnblogs.com/blog/306976/201908/306976-20190822203003076-2036878257.png">&nbsp;</p>
<p><img src="https://img2018.cnblogs.com/blog/306976/201908/306976-20190822203130087-421060758.png"></p>
<h3>&nbsp;</h3>
<h3>最后</h3>
<p>以上, 就把nginx基于域名的虚拟主机配置说完了。通过这三篇文章,介绍了nginx配置虚拟主机的三种方式,分别是基于ip 的虚拟主机配置,基于端口的虚拟主机配置和基于域名的虚拟主机配置。</p>
<p>其中最常用的还是基于域名的虚拟主机配置。</p>
<p>&nbsp;</p>

</div>
<div id="MySignature" role="contentinfo">
    <p style="border-top: #e0e0e0 1px dashed; border-right: #e0e0e0 1px dashed; border-bottom: #e0e0e0 1px dashed; border-left: #e0e0e0 1px dashed; padding-top: 10px; padding-right: 10px; padding-bottom: 20px; padding-left: 25px; background: #e5f1f4 no-repeat 1% 50%; font-family: 微软雅黑; font-size: 14px" id="PSignature">
                <br>
                作者:<b><span style="font-size: 12px; color: red">章为忠</span></b>
                <br>
                如有问题,可以微信:<strong>18618243664</strong>&nbsp;联系我,非常感谢。
                <br>
<br>
          <strong>关注我的微信公众号,获取相关的<span style="font-size: 12px; color: red"><strong> 源代码及视频资料</strong></span>。</strong>
<br>

<img src="https://images2015.cnblogs.com/blog/306976/201509/306976-20150905110907420-2085368719.jpg">
            </p><br><br>
来源:https://www.cnblogs.com/zhangweizhong/p/11378554.html
頁: [1]
查看完整版本: Nginx总结(四)基于域名的虚拟主机配置