-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnginx.conf
More file actions
95 lines (79 loc) · 3.81 KB
/
Copy pathnginx.conf
File metadata and controls
95 lines (79 loc) · 3.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# 指定 Nginx 运行的用户(通常为 nginx 或 www-data)
user nginx; # 或者 www-data
# 根据 CPU 核心数自动调整 worker 进程数
worker_processes auto;
# 错误日志路径和级别
error_log /var/log/nginx/error.log warn;
# PID 文件路径
pid /var/run/nginx.pid;
# 事件相关配置
events {
worker_connections 1024; # 每个 worker 进程的最大连接数
}
# HTTP 配置块
http {
# 文件类型映射
include /etc/nginx/mime.types;
default_type application/octet-stream;
# 日志格式定义
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
# 访问日志路径
access_log /var/log/nginx/access.log main;
# 性能优化相关
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
# Gzip 压缩(建议开启)
gzip on;
gzip_comp_level 5;
gzip_min_length 1k;
gzip_types text/plain application/javascript application/json text/css text/xml application/xml application/xml+rss text/javascript image/svg+xml;
# 站点配置
server {
# 监听 80 端口,用于处理 HTTP 请求
listen 80;
# 请将 yourdomain.com 替换为你的实际域名
server_name your_domain_name.com www.your_domain_name.com;
# 开启 gzip 压缩以提升网站加载速度
# gzip on; # 已在 http 块全局开启
# ==================================
# 前端 Next.js 应用请求转发
# ==================================
location / {
# 将所有匹配根路径 / 的请求转发到本地运行的前端服务
proxy_pass http://127.0.0.1:3666;
# --- 重要的代理头设置 ---
# 将原始请求的 Host 头部传递给后端,这对于 Next.js 正确路由很重要
proxy_set_header Host $host;
# 传递客户端的真实 IP 地址
proxy_set_header X-Real-IP $remote_addr;
# 传递完整的代理链 IP,用于多层代理环境
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# 传递原始请求的协议 (http 或 https),用于生成正确的 URL
proxy_set_header X-Forwarded-Proto $scheme;
}
# ==================================
# 后端 API 接口请求转发
# ==================================
location /api/ { # 在 /api 后添加斜杠,是个好习惯,可以避免一些路径拼接问题
# 将所有以 /api/ 开头的请求转发到本地运行的后端服务
proxy_pass http://127.0.0.1:3888/api/; # 这里的斜杠与上面的斜杠对应
# 同样设置必要的代理头,确保后端能获取到正确的请求信息
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 块。
# 原因:Next.js 会为构建产物(如 JS/CSS chunk)生成带哈希值的文件名,
# 并且 Next.js 服务本身会为这些文件设置最佳的缓存头(如 Cache-Control: public, immutable)。
# 让 Next.js 自行管理其静态资源的缓存是更安全、更可靠的做法,可以避免因 Nginx 强缓存导致的前端代码更新不及时问题。
# Next.js 服务已经很好地处理了 /_next/static/* 路径下的资源。
}
}