# ============================================================
# 聊天室 Nginx 生产环境配置（基于宝塔面板）
# 域名：chat.ay.lc
# 支持：Laravel 12 + Laravel Reverb WebSocket 反向代理
# ============================================================
#
# 部署方式：
#   在宝塔面板 → 网站 → chat.ay.lc → 设置 → 配置文件 中，
#   将下方 WebSocket 反向代理部分和 Laravel 伪静态部分
#   粘贴到您现有配置的对应位置即可。
#
# ============================================================

# ═══════════════════════════════════════════════════════════
# 请将下面这段放在 server { } 块的【最外层上方】
# （与 server 同级，不要放在 server 内部）
# ═══════════════════════════════════════════════════════════

map $http_upgrade $connection_upgrade {
    default upgrade;
    ''      close;
}

# ═══════════════════════════════════════════════════════════
# 请将下面这段放在 server { } 块内部，
# 建议放在 #REWRITE-END 之后，禁止访问敏感文件之前
# ═══════════════════════════════════════════════════════════

    # ── Laravel 伪静态规则 ──────────────────────────────────
    # 如果宝塔的伪静态配置文件 rewrite/chat.ay.lc.conf 为空，
    # 请在宝塔面板 → 网站 → 伪静态 中选择 "laravel5"，
    # 或者直接在 rewrite/chat.ay.lc.conf 中写入以下内容：
    #
    # location / {
    #     try_files $uri $uri/ /index.php?$query_string;
    # }

    # ── Vite 静态资源缓存（文件名带 hash，可安全长期缓存）────────────
    location ^~ /build/assets/ {
        access_log off;
        expires 1y;
        add_header Cache-Control "public, max-age=31536000, immutable" always;
        try_files $uri =404;
    }

    # ── 常规静态资源缓存（不带 hash，保守缓存 7 天）───────────────
    location ~* \.(?:css|js|mjs|png|jpg|jpeg|gif|webp|svg|ico|woff2?|ttf|eot)$ {
        access_log off;
        expires 7d;
        add_header Cache-Control "public, max-age=604800" always;
        try_files $uri =404;
    }

    # ── 传输压缩：宝塔若已全局开启，可保持全局配置，不必重复添加 ──────
    gzip on;
    gzip_comp_level 5;
    gzip_min_length 1024;
    gzip_vary on;
    gzip_types
        text/plain
        text/css
        text/javascript
        application/javascript
        application/json
        application/xml
        image/svg+xml;

    # 如果服务器已安装 ngx_brotli，可启用以下配置获得更高压缩率：
    # brotli on;
    # brotli_comp_level 5;
    # brotli_types text/plain text/css text/javascript application/javascript application/json application/xml image/svg+xml;

    # ⚡ WebSocket 反向代理（核心配置 - 必须添加！）
    # Laravel Reverb 监听在 127.0.0.1:8080
    # 浏览器通过 /app 和 /apps 路径发起 WebSocket 连接
    location /app {
        proxy_pass             http://127.0.0.1:8080;
        proxy_http_version     1.1;
        proxy_set_header       Upgrade $http_upgrade;
        proxy_set_header       Connection $connection_upgrade;
        proxy_set_header       Host $host;
        proxy_set_header       X-Real-IP $remote_addr;
        proxy_set_header       X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header       X-Forwarded-Proto $scheme;

        # WebSocket 长连接保活：120秒无数据才断开
        proxy_read_timeout     120s;
        proxy_send_timeout     120s;

        # 关闭缓冲，保证实时性
        proxy_buffering        off;
    }

    location /apps {
        proxy_pass             http://127.0.0.1:8080;
        proxy_http_version     1.1;
        proxy_set_header       Upgrade $http_upgrade;
        proxy_set_header       Connection $connection_upgrade;
        proxy_set_header       Host $host;
        proxy_set_header       X-Real-IP $remote_addr;
        proxy_set_header       X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header       X-Forwarded-Proto $scheme;

        proxy_read_timeout     120s;
        proxy_send_timeout     120s;
        proxy_buffering        off;
    }


# ═══════════════════════════════════════════════════════════
# 完整配置合并后的参考样本（方便您核对）
# ═══════════════════════════════════════════════════════════

# map $http_upgrade $connection_upgrade {
#     default upgrade;
#     ''      close;
# }
#
# server
# {
#     listen 80;
#     listen 443 ssl;
#     listen 443 quic;
#     http2 on;
#     server_name chat.ay.lc;
#     index index.php index.html index.htm default.php default.htm default.html;
#     root /www/wwwroot/chat.ay.lc/public;
#
#     #CERT-APPLY-CHECK--START
#     include /www/server/panel/vhost/nginx/well-known/chat.ay.lc.conf;
#     #CERT-APPLY-CHECK--END
#     include /www/server/panel/vhost/nginx/extension/chat.ay.lc/*.conf;
#
#     #SSL-START
#     ... （您现有的 SSL 配置保持不变）
#     #SSL-END
#
#     #ERROR-PAGE-START
#     error_page 404 /404.html;
#     #ERROR-PAGE-END
#
#     #PHP-INFO-START
#     include enable-php-84.conf;
#     #PHP-INFO-END
#
#     #REWRITE-START
#     include /www/server/panel/vhost/rewrite/chat.ay.lc.conf;
#     #REWRITE-END
#
#     location ^~ /build/assets/ {
#         access_log off;
#         expires 1y;
#         add_header Cache-Control "public, max-age=31536000, immutable" always;
#         try_files $uri =404;
#     }
#
#     location ~* \.(?:css|js|mjs|png|jpg|jpeg|gif|webp|svg|ico|woff2?|ttf|eot)$ {
#         access_log off;
#         expires 7d;
#         add_header Cache-Control "public, max-age=604800" always;
#         try_files $uri =404;
#     }
#
#     gzip on;
#     gzip_comp_level 5;
#     gzip_min_length 1024;
#     gzip_vary on;
#     gzip_types text/plain text/css text/javascript application/javascript application/json application/xml image/svg+xml;
#
#     # ⚡⚡⚡ 在这里插入 WebSocket 反向代理 ⚡⚡⚡
#     location /app {
#         proxy_pass             http://127.0.0.1:8080;
#         proxy_http_version     1.1;
#         proxy_set_header       Upgrade $http_upgrade;
#         proxy_set_header       Connection $connection_upgrade;
#         proxy_set_header       Host $host;
#         proxy_set_header       X-Real-IP $remote_addr;
#         proxy_set_header       X-Forwarded-For $proxy_add_x_forwarded_for;
#         proxy_set_header       X-Forwarded-Proto $scheme;
#         proxy_read_timeout     120s;
#         proxy_send_timeout     120s;
#         proxy_buffering        off;
#     }
#
#     location /apps {
#         proxy_pass             http://127.0.0.1:8080;
#         proxy_http_version     1.1;
#         proxy_set_header       Upgrade $http_upgrade;
#         proxy_set_header       Connection $connection_upgrade;
#         proxy_set_header       Host $host;
#         proxy_set_header       X-Real-IP $remote_addr;
#         proxy_set_header       X-Forwarded-For $proxy_add_x_forwarded_for;
#         proxy_set_header       X-Forwarded-Proto $scheme;
#         proxy_read_timeout     120s;
#         proxy_send_timeout     120s;
#         proxy_buffering        off;
#     }
#
#     # 禁止访问的敏感文件
#     ... （您现有的安全配置保持不变）
#
#     access_log  /www/wwwlogs/chat.ay.lc.log;
#     error_log  /www/wwwlogs/chat.ay.lc.error.log;
# }
