查看: 47|回覆: 0

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

[複製鏈接]

3

主題

0

回帖

0

積分

热心网友

金币
0
閲讀權限
220
精華
0
威望
0
贡献
0
在線時間
0 小時
註冊時間
2010-6-19
發表於 2020-6-9 14:29:00 | 顯示全部樓層 |閲讀模式

新项目采用前后端分离的方式开发,前后端代码打算分开部署(同机器且同域名),但打算支持后端依然可访问静态资源(nginx配置仅一份)。

搜索nginx配置大部分都通过url前缀进行转发来做前后端分离,不适用目前项目。

 

说明

前端框架:vue

后端框架:thinkphp6

前端部署目录:/www/project_static

后端部署目录:/www/project

 

nginx配置方式 

`api`及`static`转发到php

server {
    listen 80;
    server_name test.aichenk.com;
    index index.html index.htm index.php;

    set $static_root '/www/project_static';
    set $php_root '/www/project/public';
    root $static_root;

    location ~ \.php$ {
        root $php_root;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
        fastcgi_buffer_size 128k;
        fastcgi_buffers 32 32k;
    }

  location / {
        try_files $uri $uri/ /index.html;
    }

    location ^~ /api/ {
        root $php_root;
        if (!-e $request_filename) {
            rewrite  ^(.*)$  /index.php?s=/$1  last;
            break;
        }
    }

    location ^~ /static/ {
        root $php_root;
        access_log off;
    }

    # 禁用缓存
    location = /index.html {
        add_header Cache-Control no-cache;
        add_header Pragma no-cache;
        add_header Expires 0;
    }

    location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
        expires       max;
        log_not_found off;
        access_log    off;
    }
}

 

另外可通过反向代理方式,若第一次判断文件不存在,则发送到另一个服务中,服务中仅关注后端配置。



来源:https://www.cnblogs.com/aichenk/p/13072454.html
回覆

使用道具 舉報

您需要登錄後才可以回帖 登錄 | 立即注册

本版積分規則

相关侵权、举报、投诉及建议等,请发 E-mail:qiongdian@foxmail.com

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.

在本版发帖返回顶部