这个世界太浮躁 發表於 2020-6-9 14:29:00

同域名前后端分离项目 nginx配置实践

<p>新项目采用前后端分离的方式开发,前后端代码打算分开部署(同机器且同域名),但打算支持后端依然可访问静态资源(nginx配置仅一份)。</p>
<p>搜索nginx配置大部分都通过url前缀进行转发来做前后端分离,不适用目前项目。</p>
<p>&nbsp;</p>
<h3>说明</h3>
<p>前端框架:vue</p>
<p>后端框架:thinkphp6</p>
<p>前端部署目录:/www/project_static</p>
<p>后端部署目录:/www/project</p>
<p>&nbsp;</p>
<h3>nginx配置方式&nbsp;</h3>
<h4>`api`及`static`转发到php</h4>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">server {
    listen </span><span style="color: rgba(128, 0, 128, 1)">80</span><span style="color: rgba(0, 0, 0, 1)">;
    server_name test.aichenk.com;
    index index.html index.htm index.php;

    set $static_root </span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">/www/project_static</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">;
    set $php_root </span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">/www/project/public</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">;
    root $static_root;

    location </span>~<span style="color: rgba(0, 0, 0, 1)"> \.php$ {
      root $php_root;
      fastcgi_pass   </span><span style="color: rgba(128, 0, 128, 1)">127.0</span>.<span style="color: rgba(128, 0, 128, 1)">0.1</span>:<span style="color: rgba(128, 0, 128, 1)">9000</span><span style="color: rgba(0, 0, 0, 1)">;
      fastcgi_indexindex.php;
      fastcgi_paramSCRIPT_FILENAME$document_root$fastcgi_script_name;
      include      fastcgi_params;
      fastcgi_buffer_size 128k;
      fastcgi_buffers </span><span style="color: rgba(128, 0, 128, 1)">32</span><span style="color: rgba(0, 0, 0, 1)"> 32k;
    }

  location </span>/<span style="color: rgba(0, 0, 0, 1)"> {
      try_files $uri $uri</span>/ /<span style="color: rgba(0, 0, 0, 1)">index.html;
    }

    location </span>^~ /api/<span style="color: rgba(0, 0, 0, 1)"> {
      root $php_root;
      </span><span style="color: rgba(0, 0, 255, 1)">if</span> (!-<span style="color: rgba(0, 0, 0, 1)">e $request_filename) {
            rewrite</span>^(.*)$/index.php?s=/$<span style="color: rgba(128, 0, 128, 1)">1</span><span style="color: rgba(0, 0, 255, 1)">last</span><span style="color: rgba(0, 0, 0, 1)">;
            break;
      }
    }

    location </span>^~ /static/<span style="color: rgba(0, 0, 0, 1)"> {
      root $php_root;
      access_log off;
    }

    # 禁用缓存
    location </span>= /<span style="color: rgba(0, 0, 0, 1)">index.html {
      add_header Cache</span>-Control no-<span style="color: rgba(0, 0, 0, 1)">cache;
      add_header Pragma no</span>-<span style="color: rgba(0, 0, 0, 1)">cache;
      add_header Expires </span><span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">;
    }

    location </span>~* \.(js|css|png|jpg|jpeg|gif|<span style="color: rgba(0, 0, 0, 1)">ico)$ {
      expires       max;
      log_not_found off;
      access_log    off;
    }
}</span></pre>
</div>
<p>&nbsp;</p>
<p>另外可通过反向代理方式,若第一次判断文件不存在,则发送到另一个服务中,服务中仅关注后端配置。</p><br><br>
来源:https://www.cnblogs.com/aichenk/p/13072454.html
頁: [1]
查看完整版本: 同域名前后端分离项目 nginx配置实践