0
点赞
收藏
分享

微信扫一扫

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

诗尚凝寒 2022-03-22 阅读 87


新项目采用前后端分离的方式开发,前后端代码打算分开部署(同机器且同域名),但打算支持后端依然可访问静态资源(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;
}
}



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



举报

相关推荐

0 条评论