直接上干货!!!
最近又又又搭了网站(之前的东西,换了个壳 哈哈)
网站:https://smartiny.top/
账号:ch3
密码:123
为了压榨下代码质量,买了个便宜的服务器运行各种逻辑和算法(主要是money不允许),结果发现部署时很多中间件或者服务运行受限,这里分析一下我碰到的前端问题。
问题描述
前端是通过nginx部署的,发现线上运行极其慢(简单的主页刷新也是巨慢)
加起来十秒了,这这这…之前框架是类似的,也没有那么慢。使用 Performance
监控发现CPU占用、JS、内存泄露都不存在,八九就是Nginx的问题了。
直接给出答案
-
性能优化:
- 调整 worker 进程绑定和连接参数
- 启用 HTTP/2 支持
- 添加 Brotli 压缩
- 优化静态资源缓存策略
- 启用 OCSP 装订减少 TLS 握手延迟
-
安全性增强:
- 添加安全头部(X-Frame-Options, X-XSS-Protection 等)
- 配置 HSTS 强制 HTTPS
- 优化 TLS 配置(仅支持 TLS 1.2+,现代加密套件)
- 禁用服务器版本信息
-
代理配置优化:
- 启用 HTTP/1.1 持久连接
- 优化代理缓冲区和超时设置
- 限制客户端上传大小
-
连接管理:
- 增加 keepalive 请求数
- 设置连接超时重置
# 用于优化工作进程性能和资源限制
worker_cpu_affinity auto;
worker_rlimit_nofile 65535;
# 优化网络连接处理模型
events {
use epoll;
multi_accept on;
worker_connections 8192;
}
# http下的字段。优化 HTTP 长连接管理、请求处理上限和超时连接回收
keepalive_timeout 75s;
keepalive_requests 1000;
reset_timedout_connection on;
# http下的字段。启用并优化 Gzip 压缩功能,通过减小传输数据量来提升网站加载速度
gzip on;
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_types text/plain text/css text/javascript application/javascript application/json image/svg+xml;
# http-server下的。 增强 HTTPS 安全性和设置安全响应头
ssl_stapling on;
ssl_stapling_verify on;
resolver 8.8.8.8 8.8.4.4 valid=300s;
resolver_timeout 5s;
# 安全头部
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;
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;
# 移除版本信息
server_tokens off;
# http-server下的。静态资源缓存策略
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$ {
root /root/cry/dist;
expires 1y;
add_header Cache-Control "public, max-age=31536000";
access_log off;
tcp_nodelay off;
}
location ~* \.html$ {
root /root/cry/dist;
add_header Cache-Control "no-cache, no-store, must-revalidate";
add_header Pragma "no-cache";
add_header Expires 0;
}
优化完,超级快!!!基本不会超过200ms