# AI Chat 应用 Nginx 配置(PHP-FPM 版本) # 将此文件复制到 /etc/nginx/sites-available/ 并创建软链接到 sites-enabled/ # # 注意:本文件为完整的 Nginx 站点配置,适用于直接部署到 Nginx + PHP-FPM 的场景。 # 如果你使用宝塔面板管理服务器,请参考 baota-nginx-snippet.conf 文件中的配置片段。 # # 前置条件: # - PHP 8.0+ 已安装并启用 PHP-FPM # - MySQL 5.7+ 已安装 # - Composer 已安装 # - 项目已部署到 /path/to/ai-chat/ 目录 server { listen 80; server_name your-domain.com; # 网站根目录指向 public/ root /path/to/ai-chat/public; index index.php login.php install.php; # 安全 Headers add_header X-Frame-Options "SAMEORIGIN" always; add_header X-Content-Type-Options "nosniff" always; add_header X-XSS-Protection "1; mode=block" always; add_header Referrer-Policy "strict-origin-when-cross-origin" always; # Gzip 压缩 gzip on; gzip_vary on; gzip_proxied any; gzip_comp_level 6; gzip_min_length 256; gzip_types text/plain text/css text/javascript application/javascript application/json application/xml image/svg+xml; # 默认路由 - PHP 文件 location / { try_files $uri $uri/ /login.php; } # API 路由 - 所有 /api/ 请求转发到 api.php location /api/ { try_files $uri /api.php$is_args$args; fastcgi_pass unix:/tmp/php-cgi-80.sock; fastcgi_index index.php; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root/api.php; fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_param REQUEST_URI $request_uri; } # SSE 流式响应专用配置(AI对话 API) location /api/chat/completions { fastcgi_pass unix:/tmp/php-cgi-80.sock; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root/api.php; fastcgi_param REQUEST_URI $request_uri; # SSE 关键配置:禁用所有缓冲 fastcgi_buffering off; fastcgi_cache off; proxy_buffering off; # 禁用 Nginx 缓冲 add_header X-Accel-Buffering no; # SSE 超时设置(5分钟) fastcgi_read_timeout 300s; } # 静态资源缓存 location /assets/ { expires 30d; add_header Cache-Control "public, immutable"; } # 上传文件缓存 location /uploads/ { expires 7d; add_header Cache-Control "public"; } # PHP 文件处理 location ~ \.php$ { fastcgi_pass unix:/tmp/php-cgi-80.sock; fastcgi_index index.php; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; } # 禁止访问非公开目录 location ~ /(app|config|vendor|tests|scripts|\.cospec)/ { deny all; return 404; } # 禁止访问隐藏文件 location ~ /\. { deny all; return 404; } # 上传文件大小限制 client_max_body_size 10m; }