基于端口
<h1>虚拟主机配置-基于端口</h1>
<ul>
<li>Nginx总结(三)基于端口的虚拟主机配置</li>
<li><a href="https://www.cnblogs.com/zhangweizhong/p/11378548.html">https://www.cnblogs.com/zhangweizhong/p/11378548.html</a></li>
</ul>
<h2>应用场景</h2>
<ul>
<li>nginx对外提供 801 和 802 两个端口监听服务。</li>
<li>请求 801 端口,则请求 192.168.32.128_801 目录下的 html</li>
<li>请求 802 端口,则请求 192.168.32.128_802 目录下的 html</li>
</ul>
<h2>准备环境</h2>
<ul>
<li>创建 192.168.32.128 的虚拟机,保证本地电脑和虚拟网络通畅</li>
<li>在虚拟主机配置-基于ip配置去修改</li>
</ul>
<h2>html目录创建</h2>
<ul>
<li>
<p>目录创建为 192.168.32.128_801 和 192.168.32.128_802,</p>
</li>
<li>
<p>为了方便测试,在每个目录下增加 index.html 内容</p>
<pre><code class="language-shell">cd /usr/local/nginx/html
mkdir 192.168.32.128_801
mkdir 192.168.32.128_802
ls</code></pre>
</li>
</ul>
<h2>进行配置</h2>
<ul>
<li>
<p>增加配置文件</p>
<pre><code class="language-shell">cd /usr/local/nginx/conf/vhosts
192.168.32.128_801.conf
192.168.32.128_802.conf</code></pre>
</li>
<li>
<p>文件 192.168.32.128_801.conf 增加内容</p>
<pre><code class="language-nginx">server {
listen 801;
server_name 192.168.32.128;
location / {
root /usr/local/nginx/html/192.168.32.128_801;
index index.html index.htm;
}
}</code></pre>
</li>
<li>
<p>文件 192.168.32.128_802.conf 增加内容</p>
<pre><code class="language-nginx">server {
listen 802;
server_name 192.168.32.128;
location / {
root /usr/local/nginx/html/192.168.32.128_802;
index index.html index.htm;
}
}</code></pre>
</li>
</ul>
<h2>测试</h2>
<ul>
<li>
<p>重新启动nginx,观察端口监听状态</p>
<pre><code class="language-shell">systemctl restart nginx
ps aux | grep nginx</code></pre>
</li>
<li>
<p>访问虚拟主机 192.168.32.128:801</p>
<ul>
<li><a href="http://192.168.32.128:801">http://192.168.32.128:801</a></li>
</ul>
</li>
<li>
<p>访问虚拟主机 192.168.32.128:802</p>
<ul>
<li><a href="http://192.168.32.128:802">http://192.168.32.128:802</a></li>
</ul>
</li>
</ul>