120 lines
3.4 KiB
Nginx Configuration File
120 lines
3.4 KiB
Nginx Configuration File
# ============================================================
|
||
# AI Chat - Nginx 完整站点配置文件
|
||
# ============================================================
|
||
#
|
||
# 适用场景:直接部署到 Nginx + PHP-FPM(不使用宝塔面板)
|
||
#
|
||
# 使用方法:
|
||
# 1. 将本文件复制到 /etc/nginx/sites-available/ai-chat
|
||
# 2. 创建软链接:ln -s /etc/nginx/sites-available/ai-chat /etc/nginx/sites-enabled/
|
||
# 3. 修改下方 server_name 和 root 为实际值
|
||
# 4. 如 PHP 版本非 8.0,需将 php-cgi-80 改为对应版本号
|
||
# 5. nginx -t 测试配置,nginx -s reload 重载生效
|
||
#
|
||
# 注意:
|
||
# 如果你使用宝塔面板,请勿使用本文件,改用 baota-nginx-snippet.conf(伪静态规则)
|
||
#
|
||
# 前置条件:
|
||
# - PHP 8.0+ 已安装并启用 PHP-FPM
|
||
# - MySQL 5.7+ 已安装
|
||
# - Composer 已安装
|
||
# - 项目已部署到服务器目录
|
||
#
|
||
|
||
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;
|
||
}
|