<rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" version="2.0">
<channel>
<atom:link href="https://www.hulei.tech/feed" rel="self" type="application/rss+xml"/>
<title>我的个人日志 </title>
<link>https://www.hulei.tech</link>
<description>为所欲为，但勿伤害</description>
<language>zh-CN</language>
<copyright>© Figgo </copyright>
<pubDate>Thu, 16 Apr 2026 15:56:38 GMT</pubDate>
<generator>Mix Space CMS (https://github.com/mx-space)</generator>
<docs>https://mx-space.js.org</docs>
<image>
    <url>https://www.hulei.tech/api/v2/objects/avatar/oedqb9drgnu68s16ah.jpg</url>
    <title>我的个人日志 </title>
    <link>https://www.hulei.tech</link>
</image>
<item>
    <title>nginx 是什么</title>
    <link>https://www.hulei.tech/posts/default/nginx</link>
    <pubDate>Wed, 22 Oct 2025 07:23:04 GMT</pubDate>
    <description>nginx 是什么

Nginx 是一个高性能的 HTTP 和 反向代理 web 服务器，并发性能强</description>
    <content:encoded><![CDATA[
      <blockquote>该渲染由 marked 生成，可能存在排版问题，最佳体验请前往：<a href='https://www.hulei.tech/posts/default/nginx'>https://www.hulei.tech/posts/default/nginx</a></blockquote>
      <h2>nginx 是什么</h2>
<p>Nginx 是一个高性能的 <strong>HTTP</strong> 和 <strong>反向代理</strong> <strong>web 服务器</strong>，并发性能强</p>
<h3>nginx 中的几个重要概念</h3>
<p><strong>反向代理</strong></p>
<p>代表服务器接收客户端请求，隐藏服务器的真实 IP 和架构。</p>
<p>正向代理 vs 反向代理</p>
<ul>
<li><strong>正向代理</strong> 是“<strong>替客户端办事</strong>”（如翻墙），<strong>客户端知道代理的存在</strong>，<strong>服务器不知道真实的客户端是谁</strong>（只看到代理的 IP）</li>
<li><strong>反向代理</strong> 是“<strong>替服务器挡事</strong>”（如负载均衡），<strong>客户端不知道代理的存在</strong>，<strong>服务器知道代理的存在</strong></li>
</ul>
<blockquote>
<p>个人理解：首先了解正向代理，科学上网时，外网部署的 ss、trojan 服务器，我们电脑使用 clash 等客户端然后配置代理服务器，选择节点就可以访问外网，本质：使用客户端请求-&gt; 外网服务器-&gt; 真正的网址，然后响应内容发回给浏览器等客户端，在这种情况下，中间的代理服务器对于客户端是可感应的，已知的。而反向代理则不同，它只知道对应的服务器地址，但这个服务器地址大概率只能到达 nginx（但在客户端视角，它感知的是这个网址对应真正提供服务的 server），再由 nginx 转发请求到后端服务器。这个过程中 nginx 代理对于客户端来说，是无感知且透明的。</p>
</blockquote>
<p><strong>负载均衡</strong>：将客户端请求，根据轮询等负载均衡算法，分发到多个后端服务器上。一句话总结，<strong>请求分发到多个服务器节点</strong></p>
<p><strong>动静分离</strong>：将静态资源文件（html、css、js）部署到 nginx 上，动态资源文件（根据用户 id 不同，响应内容不同，需要后端服务 Controller 请求数据库）部署到真正的服务器。</p>
<h2>nginx 安装</h2>
<pre><code class="language-undefined">[object Object]</code></pre><h2>nginx 配置</h2>
<h3>nginx 配置文件位置</h3>
<p><code>/usr/local/nginx/conf/nginx.conf</code></p>
<h3>nginx 配置文件组成部分</h3>
<h4>全局块</h4>
<p>从配置文件开始到 events 块之间的内容，主要会设置一些影响 nginx 服务器整体运行的配置指令，比如</p>
<pre><code class="language-undefined">[object Object]</code></pre><h4>events 块</h4>
<p>events 块涉及的指令主要影响 Nginx 服务器与用户的网络连接</p>
<pre><code class="language-undefined">[object Object]</code></pre><h4>http 块</h4>
<p>这是 Nginx 服务器配置中最常用的部分，代理、缓存和日志定义等绝大多数功能和第三方模块的配置都在这里。</p>
<p><strong>需要注意的是：http 块包括 http 全局块、server 块。每个 http 块可以包括多个 server 块，而每个 server 块就相当于一个虚拟主机。而每个 server 块也分为全局 server 块，以及可以同时包含多个 locaton 块。</strong></p>
<p>http 块包含两部分内容：</p>
<ol>
<li>http 全局块：配置的指令包括文件引入、MIME-TYPE 定义、日志自定义、连接超时时间、单链接请求数上限等。</li>
<li>server 块：这块和虚拟主机有密切关系，虚拟主机从用户角度看，和一台独立的硬件主机是完全一样的，该技术的产生是为了
节省互联网服务器硬件成本。<ul>
<li>全局 server 块：最常见的配置是本虚拟机主机的监听配置和本虚拟主机的域名或 IP 配置。</li>
<li>location 块：一个 server 块可以配置多个 location 块，主要作用是基于 Nginx 服务器接收到的请求字符串（例如 server_name/uri-string），对虚拟主机名称（也可以是 IP 别名）之外的 <strong>字符串</strong>（例如前面的/uri-string）进行 <strong>匹配，对特定的请求进行处理。地址定向、数据缓存和应答控制等功能</strong>，还有许多第三方模块的配置也在这里进行。T</li>
</ul>
</li>
</ol>
<h3>Nginx 配置实例——反向代理</h3>
<p>（1）准备工作</p>
<ul>
<li>部署服务，使用 web 服务器部署一个后端服务，并启动</li>
<li>对外开放端口，服务器防火墙开放相应的端口，如果 vps 主机有安全组，也要开放相应端口</li>
<li>浏览器测试是否能正常访问</li>
</ul>
<p>（2）反向代理配置</p>
<p><strong>示例一</strong></p>
<p>实现效果：访问域名 <code>www.123.com</code>，会跳转到 <code>127.0.0.1:8080</code> 路径上，访问实际的后端服务</p>
<ol>
<li><p>域名解析配置，<a href="http://www.123.com">www.123.com</a> -&gt; 192.168.17.129</p>
</li>
<li><p>nginx 配置文件中，配置反向代理</p>
<pre><code class="language-undefined">[object Object]</code></pre><p><strong>示例二</strong></p>
<p>实现效果：使用 nginx 反向代理，根据访问的路径跳转到不同端口的服务中，nginx 监听端口为 9001，</p>
<ul>
<li>访问 <code>http://192.168.17.129:9001/edu/</code> 直接跳转到 <code>127.0.0.1:8080</code></li>
<li>访问 <code>http://192.168.17.129:9001/vod/</code> 直接跳转到 <code>127.0.0.1:8081</code></li>
</ul>
</li>
</ol>
<p>（1）准备工作</p>
<ul>
<li>准备两个 tomcat 服务器，一个 8080 端口，一个 8081 端口</li>
<li>创建文件夹和测试页面，使得访问 <code>http://192.168.17.129:8080/edu/a.html</code> 和 <code>http://192.168.17.129:8081/vod/a.html</code> 成功</li>
</ul>
<p>（2）nginx 配置</p>
<ul>
<li>http 块里再配置一个 server 块，具体配置如下：</li>
</ul>
<pre><code class="language-undefined">[object Object]</code></pre><ul>
<li>开放对外端口，9001 8080 8081</li>
</ul>
<h4>location 指令说明</h4>
<p>该指令用于匹配 URL。
语法如下：</p>
<pre><code class="language-undefined">[object Object]</code></pre><table>
<thead>
<tr>
<th>模式</th>
<th>含义</th>
</tr>
</thead>
<tbody><tr>
<td>location = /uri</td>
<td>= 表示精确匹配，只有完全匹配上才能生效</td>
</tr>
<tr>
<td>location ^~ /uri</td>
<td>^~ 开头对 URL 路径进行前缀匹配，并且在正则之前。</td>
</tr>
<tr>
<td>location ~ pattern</td>
<td>开头表示区分大小写的正则匹配</td>
</tr>
<tr>
<td>location ~* pattern</td>
<td>开头表示不区分大小写的正则匹配</td>
</tr>
<tr>
<td>location /uri</td>
<td>不带任何修饰符，也表示前缀匹配，但是在正则匹配之后</td>
</tr>
<tr>
<td>location /</td>
<td>通用匹配，任何未匹配到其它 location 的请求都会匹配到，相当于 switch 中的 default</td>
</tr>
</tbody></table>
<p>多个 location 配置的情况下匹配顺序为：</p>
<ul>
<li>首先精确匹配 <code>=</code></li>
<li>其次前缀匹配 <code>^~</code></li>
<li>其次是按文件中顺序的正则匹配</li>
<li>然后匹配不带任何修饰的前缀匹配。</li>
<li>最后是交给 <code>/</code> 通用匹配</li>
<li>当有匹配成功时候，停止匹配，按当前匹配规则处理请求</li>
</ul>
<p>所以实际使用中，至少有三个匹配规则定义，如下：</p>
<pre><code class="language-undefined">[object Object]</code></pre><h3>Nginx 配置实例——负载均衡</h3>
<p>1、实现效果
浏览器地址栏输入地址 <code>http://192.168.17.129/edu/a.html</code>，负载均衡，平均分配到 8080 和 8081 端口中</p>
<p>2、准备工作</p>
<ul>
<li>准备两台 tomcat 服务器，一台 8080，一台 8081</li>
<li>在两台 tomcat 里面 webapps 目录中，创建名称是 edu 文件夹，在 edu 文件夹中创建页面 <code>a.html</code>，用于测试</li>
</ul>
<p>3、负载均衡配置</p>
<ul>
<li>http 块中配置负载均衡服务列表 <code>upstream myserver {server ...; server ...;}</code></li>
<li>server块的<code>location</code>中配置<code>proxy_pass http://myserver;</code></li>
</ul>
<pre><code class="language-undefined">[object Object]</code></pre><p>可以配置不同的负载均衡策略：</p>
<ul>
<li><p>轮询</p>
<pre><code class="language-undefined">[object Object]</code></pre></li>
<li><p>指定权重<code>weight=10</code></p>
<pre><code class="language-undefined">[object Object]</code></pre></li>
<li><p>ip绑定<code>ip_hash; </code></p>
<pre><code class="language-undefined">[object Object]</code></pre></li>
<li><p>按后端服务器的响应时间来分配请求，响应时间短的优先分配。<code>fair; </code></p>
</li>
</ul>
<pre><code class="language-undefined">[object Object]</code></pre><ul>
<li>按访问url的hash结果来分配请求，使每个url定向到同一个后端服务器，后端服务器为缓存时比较有效。</li>
</ul>

      <p style='text-align: right'>
      <a href='https://www.hulei.tech/posts/default/nginx#comments'>看完了？说点什么呢</a>
      </p>
    ]]>
    </content:encoded>
  <guid isPermaLink="false">68f886584516307d8a16a786</guid>
  <category>posts</category>
<category>默认分类</category>
 </item>
  
</channel>
</rss>