fix: 添加生产环境 Nginx 配置,修复 Slim base path 导致 API 404

- 显式设置 SCRIPT_NAME 为 /index.php,避免 PHP-FPM 推断为 /api/index.php
- 否则 Slim 4 认为 base path 是 /api,路由 /api/conversations 匹配失败

2026-05-15
This commit is contained in:
2026-05-15 18:26:08 +08:00
parent 6b26ccb1e4
commit 552bfe7e7a
+43
View File
@@ -0,0 +1,43 @@
server {
listen 80;
server_name localhost;
# 前端静态文件
root /var/www/dist;
index index.html;
# 文件上传限制
client_max_body_size 50M;
# API 路由 → PHP-FPM
location /api/ {
fastcgi_pass chat-php:9000;
fastcgi_index index.php;
include fastcgi_params;
# 关键:硬编码 PHP 入口,让 Slim 正确解析路由
fastcgi_param SCRIPT_FILENAME /var/www/html/public/index.php;
# 关键:显式设置 SCRIPT_NAME 为空 base path
# 否则 PHP-FPM 会推断为 /api/index.php,导致 Slim 路由匹配失败
fastcgi_param SCRIPT_NAME /index.php;
fastcgi_read_timeout 300;
# HTTPS 反向代理头
fastcgi_param HTTPS on;
fastcgi_param HTTP_X_FORWARDED_PROTO https;
fastcgi_param HTTP_X_FORWARDED_HOST www.eryang.wang;
}
# SPA 路由回退:所有非 API 请求返回 index.html
location / {
try_files $uri $uri/ /index.html;
}
# 静态资源缓存
location ~* \.(css|js|png|jpg|jpeg|gif|ico|svg|woff|woff2)$ {
expires 1M;
add_header Cache-Control "public, immutable";
}
}