Files
server/nginx.conf
2026-01-16 15:49:34 +08:00

253 lines
7.8 KiB
Nginx Configuration File
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# You can choice one setting
## Only HTTP
server {
listen 80;
server_name web.goravel.dev;
access_log off;
location /storage/ {
alias /path/to/storage/app/public/;
expires 1h;
add_header Cache-Control "public, immutable";
access_log off;
}
# WebSocket 代理配置
location /ws {
proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1;
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_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Port $server_port;
proxy_read_timeout 86400; # WebSocket 需要较长的超时时间
proxy_send_timeout 86400;
proxy_buffering off; # 禁用缓冲,确保 WebSocket 实时通信
}
# 静态资源文件(JS, CSS, 图片等)- 必须放在 API 代理之前
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot|map)$ {
root /path/to/html/dist;
expires 1y;
add_header Cache-Control "public, immutable";
access_log off;
try_files $uri =404;
}
# 静态资源目录(assets
location /assets/ {
root /path/to/html/dist;
expires 1y;
add_header Cache-Control "public, immutable";
access_log off;
try_files $uri =404;
}
# SSE (Server-Sent Events) 路由 - 必须放在 /api/ 之前,按最长匹配原则
# 匹配所有 SSE 流式接口:monitor/system-info/stream
location ~ ^/api/admin/monitor/system-info/stream {
proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1;
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_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Port $server_port;
# SSE 关键配置:禁用缓冲,确保实时推送
proxy_buffering off;
proxy_cache off;
proxy_read_timeout 86400; # 24小时超时,SSE连接需要保持长时间
proxy_send_timeout 86400;
# 禁用gzip压缩,SSE需要实时传输
gzip off;
}
# API 路由代理到 Go 服务器
location /api/ {
proxy_pass http://127.0.0.1:3000;
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;
# 大文件上传支持
client_max_body_size 10G; # 允许上传最大10GB文件
client_body_timeout 300s; # 客户端请求体超时时间
proxy_read_timeout 300s; # 读取后端响应超时时间
proxy_send_timeout 300s; # 发送请求到后端超时时间
proxy_connect_timeout 60s; # 连接后端超时时间
}
# Swagger 文档
location /swagger/ {
proxy_pass http://127.0.0.1:3000;
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;
}
location / {
proxy_pass http://127.0.0.1:3000;
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;
}
# SPA 路由 - 所有其他请求返回 index.html
# location / {
# root /path/to/html/dist;
# try_files $uri $uri/ /index.html;
# index index.html;
# }
gzip on;
gzip_min_length 1k;
gzip_comp_level 9;
gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
gzip_vary on;
gzip_disable "MSIE [1-6]\.";
}
## Only HTTPS
server {
if ($host = web.goravel.dev) {
return 301 https://$host$request_uri;
}
listen 80;
server_name web.goravel.dev;
return 404;
}
server {
listen 443 ssl;
server_name web.goravel.dev;
access_log off;
location /storage/ {
alias /path/to/storage/app/public/;
expires 1h;
add_header Cache-Control "public, immutable";
access_log off;
}
# WebSocket 代理配置(WSS- 通用 WebSocket 路径
location /ws {
proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1;
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_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Port $server_port;
proxy_read_timeout 86400; # WebSocket 需要较长的超时时间
proxy_send_timeout 86400;
proxy_buffering off; # 禁用缓冲,确保 WebSocket 实时通信
}
# SSE (Server-Sent Events) 路由 - 必须放在 /api/ 之前,按最长匹配原则
# 匹配所有 SSE 流式接口:monitor/system-info/stream
location ~ ^/api/admin/monitor/system-info/stream {
proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1;
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_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Port $server_port;
# SSE 关键配置:禁用缓冲,确保实时推送
proxy_buffering off;
proxy_cache off;
proxy_read_timeout 86400; # 24小时超时,SSE连接需要保持长时间
proxy_send_timeout 86400;
# 禁用gzip压缩,SSE需要实时传输
gzip off;
}
# API 路由代理到 Go 服务器
location /api/ {
proxy_pass http://127.0.0.1:3000;
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;
# 大文件上传支持
client_max_body_size 10G; # 允许上传最大10GB文件
client_body_timeout 300s; # 客户端请求体超时时间
proxy_read_timeout 300s; # 读取后端响应超时时间
proxy_send_timeout 300s; # 发送请求到后端超时时间
proxy_connect_timeout 60s; # 连接后端超时时间
}
# Swagger 文档
location /swagger/ {
proxy_pass http://127.0.0.1:3000;
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;
}
# 静态资源文件(JS, CSS, 图片等)- 必须放在 API 代理之前
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot|map)$ {
root /path/to/html/dist;
expires 1y;
add_header Cache-Control "public, immutable";
access_log off;
try_files $uri =404;
}
# 静态资源目录(assets
location /assets/ {
root /path/to/html/dist;
expires 1y;
add_header Cache-Control "public, immutable";
access_log off;
try_files $uri =404;
}
# SPA 路由 - 所有其他请求返回 index.html
location / {
root /path/to/html/dist;
try_files $uri $uri/ /index.html;
index index.html;
}
ssl_certificate ./ssl/web.goravel.dev.pem;
ssl_certificate_key ./ssl/web.goravel.dev.key;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
gzip on;
gzip_min_length 1k;
gzip_comp_level 9;
gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
gzip_vary on;
gzip_disable "MSIE [1-6]\.";
}