1、nginx转发:
location / {
proxy_pass http://localhost:3000;
#proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
#proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
2、页面引用/_next/...和输出目录/.next/...不一致问题,修改构建输出目录:
// next.config.js
module.export = {
distDir: '_next'
}
3、字体文件加载 next-plugins:
npm install next-fonts
// next.config.js
const withFonts = require('next-fonts');
module.export = withFonts({
...
});
4、异步加载的js,全局变量需要加判断,否则会闪一下错误信息,例如Google Analytics:
<script async src="https://www.googletagmanager.com/gtag/js"></script>
if (typeof gtag !== "undefined"){
gtag("event", name, {event_category: category});
}
5、Linux系统open files 1024限制导致too many open files错误
修改 /etc/security/limits.conf
* soft nofile 1000000
* hard nofile 1000000
* soft nproc 1000000
* hard nproc 1000000
保存后重新登录,执行 ulimit -a 查看是否生效
6、nginx负载均衡
upstream yourname {
least_conn;
server 127.0.0.1:3006;
server 127.0.0.1:3007;
server 127.0.0.1:3008;
# 该connections参数设置在每个工作进程的缓存中保留的上游服务器的最大空闲keepalive连接数。超过此数量时,将关闭最近最少使用的连接。
# 应特别注意的是,该keepalive指令不限制nginx工作进程可以打开的上游服务器的连接总数。该connections参数应设置为足够小的数字,以允许上游服务器处理新的传入连接。
keepalive 3;
# nginx的长链接是等超时以后才会断开,如果超时时间过长,机器会因为TCP链接耗尽崩溃出现EBUSY错误
keepalive_timeout 5;
}
server {
location / {
proxy_pass http://yourname;
add_header backend-addr $upstream_addr; #方便查看请求被转发到哪个地址
add_header backend-status $upstream_status;
}
}
7、优化Linux系统配置,提高并发能力
修改 /etc/sysctl.conf, 执行 sysctl -p 生效
# 开启重用,将TIME_WAIT的socket重新用于连接
net.ipv4.tcp_tw_reuse = 1
# 默认值1
net.ipv4.tcp_timestamps = 1
# 减少TIME_WAIT状态的持续时间,加快端口释放
net.ipv4.tcp_fin_timeout = 10
# 减少TCP KeepAlive连接探测的等待时间,可以使服务器更快的侦测出异常退出的客户端连接,默认为7200s,WebSocket服务器要增加
net.ipv4.tcp_keepalive_time = 300
# 指定两次 TCP keepalive 探测之间的时间间隔,单位秒
net.ipv4.tcp_keepalive_intvl = 30
# 减少超时前的探测次数,探测次数会增加负载
net.ipv4.tcp_keepalive_probes = 5
# 防止TCP在空闲后重新开始慢启动
net.ipv4.tcp_slow_start_after_idle = 0
# 表示用于向外连接的端口范围。缺省情况下很小:32768 61000,改为1024 65535
net.ipv4.ip_local_port_range = 1024 65535
# 表示SYN队列的长度,默认为1024,加大队列长度为8192,可以容纳更多等待连接的网络连接数
net.ipv4.tcp_max_syn_backlog = 8192
# 增加等待连接队列的最大长度
net.core.somaxconn = 4096
# 表示系统同时保持TIME_WAIT套接字的最大数量,默认180000
net.ipv4.tcp_max_tw_buckets = 180000
# 启SYN Cookies。当出现SYN等待队列溢出时,启用cookies来处理,可防范少量SYN攻击,默认为0
net.ipv4.tcp_syncookies = 1
# 网络设备收发包的队列大小
net.core.netdev_max_backlog = 65535
# 孤儿连接废弃前重试的次数,重负载web服务器建议调小
net.ipv4.tcp_orphan_retries = 3
# TCP三次握手的syn/ack阶段,重试次数,默认5,设为2-3
net.ipv4.tcp_synack_retries = 5
# 所有进程可分配的最大文件描述符
fs.file-max = 1000000
# 单个进程可分配的最大文件描述符
fs.nr_open = 1000000
# 启用 TCP 快速打开
net.ipv4.tcp_fastopen = 3
# 启用 TCP 窗口缩放
net.ipv4.tcp_window_scaling = 1
# 增加 TCP 读写缓冲区大小
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.ipv4.tcp_rmem = 4096 87380 16777216
net.ipv4.tcp_wmem = 4096 65536 16777216
# 启用选择性应答
net.ipv4.tcp_sack = 1
# 优化网络内存
net.core.optmem_max = 65535
# # 禁用 IPv6(如果不需要)
# net.ipv6.conf.all.disable_ipv6 = 1
# net.ipv6.conf.default.disable_ipv6 = 1
# net.ipv6.conf.lo.disable_ipv6 = 1
# 启用 TCP 拥塞控制算法(BBR 如果内核支持)
net.core.default_qdisc = fq
net.ipv4.tcp_congestion_control = bbr
8、nginx配置
user www www;
worker_processes auto;
error_log /www/wwwlogs/nginx_error.log crit;
pid /www/server/nginx/logs/nginx.pid;
worker_rlimit_nofile 1000000;
events
{
use epoll;
worker_connections 51200;
multi_accept on;
}
http
{
include mime.types;
#include luawaf.conf;
include proxy.conf;
default_type application/octet-stream;
server_names_hash_bucket_size 512;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 50m;
sendfile on;
tcp_nopush on;
keepalive_timeout 5;
tcp_nodelay on;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 256k;
fastcgi_intercept_errors on;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types text/plain application/javascript application/x-javascript text/javascript text/css application/xml;
gzip_vary on;
gzip_proxied expired no-cache no-store private auth;
gzip_disable "MSIE [1-6]\.";
limit_conn_zone $binary_remote_addr zone=perip:10m;
limit_conn_zone $server_name zone=perserver:10m;
server_tokens off;
access_log off;
server
{
listen 888;
server_name phpmyadmin;
index index.html index.htm index.php;
root /www/server/phpmyadmin;
#error_page 404 /404.html;
include enable-php.conf;
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 12h;
}
location ~ /\.
{
deny all;
}
access_log /www/wwwlogs/access.log;
}
include /www/server/panel/vhost/nginx/*.conf;
}
9、查看网络连接数
netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'