我的基于 nextjs 的应用程序部署在 AWS EC2 中,使用 nginx 作为负载均衡器/反向代理,一段时间后(比如 5 分钟后)速度就会变慢,特别是当用户登录时。
- 我仅使用 2 个 http cookie 来存储加密的会话和配置文件信息。2. 如果使用后端端口(3000)以及我的服务器 ip(而不是默认端口 80)访问网站,则网站将按预期运行。3. 当我清除浏览器缓存时,会话 cookie 将被删除,网站将开始正常工作。4. 访问日志中频繁出现 408 状态,后续请求也大多导致 408 状态。
以下是我的配置文件。请帮助解决此问题。
proxy_cache_path /var/cache/nginx 级别=1:2 keys_zone=YOPACKCACHE:100m 不活动=7d use_temp_path=off;
#sendfile_max_chunk 1m;
sendfile on;
tcp_nopush on;
proxy_buffering on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
client_header_timeout 3m;
client_body_timeout 3m;
send_timeout 1m;
client_header_buffer_size 5k;
large_client_header_buffers 4 16k;
client_max_body_size 20M;
服务器 { server_name xx.xxx.xxx.xx; 监听 80 默认服务器; 监听 [::]:80 默认服务器; root /var/www/yopacks;
gzip on;
gzip_proxied any;
gzip_comp_level 4;
gzip_types text/css application/javascript image/svg+xml;
proxy_connect_timeout 60s;
proxy_send_timeout 40s;
proxy_read_timeout 50s;
proxy_buffer_size 240k;
proxy_buffers 240 240k;
proxy_busy_buffers_size 240k;
#proxy_temp_file_write_size 64k;
proxy_max_temp_file_size 0;
proxy_pass_header Set-Cookie;
proxy_redirect off;
proxy_hide_header Vary;
proxy_set_header Accept-Encoding '';
proxy_ignore_headers Cache-Control Expires;
proxy_set_header Referer $http_referer;
proxy_set_header Host $host;
proxy_set_header Cookie $http_cookie;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
location = /favicon.ico { log_not_found off; }
location ~* ^/.*\\.(?:jpeg|jpg|gif|png|icu|cur|bmp|webp|gz|svg|ttf)$ {
proxy_cache YOPACKCACHE;
expires 7d;
#add_header Cache-Control "public, max-age=36000, immutable";
proxy_http_version 1.1;
proxy_set_header "Connection" "";
proxy_pass http://127.0.0.1:1337;
}
# Serve any static assets with NGINX
location /_next/static {
proxy_cache YOPACKCACHE;
expires 7d;
alias /var/www/yopacks/.next/static;
add_header Cache-Control "public, max-age=36000, immutable";
}
location / {
try_files $uri $uri/ /_next/$uri
@public;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header "Connection" "";
proxy_pass http://myappcluster;
#proxy_set_header Upgrade $http_upgrade;
#proxy_set_header Connection 'upgrade';
#proxy_cache_bypass $http_upgrade;
#add_header Last-Modified $date_gmt;
#add_header Cache-Control 'no-store, no-cache';
#if_modified_since off;
#expires off;
#etag off;
}
location @public {
proxy_cache YOPACKCACHE;
expires 7d;
别名/var/www/yopacks/public;
proxy_http_version 1.1;
proxy_set_header "Connection" "";
proxy_pass http://myappcluster;
}
location /nginx_status {
stub_status;
}
} ############################################################### nginx.conf 文件如下
用户 www-data;worker_processes 2;pid /run/nginx.pid;error_log /var/log/nginx/error.log debug;包括 /etc/nginx/modules-enabled/*.conf;
事件 { worker_connections 768;
工作者连接数 1000;
multi_accept on;
}
http {
##
# Basic Settings
发送超时1800;
upstream myappcluster {
# The upstream elements lists all
# the backend servers that take part in
# the Nginx load balancer
#hash $binary_remote_addr consistent;
zone upstreams 64K;
server 127.0.0.1:3000;
keepalive 2;
keepalive_timeout 300s;
}
##
#types_hash_max_size 2048;
# server_tokens off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# SSL Settings
##
ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
##
# Gzip Settings
##
gzip on;
# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
#include /etc/nginx/sites-enabled/*;
} ################################################################ 从访问日志中提取的示例(ip 已更改)
41.144.30.98 - - [03/May/2024:06:41:51 +0000] "GET / HTTP/1.1" 408 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36" 41.144.30.98 - - [03/May/2024:06:44:52 +0000] "GET / HTTP/1.1" 408 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36" 客户端计时读取客户端请求标头时出现(110:连接超时),客户端:41.144.30.98,服务器:xx.xxx.xxx.xx,请求:“GET /?category=Appliances&_rsc=1iwkq HTTP/1.1”,主机:“xx.xxx.xxx.xx”,引荐来源:“http://xx.xxx.xxx.xx/”