65 lines
1.9 KiB
Plaintext
65 lines
1.9 KiB
Plaintext
# Nginx WebSocket 代理配置示例
|
|
# 用于将 /api/socket.io 请求代理到后端的 Socket.IO 服务器
|
|
|
|
server {
|
|
listen 80;
|
|
server_name d1kt.cn;
|
|
|
|
# 重定向 HTTP 到 HTTPS
|
|
return 301 https://$server_name$request_uri;
|
|
}
|
|
|
|
server {
|
|
listen 443 ssl http2;
|
|
server_name d1kt.cn;
|
|
|
|
# SSL 证书配置
|
|
ssl_certificate /etc/letsencrypt/live/d1kt.cn/fullchain.pem;
|
|
ssl_certificate_key /etc/letsencrypt/live/d1kt.cn/privkey.pem;
|
|
|
|
# SSL 优化配置
|
|
ssl_protocols TLSv1.2 TLSv1.3;
|
|
ssl_ciphers ECDHE-RSA-AES256-GCM-SHA512:DHE-RSA-AES256-GCM-SHA512:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES256-GCM-SHA384;
|
|
ssl_prefer_server_ciphers off;
|
|
ssl_session_cache shared:SSL:10m;
|
|
ssl_session_timeout 10m;
|
|
|
|
# 根目录静态文件服务
|
|
root /var/www/typing_practiceweb/build;
|
|
index index.html index.htm;
|
|
|
|
# 静态资源缓存
|
|
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg)$ {
|
|
expires 1y;
|
|
add_header Cache-Control "public, immutable";
|
|
}
|
|
|
|
# API 请求代理配置
|
|
location /api/ {
|
|
proxy_pass http://localhost:5001/; # 注意:末尾的斜杠很重要!
|
|
proxy_http_version 1.1;
|
|
|
|
# WebSocket 代理头
|
|
proxy_set_header Upgrade $http_upgrade;
|
|
proxy_set_header 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 86400; # WebSocket 长连接需要较长的超时时间
|
|
proxy_send_timeout 86400;
|
|
proxy_connect_timeout 30;
|
|
|
|
# 缓冲区设置
|
|
proxy_buffering off;
|
|
proxy_buffer_size 16k;
|
|
proxy_buffers 4 32k;
|
|
}
|
|
|
|
# SPA 回退路由
|
|
location / {
|
|
try_files $uri $uri/ /index.html;
|
|
}
|
|
} |